GalSim-developers / GalSim-developers/GalSim
Investigate functools.lru_cache
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 272
- Forks
- 121
- PR merge metrics
- No merged PRs in 30d
Description
I'm not sure if functools.lru_cache existed when we added galsim.utilities.LRU_Cache, but I've been playing around with it recently and I think I like it better. With this simple program:
from functools import lru_cache
from galsim.utilities import LRU_Cache
import numpy as np
@lru_cache(1024)
# @LRU_Cache
def f(x):
return x*2+np.sin(3*x)+np.cos(5.5*x+0.2*x)**2
def g(x):
return f(x) + f((x+31)%1000)
def main():
for i in range(1000000):
g(i%1000)
if __name__ == '__main__':
main()
I get better performance with functools.lru_cache (~1.0 s vs ~1.8 s), and also like the output I get through gprof2dot better:
utilities.LRU_Cache

functools.lru_cache

Querying the cache hits/misses is also nice with functools.lru_cache.
The one feature we'd be giving up is the ability to resize the already-created cache. It's definitely nice for the user to be able to set the size, so I think we'd need to invent some kind of API wrapper for that. Looks like there's a __wrapped__ attribute that might help.
Finally, functools.lru_cache is only available for python >3.2, so this would be easiest if we dropped support for 2.7.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting galsim.utilities.LRU_Cache and the project's supported Python versions, then compare its behavior with functools.lru_cache. Determine whether a replacement and a wrapper for resizing are needed, including the loss of Python 2.7 support, and document or implement the chosen API with cache hit/miss behavior preserved.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100