Improve zstd performance & fix KTOR-9487#5519
Improve zstd performance & fix KTOR-9487#5519solonovamax wants to merge 3 commits intoktorio:mainfrom
Conversation
This should improve zstd encoding & decoding performance because it avoids allocating byte arrays on the heap, instead relying on the byte buffers. This also fixes KTOR-9487 by just using the streaming api like it should have from the start. Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughSwitched Zstd encode/decode helpers to use a direct ByteBuffer pool and replaced manual frame-size probing with direct streaming compression/decompression using pooled direct ByteBuffers; added a default direct ByteBuffer pool and adjusted a test to exercise smaller direct buffers. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@ktor-shared/ktor-encoding-zstd/jvm/src/io/ktor/encoding/zstd/Zstd.jvm.kt`:
- Around line 103-112: The fixed 4098-byte outputBuf can overflow for
incompressible input when calling ctx.compress(outputBuf, inputBuf) and the
return code is ignored; change the logic in the loop around
ctx.compress/outputBuf/inputBuf to allocate or ensure outputBuf.capacity() >=
Zstd.compressBound(inputBuf.remaining()) (or switch to the streaming compressor
API), call ctx.compress and check its return value for errors (handle negative
error codes by throwing/logging or reallocating a larger outputBuf), and then
proceed to flip and writeFully only after a successful compress.
In `@ktor-utils/jvm/src/io/ktor/util/cio/ByteBufferPool.kt`:
- Around line 20-25: The new public JVM symbol KtorDefaultDirectPool (and its
use of DirectByteBufferPool/DEFAULT_KTOR_POOL_SIZE/DEFAULT_BUFFER_SIZE) is
missing from the ABI signature files; run the Gradle task to update the legacy
ABI so the new symbol is recorded by executing ./gradlew
:ktor-utils:updateLegacyAbi (or the project’s ABI update task), then commit the
modified ktor-utils/api/ktor-utils.api so KtorDefaultDirectPool is included in
the published ABI signatures.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: a9ad9c2c-077d-4fb0-9a01-9ccac2de817b
📒 Files selected for processing (3)
ktor-shared/ktor-encoding-zstd/jvm/src/io/ktor/encoding/zstd/Zstd.jvm.ktktor-shared/ktor-encoding-zstd/jvm/test/io/ktor/encoding/zstd/ZstdTest.ktktor-utils/jvm/src/io/ktor/util/cio/ByteBufferPool.kt
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
Signed-off-by: solonovamax <solonovamax@12oclockpoint.com>
Subsystem
shared/ktor-encoding-zstd
Motivation
I didn't like the way it did things so I took a deeper look at it and cleaned it up.
in the process I also discovered KTOR-9487.
Solution
This should improve zstd encoding & decoding performance because it avoids allocating byte arrays on the heap, instead relying on the byte buffers.
Also fixes KTOR-9487 by just using the streaming api like it should have from the start.
I also fixed the test which wasn't actually correctly testing what it said it was testing.
I had to add a new byte buffer pool because the zstd encoder needs direct byte buffers.