Repository navigation
Skip the temporary when assigning value-only generators - #2944
Open
rithikasridharbabu-2007 wants to merge 1 commit into
Open
rithikasridharbabu-2007 wants to merge 1 commit into
rithikasridharbabu-2007 wants to merge 1 commit into
Conversation
Author
|
Hi! Just checking whether this direction looks reasonable to you. If it would help review, I can add a small benchmark showing the allocation savings, or adjust the approach. Thanks for maintaining xtensor! |
Since xtensor-stack#2936 every xgenerator reports a possible memory overlap, so assigning arange, linspace, eye or a random expression allocates a full-size temporary even though these never read array memory. Add an is_memory_independent_generator trait on generator functors and use it in xgenerator's overlapping_memory_checker_traits. It defaults to false, so functors that hold expressions (concatenate, stack, diagonal, ...) and unknown functors keep the overlap protection. Opt in arange_generator with arithmetic types, eye_fn and random_impl; fn_impl forwards to the functor it wraps. Related to xtensor-stack#2943. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
rithikasridharbabu-2007
marked this pull request as ready for review
November 14, 2026 06:10
rithikasridharbabu-2007
marked this pull request as draft
November 14, 2026 06:11
rithikasridharbabu-2007
force-pushed
the
contribution/independent-generator-assignment
branch
from
November 14, 2026 22:27
390d6bb to
9668130
Compare
rithikasridharbabu-2007
marked this pull request as ready for review
November 14, 2026 22:28
Author
|
Rebased on master after #2964 and reworked: the trait now lives on the generator functor and is used in |
This branch has not been deployed
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.
Checklist
Description
Related to #2943. Rebased on master after #2964 and re-implemented; the earlier version of this PR patched
xcontainer_semantic::operator=, which no longer applies.Since #2936,
overlapping_memory_checker_traitsforxgeneratoralways reports an overlap, so assigningarange,linspace,eyeor a random expression allocates a full-size temporary even though these never read array memory.This adds the trait suggested in the issue, on the generator functor:
is_memory_independent_generator<F>defaults tostd::false_type. Unknown functors and functors holding expressions (concatenate,stack,diagonal, ...) keep the overlap protection.xgenerator'scheck_overlapreturnsfalseonly when the functor opts in.arange_generatorwith arithmetic types (used byarangeandlinspace),eye_fn, andrandom_impl.fn_impl<F>forwards toF.No change to
xsemantic.hppor the assigner.XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTSbehaves as before.Tests
New cases in
test_xassign.cpp: the trait values for each builder, zero data-buffer allocations for same-size assignment ofarange/linspace/rand/eye(counting allocator; fails without the change), resizing, assignment to views, and that a reading functor, self-concatenateanddiagonalof the destination still go through a temporary.Run locally on macOS arm64, AppleClang, C++20:
XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS=ON(default): 77/77 test executables pass.=OFF: 76/77 pass.test_xmasked_viewdoes not compile in this mode on unmodified master either (xtl::xmasked_valuereference binding, xtl 0.8.0).Performance: one case gets slower
The gain is the avoided allocation, not speed. Best of 15 runs, 1,048,576 doubles,
-O3 -DNDEBUG, overlap check enabled:arange/linspace/rand/eyerandeyearangearangeAssigning
arangeto a view is slower without the temporary: filling a contiguous temporary througharange'sassign_toand copying it beats stepping the generator element by element. If you would rather keep the temporary for view destinations, or restrict the opt-in differently, I am happy to change it.