devito icon indicating copy to clipboard operation
devito copied to clipboard

misc: Thread-safe cache rewrite

Open enwask opened this issue 5 months ago • 1 comments

Rewrites memoized_meth and memoized_generator for concurrency, as well as dropping the legacy memoized_func in favor of functools.cache. Also adds a global lock for the symbol cache allowing thread-safe Symbol construction and cache manipulation.

The memoized_meth decorator now stores one cache per thread, whereas memoized_generator stores a single cache for a given method (though there may still be misses if the cache is initialized concurrently). This means neither method cache has a call-once guarantee.

Memoized generators block for the initial call of the generator function and construct a thread-safe version of itertools.tee, which allows for concurrent iteration (but blocks when iterating elements that are not yet in the buffer). After the source generator is consumed, there is no blocking and subsequently threads can iterate the buffer in parallel (see SafeTee in tools.memoization).

enwask avatar Jul 24 '25 12:07 enwask

Codecov Report

:x: Patch coverage is 89.13043% with 25 lines in your changes missing coverage. Please review. :white_check_mark: Project coverage is 87.52%. Comparing base (cd9058b) to head (8bb95f2). :warning: Report is 145 commits behind head on main.

Files with missing lines Patch % Lines
tests/test_tools.py 83.33% 17 Missing :warning:
devito/tools/memoization.py 95.18% 1 Missing and 3 partials :warning:
devito/types/basic.py 84.61% 1 Missing and 1 partial :warning:
devito/types/caching.py 84.61% 0 Missing and 2 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2683      +/-   ##
==========================================
+ Coverage   78.59%   87.52%   +8.92%     
==========================================
  Files         245      245              
  Lines       49089    49230     +141     
  Branches     4322     4322              
==========================================
+ Hits        38582    43087    +4505     
+ Misses       9714     5408    -4306     
+ Partials      793      735      -58     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Aug 05 '25 10:08 codecov[bot]