Repository navigation
Compress SChunk writes through c-blosc2's *_buffer functions (no new buffer per chunk for frames) - #765
Merged
Conversation
FabianIsensee
added a commit
to FabianIsensee/python-blosc2
that referenced
this pull request
Nov 5, 2026
update_data(), append_data(), insert_data(), SChunk(data=...) and
set_slice() compressed every chunk into a new malloc(nbytes + 32), shrank
it and handed it to c-blosc2. They now call
blosc2_schunk_{append,insert,update}_buffer() through one helper,
_store_buffer(), so frames are compressed into a buffer that the thread
reuses and written without a copy. The copy argument of update_data()
and insert_data() has no effect any more and is documented as such.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
FrancescAlted
force-pushed
the
write-buffer-reuse
branch
from
November 6, 2026 07:57
ddad0b2 to
1219bf9
Compare
Member
|
My agent struggled a bit when merging this one. Please give the final merged version another look. Thanks! |
Contributor
Author
|
Thanks for merging! I checked the squashed commit against my branch. The conflicts with #766 are resolved as intended: |
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.
Hi Francesc,
Depends on: Blosc/c-blosc2#850 (
blosc2_schunk_update_buffer()/blosc2_schunk_insert_buffer()). The twoTEMPcommits bundle that branch until a c-blosc2 release has it; then they become a version pin (andBLOSC2_MIN_VERSIONgoes up if 3.5.1 ships without it).Please check before merging: with
set_releasegil(True), two handles of one file written withoutlocking=Truecan now corrupt it. Details in the section below; if you'd rather keep the old behaviour, there is an alternative.SChunk.update_data(),append_data(),insert_data(),SChunk(data=...)andset_slice()compress every chunk into a newmalloc(nbytes + 32), shrink it, and hand it to c-blosc2.blosc2.asarray()above 16 MB, lazy-expressioncompute(),_copy_chunkwise()and linalg go throughupdate_data(). With glibc each of those buffers is a freshmmap(), faulted in while the compression threads write into it.In 4.14.1 this was mostly hidden after reads. The read path then freed a chunk-sized scratch per chunk, which raised glibc's mmap threshold, so later compressions used warm heap memory. The slice reads no longer use that scratch (c-blosc2 1aae9481), so on main, compressing to a file right after reads is up to 1.4x slower than in 4.14.1 at 28 threads. In a fresh process main is no slower than 4.14.1.
With this PR these five methods call c-blosc2's
blosc2_schunk_{append,insert,update}_buffer()through one helper,_store_buffer(). Frames are then compressed into a buffer that the thread reuses and are written without a copy. Thecopyargument ofupdate_data()/insert_data()has no effect any more and is documented as such: the compressed chunk never leaves c-blosc2.set_releasegil(True)and several writers on one file: please decideWith
set_releasegil(True)the GIL used to be released only around the compression, and the frame write ran with the GIL held. Now it is released around the whole call, so the frame write runs without the GIL too (it runs no Python).The consequence: two handles of the same file, opened without
locking=Trueand written from two threads at the same time, can now corrupt the file. In a review we ran two threads appending 40 chunks each through two such handles, 8 times per build: main kept all 81 chunks every time, because the GIL happened to serialise the writes; this branch failed every run, and in 3 of the 8 the file could no longer be opened. Withlocking=True, both are fine. The sharing guide already says that this case is not supported: "Two writers mutating the same container without locking is not supported and can corrupt it." That is why I left it like this.If you would rather keep the old behaviour, the alternative is to hold the GIL for the whole call when writing to an on-disk frame opened without locking. The price: threads that write different files with
set_releasegil(True)would no longer compress in parallel. Tell me which you prefer and I will change it.Other changes in behaviour
BLOSC2_MAX_BUFFERSIZEraiseValueError; before, their size was silently cut to 32 bits.set_slice()with too little data, andupdate_data()with an invalid argument, used to leak it.RuntimeErrormessages of these methods are reworded.Benchmarks
Ryzen 9 7950X, 28 threads, float32 array of shape (64, 512, 512) (67 MB, default ZSTD 5 + SHUFFLE, 8 chunks of 8 MiB). Each configuration ran in a fresh process; the numbers are the median of 15 calls over 3 alternated rounds. "After reads" means the process read an array before.
append_data()x 8, on diskSChunk(data=...), on diskSChunk(data=...), contiguous in memoryblosc2.asarray(), on diskblosc2.asarray(), contiguous in memory(a + 1).compute(urlpath=...), after readsschunk[:] = datawithin the super-chunk goes through c-blosc2'sblosc2_schunk_set_slice_buffer(), which the C PR changes as well: 11.3 -> 9.0 ms on disk, 10.1 -> 7.3 ms contiguous in memory.Plain in-memory arrays (the default) are unchanged: the super-chunk keeps each chunk, so it gets its own allocation as before. In 4.14.1 they compress faster after reads only when glibc happens to keep its heap: identical runs gave either 6.4 or 15 ms for the 8 appends. Copying the chunk out of a reused buffer into an exact-size allocation would make that reliable for
append_data(), but it madeblosc2.asarray()andcompute()in memory 12-44% slower, so it is not in this PR.Tests
tests/test_schunk_data_writes.py(27 tests), on the 4 storages:set_releasegil(True)on main; a fix for that comes in a separate PR);SChunk(data=...)plus aset_slice()that grows the last chunk and appends, with the GIL held and released;locking;Full suite: 10661 passed; the 2 b2view HTTP tests fail without aiohttp, as on main. Fork CI: Linux x86 (Python 3.12, 3.14, NumPy 1.26), Linux aarch64, Windows and WASM pass; macOS did not get a runner on the fork for the last push (it passed on the first one).
AI disclosure: I used Claude Code (Anthropic) for the code, the tests and a first draft of this description, and Claude Code opened this PR on my behalf. I reviewed and tested everything.
Best,
Fabian
🤖 Generated with Claude Code