Add 'pread'-backed method for thread-safe hunk reading - #162
Open
xakep666 wants to merge 1 commit into
Open
Conversation
xakep666
force-pushed
the
stateless-read
branch
2 times, most recently
from
June 20, 2026 18:24
9a1137d to
160f6af
Compare
xakep666
marked this pull request as ready for review
June 20, 2026 18:52
xakep666
force-pushed
the
stateless-read
branch
4 times, most recently
from
August 28, 2026 00:09
dd4c6c4 to
4159fe7
Compare
xakep666
force-pushed
the
stateless-read
branch
from
September 1, 2026 21:34
4159fe7 to
234ce82
Compare
rtissera
added a commit
that referenced
this pull request
Sep 2, 2026
…WRAM cdzs_codec_init() built two ZSTD_DCtx, one per stream. A DCtx is ~94KB - the single largest allocation left in libchdr - so a cdzs-coded CHD spent 187KB on decompression contexts alone, most of a 320KB part's memory before anything else. Sharing one is safe. The two streams are decoded strictly in sequence by cd_codec_decompress(): base to completion, error returns early, then subcode, into disjoint halves of the same buffer, never nested and never concurrently. zstd_codec_decompress() calls ZSTD_initDStream() on entry, so nothing carries between them. And zstd_codec_init() ignores its size argument, so the two contexts were identical objects to begin with. It is not free, which is why it is conditional. Alternating two differently-shaped streams through one context rebuilds its working set each way: measured on x86-64 over a whole file, +3.9%. 94KB for 3.9% is a good trade on a memory-constrained part and a bad one on a desktop, so it is made only under LOWRAM_TARGET - the switch that exists to make exactly this choice. A default build keeps two contexts and its previous speed. Ikaruga (92.9% cdzs), peak heap under LOWRAM_TARGET=1: 251.1 -> 157.4KB. The same sharing was tried for cdzl and reverted. Once the unused 32KB miniz dictionary is gone an inflate context is only ~8KB, and it measured +2.6% for that - not worth it. Also worth recording what did not work, since the reasoning looked sound: switching the zstd codec from the streaming API to one-shot ZSTD_decompressDCtx() saves nothing at all. ZSTD_DStream is a typedef for ZSTD_DCtx, so the memory is the context itself and not streaming staging buffers; measured identical peak and marginally slower. The equivalent change for miniz worked only because miniz allocates a genuinely separate and, in libchdr's usage, entirely unused 32KB dictionary. NOTE for future threading work (see PR #162): codec state is already shared across concurrent chd_read() calls, but this removes even the accidental separation between base and subcode. Per-thread codec state has to mean per-thread codec instances. Verified: 14/14 sample files decode identically to separate contexts under LOWRAM_TARGET=1, block CRCs verified throughout; both LOWRAM_TARGET=1 and =0 build and run; cdzl speed confirmed back at its baseline after the revert. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KMYbZzB8mioFmotWGFnAXG
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.
Addressing proposal in #34
This PR adds method
chd_read_threadsafe- a thread-safe version ofchd_read. This method will be helpful for implementing threaded read-aheads purely by consumers without adding external dependencies (likepthread) .My main programming language is Go and there's a popular approach "leave concurrency to a caller" used when concurrent code is written. And I think it fits good in this case.
Some implementation details:
chd_read_threadsafemethod has extra argumentcompressed_bufferwhich is used to store raw compressed data read from file. This is the one of two things required for thread-safety.chd_readuses same buffer stored inchd_filestructure.fpreadadded tochd_core_file_callbacksto allow thread-safe reading from file. Used only inchd_read_threadsafe.fpreadwas not providedchd_read_threadsafewill return errorCHDERR_NOT_SUPPORTED.hunk_read_into_memoryhas been tweaked a bit to accept arbitrary compressed data buffer pointer and different reading methods:seek_and_read(old one) orread_at(new one).