[perf] Constructors where `this` escapes are slow
Les mainteneurs répondent en général sous 1 jour
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 35/100
- Type d'issue
- Refactorisation
- Clarté
- Plutôt claire
- Activité
- Active
- Domaine
- compilers, performance
Piste de recherche
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.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
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.
- Langage dominant
- Rust
- Étoiles
- 310
- Forks
- 18
- Merge moyen
- 10 h 18 min
- PR mergées (30 j)
- 125
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de Cpp2Rust/cpp2rust
-
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 Une demi-journée Accessibilité débutants 55/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 56/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 4/5 1-2 jours Accessibilité débutants 35/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 3/5 1-2 jours Accessibilité débutants 56/100
Les mainteneurs répondent en général sous 1 jour
Toutes les issues de Cpp2Rust/cpp2rust
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 66/100
ScoopInstaller/Extras#19275 ·
Les mainteneurs répondent en général sous 1 jour
-
area:plugins kind:bug priority:p2 ready
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 85/100
Les mainteneurs répondent en général sous 1 jour
-
bug priority: medium ui
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
storytold/gridcraft#3496 · 1 commentaire ·
Les mainteneurs répondent en général sous 1 jour
-
bip39: Mnemonic::new_from_phrase rejects phrases with extra or non-space whitespacePeut-être pris @chukwudiikeh l’a pris aujourd’hui. Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 90/100
Les mainteneurs répondent en général sous 1 jour