Improve performance of deepcopy by setting keep alive key in the memo
Personne n'a encore pris cette issue.
- Langage dominant
- Python
- Étoiles
- 77.2k
- Forks
- 35.9k
- Métriques de merge des PR
- Métriques de PR en attente
Description
Feature or enhancement
Proposal:
In the deepcopy implementation an exception is generated when the memo dict does not contain a key id(memo). We can avoid generating an exception by adding this key when the memo is generated. As a consequence the _keep_alive method is just a single line of code and we can inline this.
There is a behavior change: a call deepcopy(x, {}) works with current main, but will raise an exception (since the key id(memo)) has not been set. This might be acceptable as the documentation states "The memo dictionary should be treated as an opaque object." (see https://docs.python.org/3/library/copy.html). And in the code itself the memo argument is not documented at all https://github.com/python/cpython/blob/9aea9c100fbbaa1437f595da65417204872106a9/Lib/copy.py#L3-L9
There are some unit tests involving the memo, but those seem to test the behavior of the deepcopy and not of the memo argument.
If the behavior change is not acceptable because memo is part of the public interface, should we then instead document the structure of the argument memo` as being an implementation detail?
The argument _nil as a sentinel to the deepcopy is redundant, as the memo can only contain integers so we can use None instead.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
Linked PRs
- gh-123746
Guide de contribution
Ouvrir le guide de contribution
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.
Piste de recherche
Commencez par lire Lib/copy.py et les tests unitaires existants qui testent deepcopy avec un memo. Examinez la PR liée gh-123746 et déterminez si le comportement de memo constitue une modification de l’interface publique ; le travail est terminé lorsque l’implémentation choisie, les tests et toute documentation nécessaire définissent le comportement de manière cohérente.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- python
- Domaine
- backend
- Type d'issue
- Fonctionnalité
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 25/100