(perf): zero-allocation RUNTIME_CHECK=1 hot path#37
Merged
Conversation
Eliminate heap allocations in S=1 safety checks after warmup:
- _check_wrapper_mutation!: replace MemoryRef boxing (getfield(arr,:ref).mem)
with ccall(:jl_array_ptr) pointer comparison, and NTuple boxing
(prod(getfield(arr,:size))) with length(::Array{T}) — 48→0 bytes/acquire
- _check_pointer_overlap: extract closure into @noinline _check_tp_pointer_overlap
and use @generated _check_all_slots_pointer_overlap for zero-allocation
dispatch over fixed slots — 128→0 bytes/call
- Apply same fixes to CUDA and Metal extensions
- Add S=1 zero-allocation tests (single/multi-type, N-D, overlap, nested)
- Show @info on load when RUNTIME_CHECK is enabled
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #37 +/- ##
==========================================
+ Coverage 96.61% 96.65% +0.04%
==========================================
Files 14 14
Lines 2748 2753 +5
==========================================
+ Hits 2655 2661 +6
+ Misses 93 92 -1
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR eliminates runtime allocations in the RUNTIME_CHECK=1 hot path by removing boxing sources in wrapper mutation detection and by replacing an allocating overlap-check closure with generated, per-slot unrolled dispatch.
Changes:
- Refactors pointer-overlap checks to avoid closure capture allocations (CPU/CUDA/Metal).
- Removes boxing in wrapper mutation checks by using
jl_array_ptrandlengthinstead ofMemoryRef/prod(dims). - Adds S=1 (RUNTIME_CHECK=1) zero-allocation regression tests.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
src/debug.jl |
Refactors overlap checking to generated per-slot calls; updates wrapper-mutation checks to avoid boxing. |
src/types.jl |
Adds an informational log when RUNTIME_CHECK is enabled. |
test/test_zero_allocation.jl |
Adds new zero-allocation regression tests for S=1 (runtime-check enabled) paths. |
ext/AdaptiveArrayPoolsCUDAExt/debug.jl |
Mirrors CPU overlap-check refactor for CUDA pools to avoid do-block closure allocations. |
ext/AdaptiveArrayPoolsMetalExt/debug.jl |
Mirrors CPU overlap-check refactor for Metal pools to avoid do-block closure allocations. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Replace length(wrapper::Array{T}) with _wrapper_prod_size(wrapper) function
barrier that reads getfield(wrapper, :size) directly. length() does not
reflect setfield!(:size) on Julia 1.11, causing mutation detection to miss
wrapper growth beyond backing vector.
- Add wrapper::Array assertion before ccall(:jl_array_ptr) to prevent segfault on corrupted wrapper (safe TypeError instead) - Reduce S=1 zero-alloc test iterations from 1000 to 100 (align with existing tests, reduce CI time)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
RUNTIME_CHECK=1allocated 48 bytes/acquire + 128 bytes per overlap check after warmup, from two sources:_check_wrapper_mutation!—wrapper::Array(type-erased fromVector{Any}) causedMemoryRefandNTuple{N,Int}boxing_check_pointer_overlap— closure capturing 6 variables into heap-allocated objectFix
getfield(arr,:ref).mem→ccall(:jl_array_ptr),prod(getfield(arr,:size))→length(::Array{T})@noinlinefunction +@generatedunrolling overFIXED_SLOT_FIELDS@infoon RUNTIME_CHECK enabledResult
All S=1 patterns (single/multi-type, N-D, nested, overlap check): 0 bytes after warmup.