Skip to content

performance optimizations - #218

Merged
aplavin merged 2 commits into
JuliaObjects:masterfrom
aplavin:optim
Dec 2, 2025
Merged

aplavin merged 2 commits into
JuliaObjects:masterfrom
aplavin:optim

Conversation

@aplavin

@aplavin aplavin commented Dec 1, 2025 •

Copy link
Copy Markdown
Member

Accessors are known to be hard for the compiler to optimize sometimes, with compiler heuristics to stop optimizing triggering quite early.
Here, I make the compiler "try harder": things like avoiding argument splatting, and making small inner functions more optimizable.

I recently used Accessors getall/setall with a few tens of elements – up to ~50. With these optimizations, it compiles down to almost nothing, while before there was a lot of dynamic dispatch inside.
Tests pass both before and after. Maybe I'll think of a short self-contained test demonstrating the improvements and add it... But I confirm in my testing that these changes greatly improve performance in such a regime.

Comment thread src/getsetall.jl
Comment on lines +137 to +138
@generated _concat(a::NTuple{N,Any}, b::NTuple{M,Any}) where {N,M} =
:(Base.Cartesian.@ntuple $(N + M) i -> i ≤ $N ? a[i] : b[i - $N])

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.

There is no reliably fast way to do this in Base?

@aplavin aplavin Dec 2, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I tried:

  • map
  • broadcast
  • ntuple the function

All of these suffer from catastrophic performance drop at some point. Seems like it's around 32 elements returned by getall in total, the unrolling threshold for tuples in Base.

Added a comment.

@aplavin

aplavin commented Dec 2, 2025

Copy link
Copy Markdown
Member Author

Ideally, Accessors should work and be (almost) zero cost in all regimes – from heterogeneous tuples at small sizes, to homogeneous abstractvectors after some threshold. Unfortunately, the seamless transition is notoriously difficult, don't think we ever seriously attempted it.
What we can do cheaply (this PR) is to expand the "tuples" regime to larger sizes – there are still fundamental limits, but multiple tens of elements works great now.

@aplavin
aplavin merged commit 4603471 into JuliaObjects:master Dec 2, 2025
11 checks passed
@aplavin
aplavin deleted the optim branch March 2, 2026 21:51
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