[perf] Constructors where `this` escapes are slow

Đang mở
#582 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Tái cấu trúc
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, rust
Lĩnh vực
compilers, performance

Hướng nghiên cứu

The issue describes how the translator emits constructors for C++ types whose this escapes, so start in the code that generates the Rust new function for constructors. Read the godbolt example linked in the issue to compare the inline and escaping cases. The proposed change is to allocate the storage before the constructor call and pass it in; done means escaping-this output no longer copies the struct and the existing transpiler tests still pass. The Rc/RefCell case is a separate, related cost.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

In unsafe constructors are translated as:

fn new() -> Self {
  let this = Self { ... };
  ... body of the constructor ...
  this
}

When this escapes in the body of the constructor, for example init(&mut this), the translated new becomes slower than necessary: this is constructed inside the body then memcpy'ed in the caller.

When this does not escape in the body of the constructor, Rust returns the this inline, without a memcpy. See this example.

C++ does no memcpy. The semantics of the constructor is: a storage, i.e. this, is passed to the constructor, the constructor initialises this, then the storage is returned.

I think that the solution for this performance problem is to follow what C++ does: create this before calling the constructor and pass the storage to the constructor. This would also fix #248.


In performance terms, refcount is worse because it does 3 memcpy's even when this does not escape:

fn new() -> Self {
  let __this = Rc::new(RefCell::new(Self { ... }));
  let this = __this.as_pointer();
  Rc::try_unwrap(this) // memcpy 1: transforms Rc<RefCell<S>> into Result<RefCell<S>, Rc<RefCell<S>>>
    .ok().unwrap() // memory 2: transforms Result<RefCell<S>, Rc<RefCell<S>>> into RefCell<S>
    .into_inner() // memcp 3: transforms RefCell<S> into S
}

The solution is the same as unsafe: create this before calling the constructor and pass the storage to the constructor.

Ngôn ngữ chính
Rust
Star
310
Fork
18
Merge trung bình
10 giờ 18 phút
Pull request đã merge (30 ngày)
125

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Cpp2Rust/cpp2rust

Tất cả issue của Cpp2Rust/cpp2rust

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.