Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support CDict & DDict for faster operation with repeated dictionary use #98

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

sekiyama58
Copy link

@sekiyama58 sekiyama58 commented Jan 24, 2025

Currently the dictionary is setup every time when it is passed to compress(dict:) / decompress(dict:), but the dictionary setup is costly process.
When the dictionary is repeatedly used, creating CDict / DDict in advance makes the compression / decompression much efficient.
This adds the Zstd::CDict & Zstd::DDict class to manage pre-loaded dictionary references.

Benchmark

CDict makes it x~13 times faster to compress spec/user_springmt.json 100 times with spec/dictionary.

                               user     system      total        real
compress with String dict  0.012185   0.001020   0.013205 (  0.013205)
compress with CDict        0.000927   0.000040   0.000967 (  0.000973)
require 'bundler/setup'
require 'zstd-ruby'
require 'benchmark'

data = IO.read("spec/user_springmt.json")
dict = IO.read("spec/dictionary")
cdict = Zstd::CDict.new(dict, 6)
ddict = Zstd::DDict.new(dict)

Benchmark.bm 24 do |x|
  x.report("compress with String dict") do
    100.times { Zstd.compress(data, dict: dict) }
  end
  x.report("compress with CDict") do
    100.times { Zstd.compress(data, dict: cdict) }
  end
end

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