Skip to content

Conversation

@ChrisRackauckas-Claude
Copy link
Contributor

Summary

  • Add @inbounds annotations to generated RHS code loops for ~43% speedup
  • Add allocation tests to prevent performance regressions
  • Ensure all hot paths remain allocation-free

Performance Benchmarks

RHS Evaluation (Telegraph model, 2x21x81 state space):

Metric Before After Improvement
Time ~101μs ~58μs 43%
Allocations 0 0

Index Iteration:

  • singleindices: 0 allocations
  • pairedindices: 0 allocations

Changes

src/build_rhs.jl

  • Add @inbounds to the first pass loop (singleindices iteration)
  • Add @inbounds to the second pass loops (pairedindices iteration)

src/build_rhs_ss.jl

  • Add @inbounds to the steady-state single pass loop

src/matrix.jl

  • Add @inbounds to sparse matrix construction loops

test/alloc_tests.jl (new)

  • Tests that RHS evaluation is allocation-free
  • Tests that index iteration is allocation-free
  • Tests the full Telegraph model

The @inbounds optimization is safe because:

  1. singleindices returns CartesianIndices which are guaranteed valid
  2. pairedindices returns index pairs computed to be within bounds
  3. The index handlers ensure all generated indices are valid

Test Plan

  • All existing tests pass
  • New allocation tests pass
  • Benchmarks show improvement

cc @ChrisRackauckas

🤖 Generated with Claude Code

This PR improves the performance of the RHS evaluation by adding @inbounds
annotations to the generated code loops. This eliminates bounds checking
overhead in the hot paths.

## Performance Improvements

**RHS Evaluation Benchmark (Telegraph model, 2x21x81 state space):**
- Before: ~101μs per call
- After: ~58μs per call
- **Speedup: ~43%**

## Changes

### Performance Optimizations
- Add @inbounds to singleindices loop in build_rhs_firstpass
- Add @inbounds to pairedindices loops in build_rhs_secondpass
- Add @inbounds to steady-state RHS single pass loop
- Add @inbounds to sparse matrix construction loops

### Testing
- Add allocation tests to verify RHS evaluation remains allocation-free
- Add tests for singleindices and pairedindices iteration
- Add allocation test for the 3-species Telegraph model

The @inbounds optimization is safe because:
1. singleindices returns CartesianIndices which are guaranteed valid
2. pairedindices returns index pairs that are computed to be within bounds
3. The index handlers ensure all generated indices are valid

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.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.

2 participants