Improve Zstd.decompress performance by avoiding unnecessary memory copy#127
Merged
SpringMT merged 1 commit intoSpringMT:mainfrom Dec 26, 2025
Merged
Improve Zstd.decompress performance by avoiding unnecessary memory copy#127SpringMT merged 1 commit intoSpringMT:mainfrom
SpringMT merged 1 commit intoSpringMT:mainfrom
Conversation
Currently, `rb_decompress` allocates a temporary buffer and copies the input string using `ALLOC_N` and `memcpy`. This overhead is significant, especially for large data, and doubles the memory usage during decompression. ## benchmark ```ruby require 'bundler/inline' gemfile do source 'https://rubygems.org' gem 'zstd-ruby' gem 'benchmark-ips' end require 'securerandom' large_data = SecureRandom.random_bytes(1024 * 1024 * 100) compressed_data = Zstd.compress(large_data) Benchmark.ips do |x| x.time = 15 x.report("zstd decompress") do Zstd.decompress(compressed_data) end end ``` ## before ``` $ ruby zstd.rb ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [x86_64-linux] Warming up -------------------------------------- zstd decompress 1.000 i/100ms Calculating ------------------------------------- zstd decompress 11.148 (± 9.0%) i/s (89.70 ms/i) - 167.000 in 15.085672s ``` ## after ``` $ ruby zstd.rb ruby 3.4.8 (2025-12-17 revision 995b59f666) +PRISM [x86_64-linux] Warming up -------------------------------------- zstd decompress 2.000 i/100ms Calculating ------------------------------------- zstd decompress 20.890 (± 9.6%) i/s (47.87 ms/i) - 312.000 in 15.015814s ```
Owner
|
Thank you! |
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.
Currently,
rb_decompressallocates a temporary buffer and copies the input string usingALLOC_Nandmemcpy. This overhead is significant, especially for large data, and doubles the memory usage during decompression.benchmark
before
after