Skip to content

Add reusable context for performance - #113

Open
kreynolds wants to merge 2 commits into
SpringMT:mainfrom
kreynolds:kreynolds/reusable-context
Open

Add reusable context for performance#113
kreynolds wants to merge 2 commits into
SpringMT:mainfrom
kreynolds:kreynolds/reusable-context

Conversation

@kreynolds

@kreynolds kreynolds commented Aug 31, 2025

Copy link
Copy Markdown

Pull Request Summary: Using the Context Feature in zstd-ruby

The zstd-ruby gem provides a Zstd::Context class for efficient, reusable compression and decompression, offering 2-3x faster performance and memory savings compared to module methods. Here's how to use it:

  1. Initialize a Context:

    ctx = Zstd::Context.new(level: 6) # Optional: set compression level (default 6)
    ctx = Zstd::Context.new(level: 6, dict: dictionary) # With dictionary for better compression
  2. Compress and Decompress:

    compressed = ctx.compress(data) # Compress data
    original = ctx.decompress(compressed) # Decompress data
  3. Specialized Contexts (for memory optimization):

    • Compression-only: cctx = Zstd::CContext.new(level: 6); cctx.compress(data)
    • Decompression-only: dctx = Zstd::DContext.new; dctx.decompress(compressed)
  4. Benefits:

    • Performance: 2-3x faster for repeated operations.
    • Memory Efficiency: Reuses context, with specialized CContext/DContext saving ~50% memory.
    • Thread-Safe: Each context instance is independent.
  5. Use Case: Ideal for multiple compression/decompression tasks or dictionary-based workflows.

Sample output from benchmarks/context_reuse.rb showing 3.5x speed improvement:

Comparison:
CContext compress (reused):   719483.0 i/s
Context compress (reused):   710765.0 i/s - 1.01x  (± 0.00) slower
DContext decompress (reused):   210555.5 i/s - 3.42x  (± 0.00) slower
Context decompress (reused):   209596.5 i/s - 3.43x  (± 0.00) slower
Module decompress (new context each time):   204158.0 i/s - 3.52x  (± 0.00) slower
Module compress (new context each time):   201053.2 i/s - 3.58x  (± 0.00) slower
@kreynolds

Copy link
Copy Markdown
Author

Accidentally made this against an outdated main, will update shortly.

@kreynolds
kreynolds force-pushed the kreynolds/reusable-context branch from 911660a to 141fd7f Compare September 1, 2025 13:34
@kreynolds
kreynolds force-pushed the kreynolds/reusable-context branch from 141fd7f to 8b40eb1 Compare September 1, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant