Skip to content

perf(processors): allocation-free early-exit contains_nan guard in all nopython kernels - #220

Merged
iguinn merged 2 commits into
legend-exp:mainfrom
ggmarshall:perf-contains-nan
Sep 1, 2026
Merged

iguinn merged 2 commits into
legend-exp:mainfrom
ggmarshall:perf-contains-nan

Conversation

@ggmarshall

@ggmarshall ggmarshall commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

Split out of #217 (per review request there) — middle of the stack. #219 has merged, so this is now a single commit on main: just the NaN-guard change. #217 is stacked on top of this.

Adds utils.contains_nan() — an allocation-free, early-exit, @guvectorize-compatible NaN scan — and replaces the np.isnan(w).any() guards in all nopython processor kernels with it. np.isnan(w).any() allocates a boolean array per call and always scans the full waveform; contains_nan allocates nothing, vectorizes, and exits at the first NaN. Measured on real L200 p18 waveform blocks: ~2× on processors whose real work is O(1) (pickoffs, min/max, etc.), ~1.1× on scan-heavy ones.

Outputs are bit-identical — verified end-to-end by rebuilding a full 199-channel L200 raw file and comparing column-by-column against a stock-dspeed control run (report: https://claude.ai/code/artifact/6d1c437c-c519-4797-a510-993d6aa80e98).

🤖 Generated with Claude Code

Copilot AI lite review requested due to automatic review settings September 1, 2026 00:16

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

contains_nan hard-codes Numba caching (cache=True) and therefore bypasses the existing DSPEED_CACHE/numba_defaults.cache control mechanism used elsewhere.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR introduces a Numba-jitted utils.contains_nan() helper to provide an allocation-free, early-exit NaN scan, and uses it to replace np.isnan(...).any() guards inside many nopython processor kernels to reduce per-call overhead.

Changes:

  • Add contains_nan() in src/dspeed/utils.py as an optimized NaN guard for nopython kernels.
  • Replace np.isnan(w).any() waveform/array guards across many processors with contains_nan(...).
  • Update processor imports to use the new helper.
File summaries
File Description
src/dspeed/utils.py Adds contains_nan() Numba helper for allocation-free NaN checks.
src/dspeed/processors/windower.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/wf_correction.py Use contains_nan for waveform/correction NaN guards.
src/dspeed/processors/wf_alignment.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/upsampler.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/trap_filters.py Use contains_nan for waveform NaN guards in trap-family processors.
src/dspeed/processors/time_point_thresh.py Use contains_nan for waveform/threshold NaN guards.
src/dspeed/processors/time_over_threshold.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/sort.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/soft_pileup_corr.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/saturation.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/recursive_filter.py Use contains_nan for input coefficient/waveform NaN guards.
src/dspeed/processors/rc_cr2.py Use contains_nan for waveform NaN guards and output validation.
src/dspeed/processors/pulse_injector.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/presum.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/poly_fit.py Use contains_nan for waveform/parameter NaN guards.
src/dspeed/processors/pole_zero.py Use contains_nan for waveform NaN guards and output validation.
src/dspeed/processors/pmt_pulse_injector.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/pad.py Use contains_nan for waveform-slice NaN guard.
src/dspeed/processors/moving_windows.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/min_max.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/log_check.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/linear_slope_fit.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/inl_correction.py Use contains_nan for INL array NaN guard.
src/dspeed/processors/histogram.py Use contains_nan for waveform NaN guards.
src/dspeed/processors/histogram_stats.py Use contains_nan for weights NaN guards.
src/dspeed/processors/get_wf_centroid.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/get_multi_local_extrema.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/fixed_time_pickoff.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/bl_subtract.py Use contains_nan for waveform NaN guard.
src/dspeed/processors/arithmetic.py Use contains_nan for waveform NaN guard.
Review details
  • Files reviewed: 31/31 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/dspeed/utils.py Resolved
Comment thread src/dspeed/utils.py Resolved
@codecov

codecov Bot commented Sep 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 59.78261% with 37 lines in your changes missing coverage. Please review.
✅ Project coverage is 66.79%. Comparing base (a5d6c59) to head (9bf5ae1).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/dspeed/utils.py 18.75% 13 Missing ⚠️
src/dspeed/processors/moving_windows.py 20.00% 4 Missing ⚠️
src/dspeed/processors/poly_fit.py 25.00% 3 Missing ⚠️
src/dspeed/processors/linear_slope_fit.py 33.33% 2 Missing ⚠️
src/dspeed/processors/soft_pileup_corr.py 33.33% 2 Missing ⚠️
src/dspeed/processors/upsampler.py 33.33% 2 Missing ⚠️
src/dspeed/processors/bl_subtract.py 50.00% 1 Missing ⚠️
src/dspeed/processors/histogram.py 66.66% 1 Missing ⚠️
src/dspeed/processors/histogram_stats.py 66.66% 1 Missing ⚠️
src/dspeed/processors/log_check.py 50.00% 1 Missing ⚠️
... and 7 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #220      +/-   ##
==========================================
+ Coverage   66.74%   66.79%   +0.04%     
==========================================
  Files          61       61              
  Lines        4066     4111      +45     
==========================================
+ Hits         2714     2746      +32     
- Misses       1352     1365      +13     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

ggmarshall and others added 2 commits September 1, 2026 03:36
…ee contains_nan

In nopython numba kernels np.isnan(w).any() heap-allocates a boolean
array and always scans the full input. contains_nan() (new in utils)
allocates nothing, vectorizes via a fixed-width inner loop, and exits
at the first NaN. Applied to all nopython kernels (object-mode kernels
keep numpy's C implementation). Outputs are bit-identical; ~2x on
O(1)-work processors like fixed_time_pickoff, ~10% on scan-heavy ones.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…dd unit tests

Addresses PR review: cache=True was hard-coded, bypassing the
DSPEED_CACHE/DSPEED_BOUNDSCHECK mechanism used by all processors.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants