Skip to content

Add 'pread'-backed method for thread-safe hunk reading - #162

Open
xakep666 wants to merge 1 commit into
rtissera:masterfrom
xakep666:stateless-read
Open

Add 'pread'-backed method for thread-safe hunk reading#162
xakep666 wants to merge 1 commit into
rtissera:masterfrom
xakep666:stateless-read

Conversation

@xakep666

@xakep666 xakep666 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Addressing proposal in #34

This PR adds method chd_read_threadsafe - a thread-safe version of chd_read. This method will be helpful for implementing threaded read-aheads purely by consumers without adding external dependencies (like pthread) .

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_threadsafe method has extra argument compressed_buffer which is used to store raw compressed data read from file. This is the one of two things required for thread-safety. chd_read uses same buffer stored in chd_file structure.
  • Extra function pointer (interface method) fpread added to chd_core_file_callbacks to allow thread-safe reading from file. Used only in chd_read_threadsafe.
  • On *nix libc's pread is being utilized for thread-safe reading from file
  • On Windows ReadFile used for same purpose. Reference: https://github.com/aleitner/windows_pread
  • On other platforms or in case fpread was not provided chd_read_threadsafe will return error CHDERR_NOT_SUPPORTED.
  • hunk_read_into_memory has been tweaked a bit to accept arbitrary compressed data buffer pointer and different reading methods: seek_and_read (old one) or read_at (new one).

@xakep666
xakep666 force-pushed the stateless-read branch 2 times, most recently from 9a1137d to 160f6af Compare June 20, 2026 18:24
@xakep666
xakep666 marked this pull request as ready for review June 20, 2026 18:52
@xakep666
xakep666 force-pushed the stateless-read branch 4 times, most recently from dd4c6c4 to 4159fe7 Compare August 28, 2026 00:09
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
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.

1 participant