python / python/cpython

Improve performance of deepcopy by setting keep alive key in the memo

Abierto
#123,745 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

stdlib type-feature
Lenguaje dominante
Python
Estrellas
77.2k
Forks
35.9k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza leyendo Lib/copy.py y las pruebas unitarias existentes que ejercitan deepcopy con un memo. Revisa el PR vinculado gh-123746 y determina si el comportamiento de memo constituye un cambio en la interfaz pública; se considera terminado cuando la implementación elegida, las pruebas y cualquier documentación necesaria definan el comportamiento de forma coherente.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
backend
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.