GeezerVison is showing github.com — BUG: f2py.crackfortran mutates global re._MAXCACHE at import time · Issue #31104 · numpy/numpy
BUG: f2py.crackfortran mutates global re._MAXCACHE at import time #31104DescriptionDescribe the issue:
https://github.com/numpy/numpy/blob/main/numpy/f2py/crackfortran.py#L246
re._MAXCACHE = 50 takes effect when this file is in the import chain, e.g. when scipy exists
This causes really hard to locate performance issues for NLP workflows. (I don't even know this module exists yet it silently slows my pipeline down by 100x)
Regex's default cache is 512, and evicts LRU style. No reason to change it process-wide, please consider removing it.
Reproduce the code example:
import re
print(re._MAXCACHE) # default 512
import nltk # Doesn't even need numpy or regex, all you need is scipy installed or not
print(re._MAXCACHE) # this now is 50
# if you don't have nltk, this does the trick too
import scipy.linalg
Python and NumPy Versions:
3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 (clang-1300.0.29.30)]
But any version would work
Runtime Environment:
[{'numpy_version': '2.4.4',
'python': '3.11.0 (v3.11.0:deaf509e8f, Oct 24 2022, 14:43:23) [Clang 13.0.0 '
'(clang-1300.0.29.30)]',
'uname': uname_result(system='Darwin', node='MacBook-Air.local', release='24.6.0', version='Darwin Kernel Version 24.6.0: Mon Jan 19 22:01:41 PST 2026; root:xnu-11417.140.69.708.3~1/RELEASE_ARM64_T8132', machine='arm64')},
{'simd_extensions': {'baseline': ['NEON', 'NEON_FP16', 'NEON_VFPV4', 'ASIMD'],
'found': ['ASIMDHP', 'ASIMDDP'],
'not_found': ['ASIMDFHM']}},
{'ignore_floating_point_errors_in_matmul': True}]
How does this issue affect you or how did you find it:
Shrinking re._MAXCACHE silently made re.sub() 100 times slower if your code involves repeatedly calling with high frequency without regex compile.
In my case, I didn't even use numpy, I use NLTK the language pack. And by just including (not using) scipy in my docker image, my workflow is 100 times slower. Because f2py is involved somewhere in the import chain of import nltk. Reacted by Joren Hammudoglu and Warren Weckesser ActivityWarrenWeckesser commented on Mar 30, 2026 Thanks for reporting the issue, @cmutrufflepig.
The line
has been there for more than 20 years. Since _MAXCACHE appears to be a private module attribute, modifying it like we do is a bad practice (although pointers to evidence to the contrary are welcome).
I suspect we could simply remove that line and use the default. Computers generally have a lot more memory now than they did 20 years ago. If I remove that line locally, all the tests still pass. added a commit that references this issue on Mar 30, 2026This issue will close once commit 57a2476 is merged into the 'master' branch.57a2476 WarrenWeckesser commented on Mar 30, 2026 PR to remove the line: #31105. cmutrufflepig commented on Mar 30, 2026 Hi @WarrenWeckesser
Thank you for the response. I thought about old hardware limitations, but truly didn't expect it to be 20 year old code.
Removing that line was my first proposal in the issue too. I really can't think of any good reason for it to exist or deleting it posts any risk. I was gonna create a PR straight, but opened an issue just to be safe.
When can I use this in a standard release? Right now we patch it by setting it back to 512 after every import. WarrenWeckesser commented on Mar 30, 2026
When can I use this in a standard release? Right now we patch it by setting it back to 512 after every import.
The PR will have to be reviewed, and if approved it will be merged into the main development branch. Then it would definitely be in NumPy 2.5.0. I don't know if it will be backported, but if so, it would end up in 2.4.5, since 2.4.4 was released just yesterday. There is no guarantee that there will be a 2.4.5, nor a definite schedule for its release if there is one. added a commit that references this issue on Apr 10, 2026This issue will close once commit c58520f is merged into the 'main' branch.c58520f
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|