Skip to content

BUG: f2py.crackfortran mutates global re._MAXCACHE at import time #31104

Description

@cmutrufflepig

Describe 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions