Release the GVL while decompressing in Zstd.decompress - #156
Open
sribalakumar wants to merge 1 commit into
Open
Conversation
Zstd.decompress runs its decode loop by calling ZSTD_decompressStream directly, so it holds the GVL for the whole operation. Zstd.compress and both streaming paths already go through the rb_thread_call_without_gvl wrappers in common.h; decompress is the one hot path that does not, so a large decompress blocks every other thread in the process until it finishes. Route the decode loop through the existing zstd_stream_decompress wrapper with gvl=false, matching the streaming decompress path. The GVL is released and reacquired once per output chunk (ZSTD_DStreamOutSize), so other threads run between chunks. This is safe here: the scratch buffer is malloc'd (ALLOC_N), not a movable Ruby String, and the input pointer comes from a stack-pinned VALUE, so nothing the call touches can move while the GVL is released.
Author
|
@SpringMT Can you approve the pending workflow for the CI to run on this fix. |
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.
Summary
Fixes #155.
The one-shot
Zstd.decompresspath holds Ruby's GVL throughout the libzstd call, blocking every other thread in the process. This routes it through the existingzstd_stream_decompresswrapper (already used by the streaming classes), which releases the GVL once per output chunk.Change
In
ext/zstdruby/zstdruby.c, functiondecode_one_frame:The
falseargument selects the GVL-releasing branch of the wrapper. The GVL is released and reacquired once per output chunk (ZSTD_DStreamOutSize, 128 KB) — the same granularity the streaming decompress path already uses, so other threads get a turn between chunks.Why it's safe to release the GVL here
decode_one_frameismalloc'd (ALLOC_N), not a Ruby String, so GC compaction cannot move it during the released window.VALUE, which is conservatively pinned by the GC.So nothing the libzstd call touches can be relocated while the GVL is released. On very old Rubies without
ruby/thread.h, the wrapper falls back to a direct call, so behaviour there is unchanged.Verification