cta-observatory / ctapipe

Low-level data processing pipeline software for CTAO or similar arrays of Imaging Atmospheric Cherenkov Telescopes
https://ctapipe.readthedocs.org
BSD 3-Clause "New" or "Revised" License
62 stars 265 forks source link

Use lru_cache instead of custom caching #2574

Open kosack opened 2 days ago

kosack commented 2 days ago

https://github.com/cta-observatory/ctapipe/blob/b63a7d4de5d65520cdf721d8b56b8990e43c2673/src/ctapipe/instrument/camera/geometry.py#L934-L935

The hand-crafted border pixel caching could just use the @functools.lru_cache or @functools.cache decorator. Is there a reason not to?

maxnoe commented 2 days ago

lrc_cache, at least in the past, didn't play nice with class methods.

The cache was global, not per instance.

maxnoe commented 2 days ago

https://docs.python.org/3/faq/programming.html#faq-cache-method-calls

seems to be supported now.

But:

The advantage of the least recently used algorithm is that the cache is bounded by the specified maxsize. The disadvantage is that instances are kept alive until they age out of the cache or until the cache is cleared.