Skip to content

perf: avoid rebuilding untouched nodes during expression rewrites - #9445

Merged
robert3005 merged 2 commits into
developfrom
rk/expr-rewrite-fewer-clones
Aug 18, 2026
Merged

perf: avoid rebuilding untouched nodes during expression rewrites#9445
robert3005 merged 2 commits into
developfrom
rk/expr-rewrite-fewer-clones

Conversation

@robert3005

Copy link
Copy Markdown
Contributor

Defer cloning children until one of them actually changes

Signed-off-by: Robert Kruszewski robert@spiraldb.com

@robert3005 robert3005 added the changelog/performance A performance improvement label Aug 17, 2026
@codspeed-hq

codspeed-hq Bot commented Aug 17, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.07%

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

❌ 6 regressed benchmarks
✅ 1994 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation compact[(4096, 90)] 1.6 µs 1.9 µs -13.97%
Simulation search_index_in_range_chunked 5.1 ms 5.9 ms -13.52%
Simulation compact[(16384, 90)] 1.8 µs 2 µs -13.01%
WallTime words_gather_dispatch[1024] 8 ns 9 ns -11.11%
Simulation compact_sliced[(4096, 90)] 1.7 µs 1.9 µs -10.51%
Simulation slice_primitive_tight_loop[10000] 422.3 µs 470.5 µs -10.25%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing rk/expr-rewrite-fewer-clones (1d30b69) with develop (b825c4f)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Defer cloning children until one of them actually changes

Signed-off-by: Robert Kruszewski <robert@spiraldb.com>
@robert3005
robert3005 force-pushed the rk/expr-rewrite-fewer-clones branch from c22e433 to b982a5b Compare August 17, 2026 17:45
I, Robert Kruszewski <github@robertk.io>, hereby add my Signed-off-by to this commit: b982a5b

Signed-off-by: Robert Kruszewski <github@robertk.io>
Comment on lines +160 to +161
// cheaply check dtype equality before rebuilding the scalar
if children.len() == old_children.len()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a comment from codex:

This optimization assumes every BoundExpression::Scalar was created through try_new, but the public enum does not enforce that invariant. Should we make construction opaque before relying on it here, or retain the validation in with_children?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are bigger problems if someone does this... not sure we can effectively restrict it though

@connortsui20 connortsui20 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, this is clever.

Seems fine to me with the exception of that optimization (see comment from codex), but IIUC that is an existing issue because the field is public and thus there are no invariants

@robert3005
robert3005 merged commit 3805421 into develop Aug 18, 2026
87 of 88 checks passed
@robert3005
robert3005 deleted the rk/expr-rewrite-fewer-clones branch August 18, 2026 18:05
@connortsui20

connortsui20 commented Aug 18, 2026

Copy link
Copy Markdown
Member

oh whoops auto merge was on.

I think its fine @robert3005

robert3005 added a commit that referenced this pull request Aug 19, 2026
…n and delegate to return_dtype (#9479)

While #9445 made some small optimisations to the expression traversal it also
have done a silly thing that made wide expressions more expensive to visit

Signed-off-by: Robert Kruszewski <github@robertk.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/performance A performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants