(fix): eliminate Core.Box allocation in @inline @with_pool functions#30
Merged
(fix): eliminate Core.Box allocation in @inline @with_pool functions#30
@inline @with_pool functions#30Conversation
Replace closure-based _dispatch_pool_scope with inline if/elseif/let chain to eliminate Core.Box boxing when @inline @with_pool functions are inlined into callers crossing try/finally boundaries. Allocation improvements (per @with_pool scope, fixed cost): - Julia 1.12: 32B → 0B (@inline), 0B unchanged (noinline) - Julia 1.10/1.11: 48B → 16B (@inline), 16B unchanged (noinline) Update test_zero_allocation.jl with version-dependent threshold (_ZERO_ALLOC_THRESHOLD: 0 on ≥1.12, 16 on <1.12) and function barriers for hot loop tests.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #30 +/- ##
==========================================
+ Coverage 96.54% 96.97% +0.43%
==========================================
Files 14 14
Lines 2632 2645 +13
==========================================
+ Hits 2541 2565 +24
+ Misses 91 80 -11
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Eliminates heap allocations caused by Core.Box when @with_pool-wrapped functions are inlined, by replacing a closure-based dispatch with closureless union-splitting in the macro expansion.
Changes:
- Reworked
@with_poolmacro expansion to avoid_dispatch_pool_scopeclosures and prevent boxing acrosstry/finally. - Added/adjusted allocation regression tests for
@inline @with_pooland version-dependent thresholds / function barriers. - Expanded CI matrix to include Julia 1.11 explicitly.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 7 comments.
| File | Description |
|---|---|
src/macros.jl |
Replaces closure-based pool dispatch with a closureless let + isa chain to avoid boxing/allocations. |
test/test_zero_allocation.jl |
Adds regression tests for @inline @with_pool and version-specific allocation thresholds/function barriers. |
test/test_reshape.jl |
Adds a function barrier for stable @allocated measurement. |
.github/workflows/CI.yml |
Adds Julia 1.11 to CI matrix for coverage of the affected behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Add _pool_type_for_backend trait so _wrap_with_dispatch generates
correct isa checks per backend (AdaptiveArrayPool for CPU,
CuAdaptiveArrayPool for CUDA). Removes closure-based
_dispatch_pool_scope from both paths.
Without this fix, @with_pool :cuda hit TypeError at runtime:
expected AdaptiveArrayPool{3}, got CuAdaptiveArrayPool{0}
_pool_type_for_backend returns nothing (instead of error) for unloaded backends, so _wrap_with_dispatch falls back to closure-based dispatch. Fixes LTS CI failure where @macroexpand @with_pool :cuda ran without CUDA extension loaded.
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
@inline @with_pool pool functioncaused heap allocations (32–48 bytes per scope) due toCore.Boxboxing. When the compiler inlined the_dispatch_pool_scopeclosure into outer callers crossingtry/finallyboundaries, it lost type tracking and boxed closure-captured variables.This was a regression from v0.2.2, where
AdaptiveArrayPoolwas non-parametric and didn't need union splitting.Fix
Replace the closure-based
_dispatch_pool_scope(pool -> body, getter)with a closurelesslet/if isachain directly in the macro expansion:No closure → no boxing → zero allocation.
Allocation comparison (per
@with_poolscope, fixed cost)acquire!(1 acquire = 8 acquires = same)letscope)Test changes
_ZERO_ALLOC_THRESHOLD: 0 on Julia ≥1.12, 16 on <1.12