[perf] Constructors where `this` escapes are slow
Maintainer antworten meist innerhalb von 1 Tag
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
- Issue-Typ
- Refactoring
- Klarheit
- Größtenteils klar
- Aktivitätsstatus
- Aktiv
- Bereich
- compilers, performance
Rechercherichtung
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.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
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.
- Vorherrschende Sprache
- Rust
- Sterne
- 310
- Forks
- 18
- Ø Merge
- 10 Std. 18 Min.
- Gemergte PRs (30 T.)
- 125
Entwicklungsumgebung
Dieses Projekt bietet weder Dev-Container noch Dockerfile noch Beitragsleitfaden – die Einrichtung liegt bei Ihnen. Beginnen Sie mit der README; die allgemeinen Schritte stehen in unserem Leitfaden für den ersten Beitrag.
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus Cpp2Rust/cpp2rust
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 Ein halber Tag Anfängerfreundlichkeit 55/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 56/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 4/5 1-2 Tage Anfängerfreundlichkeit 35/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 56/100
Maintainer antworten meist innerhalb von 1 Tag
Alle Issues in Cpp2Rust/cpp2rust
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
NSPG13/agent-bounties#1749 ·
Maintainer antworten meist innerhalb von 1 Tag
-
area: web bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
kinqsradio/orbyn-engineering#227 ·
Maintainer antworten meist innerhalb von 1 Tag
-
agentic-workflows automation repo-assist
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 85/100
shanselman/winget-tui#483 ·
Maintainer antworten meist innerhalb von 2 Tagen
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 70/100
lightonai/next-plaid#205 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
apache/datafusion-iceberg#100 ·
Maintainer antworten meist innerhalb von 1 Tag