Skip to content

Skip the temporary when assigning value-only generators - #2944

Open
rithikasridharbabu-2007 wants to merge 1 commit into
xtensor-stack:masterfrom
rithikasridharbabu-2007:contribution/independent-generator-assignment
Open

rithikasridharbabu-2007 wants to merge 1 commit into
xtensor-stack:masterfrom
rithikasridharbabu-2007:contribution/independent-generator-assignment

Conversation

@rithikasridharbabu-2007

@rithikasridharbabu-2007 rithikasridharbabu-2007 commented Oct 23, 2026 •

Copy link
Copy Markdown

Checklist

  • The title and commit message(s) are descriptive.
  • Small commits made to fix your PR have been squashed to avoid history pollution.
  • Tests have been added for new features or bug fixes.
  • API of new functions and classes are documented.

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_traits for xgenerator always reports an overlap, so assigning arange, linspace, eye or 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 to std::false_type. Unknown functors and functors holding expressions (concatenate, stack, diagonal, ...) keep the overlap protection.
  • xgenerator's check_overlap returns false only when the functor opts in.
  • Opted in: arange_generator with arithmetic types (used by arange and linspace), eye_fn, and random_impl. fn_impl<F> forwards to F.

No change to xsemantic.hpp or the assigner. XTENSOR_FORCE_TEMPORARY_MEMORY_IN_ASSIGNMENTS behaves as before.

Tests

New cases in test_xassign.cpp: the trait values for each builder, zero data-buffer allocations for same-size assignment of arange / linspace / rand / eye (counting allocator; fails without the change), resizing, assignment to views, and that a reading functor, self-concatenate and diagonal of 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_view does not compile in this mode on unmodified master either (xtl::xmasked_value reference binding, xtl 0.8.0).
  • Not run locally: GCC, MSVC, Linux, sanitizers, and the JSON tests (nlohmann_json not installed). Relying on CI for those.

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:

Assignment master this PR
container = arange / linspace / rand / eye same same
column view = rand 4.50 ms 3.84 ms
2D view = eye 4.10 ms 2.26 ms
row view = arange 0.26 ms 0.39 ms
column view = arange 0.80 ms 1.82 ms

Assigning arange to a view is slower without the temporary: filling a contiguous temporary through arange's assign_to and 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.

@rithikasridharbabu-2007

Copy link
Copy Markdown
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 rithikasridharbabu-2007 changed the title Avoid temporary arrays for independent numeric generators Skip the temporary when assigning value-only generators Nov 14, 2026
@rithikasridharbabu-2007
rithikasridharbabu-2007 marked this pull request as ready for review November 14, 2026 06:10
@rithikasridharbabu-2007
rithikasridharbabu-2007 marked this pull request as draft November 14, 2026 06:11
@rithikasridharbabu-2007
rithikasridharbabu-2007 force-pushed the contribution/independent-generator-assignment branch from 390d6bb to 9668130 Compare November 14, 2026 22:27
@rithikasridharbabu-2007
rithikasridharbabu-2007 marked this pull request as ready for review November 14, 2026 22:28
@rithikasridharbabu-2007

Copy link
Copy Markdown
Author

Rebased on master after #2964 and reworked: the trait now lives on the generator functor and is used in xgenerator's overlapping_memory_checker_traits, as suggested in #2943, instead of patching xsemantic.hpp. The description has the details, including one case (arange assigned to a view) that is slower without the temporary. Could you approve the CI run when you have a moment? Thanks.

This branch has not been deployed

No deployments
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