`TreeInterpreter` creates reference cycle, causing GC pressure
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
- Tipo de issue
- Error
- Claridad
- Bastante claro
- Estado de actividad
- Estancado
- Stack tecnológico
- python
- Área
- performance
Línea de trabajo
Comienza en parser.py, en ParsedResult.search(), e inspecciona la creación de TreeInterpreter; después, lee en visitor.py alrededor de Visitor._method_cache para confirmar el ciclo de referencias del método ligado. Compara los enfoques propuestos para almacenar en caché el intérprete y ejecuta el reproductor proporcionado con un millón de iteraciones; se considera terminado cuando se evitan los ciclos repetidos de TreeInterpreter y la presión de GC asociada.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
We recently noticed that a heavy JMESpath workload was triggering a large number of garbage collection runs. We are using jmespath.compile(), and we tracked this down to the jmespath.visitor.TreeInterpreter that is created on every call to `ParsedResult.search(): https://github.com/jmespath/jmespath.py/blob/bbe7300c60056f52413603cf3e2bcd0b6afeda3d/jmespath/parser.py#L508
It appears that TreeInterpreter creates a reference cycle, which leads to the GC being triggered frequently to clean up the cycles. As far as I can tell, the problem comes from the Visitor._method_cache: https://github.com/jmespath/jmespath.py/blob/bbe7300c60056f52413603cf3e2bcd0b6afeda3d/jmespath/visitor.py#L91-L93
...which store references to methods that are bound to self in a member of self.
Possible solution
We worked around the problem by monkey patching ParsedResult so that it (1) caches a default_interpreter for use when options=None, and (2) uses it in search(). If I understand correctly, we could go further and use a global TreeInterpreter for all ParsedResult instances. The TreeInterpreter seems to be stateless apart from self._method_cache and that implementation seems to be thread-safe (with only the risk of multiple lookups for the same method in a multithreaded case).
I'd be happy to contribute a PR for either version if this would be welcome.
How to reproduce
The following reproducer shows the problem:
import jmespath
import gc
gc.set_debug(gc.DEBUG_COLLECTABLE)
pattern = jmespath.compile("foo")
value = {"foo": "bar"}
for _ in range(1000000):
pattern.search(value)
...where the output contains one million repetitions of something like:
gc: collectable <TreeInterpreter 0x10f634fa0>
gc: collectable <dict 0x10f63e780>
gc: collectable <Options 0x10f634520>
gc: collectable <Functions 0x10f6345b0>
gc: collectable <method 0x10f63ee80>
gc: collectable <dict 0x10f63eb00>
- Lenguaje dominante
- Python
- Estrellas
- 2.5k
- Forks
- 231
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de jmespath/jmespath.py
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
jmespath/jmespath.py#401 ·
-
floor() and ceil() raise OverflowError / ValueError on Infinity and NaNPosiblemente ocupada @guocn la tomó hace 48 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
jmespath/jmespath.py#381 ·
-
Lexer gives quoted identifiers an `end` that is the token length, not its end offsetPosiblemente ocupada @chiliec la tomó hace 50 días. Abierto
Dificultad 1/5 Menos de una hora Aptitud para principiantes 20/100
jmespath/jmespath.py#380 ·
-
sdist is missing `tests/__init__.py`Posiblemente ocupada @deepakganesh78 la tomó hace 127 días. Abiertobug
Dificultad 2/5 1-3 horas Aptitud para principiantes 38/100
jmespath/jmespath.py#341 · 1 comentario ·
-
Different behavior on merge function between the JS lib and the Python libQuizá libre de nuevo Un pull request para esta issue se cerró sin fusionarse. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 42/100
jmespath/jmespath.py#329 · 1 comentario ·
Todos los issues de jmespath/jmespath.py
Issues similares
-
test_real_pandoc_failure_is_one_path_free_line fails on macOS CI with current Homebrew pandocAbiertobug TAG: unit tests
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
learningequality/ricecooker#898 ·
-
task
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
freelawproject/litigant-portal#1285 ·
Los mantenedores suelen responder en 2 días
-
least_squares returns a nonfinite objective for an invalid initial residualPosiblemente ocupada @sylvesterkaczmarek la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
google-deepmind/mujoco#4023 ·
Los mantenedores suelen responder en 11 días
-
Five Australian birds: Red-capped Parrot, Red-winged Fairywren, Gilbert’s Honeyeater, Western Spinebill, Western WattlebirdPosiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
arnegiacomo/fugleramme#427 ·
Los mantenedores suelen responder en 1 día
-
MONDO's zebu and cat Arnold-Chiari terms are is_a a dog disease (monarch-initiative/mondo#10939)Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
NCATSTranslator/Babel#1317 ·
Los mantenedores suelen responder en 2 días