Skip to content

estimate_chunksize without allocations - #332

Merged
meggart merged 2 commits into
JuliaIO:mainfrom
bjarthur:bja/estimate-chunksize
Oct 7, 2026
Merged

meggart merged 2 commits into
JuliaIO:mainfrom
bjarthur:bja/estimate-chunksize

Conversation

@bjarthur

@bjarthur bjarthur commented Oct 6, 2026

Copy link
Copy Markdown
Member

estimate_chunksize is what eachchunk returns for any array that is not a DiskArray, so it runs for every view taken of an in-memory array by packages that track chunks; YAXArrays rebuilds its chunk grid on every getindex, and it was the largest remaining cost in JuliaDataCubes/YAXArrays.jl#361 after the YAXArrays-side fixes.

It collected the size tuple into a Vector for cumprod, called ntuple with a runtime length (so the closure was not specialised), and computed a clamp whose result was never used: 292 ns and 10 allocations for a 3-d array. The same arithmetic on tuples, with the dimension count known at compile time, gives the identical GridChunks in 9 ns with no allocations:

julia> @btime DiskArrays.estimate_chunksize((3, 20, 5), 8)   # 0.4.24
  292.000 ns (10 allocations: 592 bytes)

julia> @btime DiskArrays.estimate_chunksize((3, 20, 5), 8)   # this PR
  9.000 ns (0 allocations: 0 bytes)

Tests: the existing estimate_chunksize cases are unchanged; added a 3-d case where the element limit falls inside the middle dimension, and assertions that estimate_chunksize and eachchunk of a view allocate nothing.

🤖 Generated with Claude Code

estimate_chunksize is what eachchunk returns for any array that is not a
DiskArray, so it runs for every view taken of an in-memory array by packages
that track chunks (YAXArrays rebuilds its chunk grid on every getindex). It
collected the size tuple into a Vector for cumprod, called ntuple with a
runtime length, and computed a clamp it never used: 292 ns and 10 allocations
for a 3-d array. Doing the same arithmetic on tuples with the dimension count
known at compile time gives the identical GridChunks in 9 ns with no
allocations. Tests cover a 3-d case where the limit falls inside the middle
dimension and assert zero allocations.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@meggart
meggart merged commit d6f18bf into JuliaIO:main Oct 7, 2026
12 checks passed
@bjarthur
bjarthur deleted the bja/estimate-chunksize branch October 21, 2026 12:15
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