[perf] Constructors where `this` escapes are slow
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 領域
- compilers, performance
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- Rust
- スター
- 310
- フォーク
- 18
- 平均マージ
- 10時間 18分
- マージ済み PR(30日)
- 125
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Cpp2Rust/cpp2rust のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 半日 初心者へのやさしさ 55/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 56/100
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 1〜2日 初心者へのやさしさ 35/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 56/100
メンテナーはふだん 1 日以内に返信
Cpp2Rust/cpp2rust の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
fritzprix/libr-agent#2612 ·
メンテナーはふだん 1 日以内に返信
-
crate: spate-core priority: low
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
NSPG13/agent-bounties#1749 ·
メンテナーはふだん 1 日以内に返信
-
area: web bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
kinqsradio/orbyn-engineering#227 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信