Repository navigation
Vectorize lazy - #985
Vectorize lazy#985matteobachetti wants to merge 8 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Address the fixed-signature initialization, thread-safety, and Numba compatibility concerns.
Get a fresh assessment by requesting another Copilot review.
Pull request overview
This pull request adds lazy Numba vectorization to reduce import time while preserving fixed-signature precision.
Changes:
- Adds deferred compilation and caching via
lazy_vectorize. - Migrates statistics and Fourier helpers.
- Adds regression tests and changelog entries.
File summaries
| File | Summary |
|---|---|
stingray/utils.py |
Implements lazy vectorization. |
stingray/tests/test_utils.py |
Tests lazy compilation behavior. |
stingray/tests/test_stats.py |
Tests precision and large trial counts. |
stingray/tests/test_fourier.py |
Tests complex precision handling. |
stingray/stats.py |
Applies lazy vectorization to statistics helpers. |
stingray/fourier.py |
Applies lazy vectorization to Fourier helpers. |
docs/changes/985.feature.rst |
Documents import-time performance improvements. |
docs/changes/985.bugfix.rst |
Documents precision and overflow fixes. |
Review details
Suppressed comments (2)
stingray/utils.py:141
- An empty signature list is treated as the "not compiled yet" sentinel here. With
signatures=[],_compile_for_argsfalls through toDUFunc's dynamic compilation, so later input-type combinations can add loops instead of being rejected, contradicting the fixed-signature contract. Reject an empty list during initialization (or use a separate initialization flag).
signatures = getattr(self, "_lazy_signatures", None)
if not signatures:
return False
stingray/utils.py:115
- These imports depend on Numba's private
numba.core/numba.npmodules, whilenumbais an unpinned optional dependency inpyproject.toml. A Numba release that moves either symbol will make importingstingray.utilsfail before theHAS_NUMBAfallback can run; please constrain the supported Numba range or add a compatibility path that preserves the optional-dependency fallback.
from numba.core import sigutils
from numba.np.numpy_support import ufunc_find_matching_loop
from numba.np.ufunc.dufunc import DUFunc
- Files reviewed: 8/8 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
…re not compiled again at each import
…first call, for fixed float32 and float64 signatures
…ctions, so that float32 inputs are computed in float64 and the first call compiles less
…vectorize, and explain why each new test is needed
…nsupported types, and when pickled; exclude its unreachable fallback from coverage
…piler lock, so that concurrent first calls are safe
db8a6bd to
a272d77
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #985 +/- ##
==========================================
+ Coverage 96.69% 96.74% +0.05%
==========================================
Files 48 48
Lines 11219 11266 +47
==========================================
+ Hits 10848 10899 +51
+ Misses 371 367 -4
... and 4 files with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
Importing Stingray was slow because the Numba
@vectorizefunctions instats.pyandfourier.pylisted their signatures, so they were compiled again at every import, which took about 1.6 s.This PR adds
stingray.utils.lazy_vectorize, which compiles a fixed list of signatures the first time a function is called (from Python or from Numba-compiled code), caches them on disk, and then compiles nothing else.Simply dropping the signatures would not have been safe: Numba then compiles a loop for each new combination of input types, and after a call with float32 and float64 arrays, a float64 array combined with a Python float could be computed in float32. Each function now has a single float64 signature, so float32 inputs are computed in float64 (with or without Numba), a number of trials ≥ 2³¹ no longer raises
OverflowError, andimport stingraydrops from 3.0 s to 1.4 s, at the cost of about 0.1 s the first time these functions are called.Disclosure: I investigated the slow loading of stingray with Claude, this is work I've done with its assistance. I verified each step and decided the tradeoffs (e.g. fewer function signatures for faster loading).