Skip to content

feat: implement ZSTD compression pooling and integrate across project#865

Merged
kalbasit merged 1 commit intomainfrom
02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project
Feb 12, 2026
Merged

feat: implement ZSTD compression pooling and integrate across project#865
kalbasit merged 1 commit intomainfrom
02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project

Conversation

@kalbasit
Copy link
Owner

This change introduces a new pkg/compress package that provides a pool
of ZSTD encoders and decoders. This optimization reduces allocation
overhead during high-concurrency compression and decompression
operations.

The implementation:

  • Uses sync.Pool for managing zstd.Encoder and zstd.Decoder instances.
  • Provides PooledZstdWriter and PooledZstdReader wrappers for easy
    resource management.
  • Integrates the pool into the local chunk store for chunk compression.
  • Integrates the pool into the server middleware for HTTP response
    compression.
  • Updates NAR reading and test utilities to use the centralized
    compression package.

This was needed to improve the efficiency of the cache server when
handling many small and large NAR files simultaneously, minimizing
garbage collection pressure.

@dosubot dosubot bot added the size:XXL This PR changes 1000+ lines, ignoring generated files. label Feb 12, 2026
@dosubot dosubot bot added enhancement New feature or request go Pull requests that update go code labels Feb 12, 2026
@dosubot
Copy link

dosubot bot commented Feb 12, 2026

Related Documentation

Checked 7 published document(s) in 1 knowledge base(s). No updates required.

How did I do? Any feedback?  Join Discord

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @kalbasit, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a fundamental optimization for ZSTD compression and decompression operations across the project. By centralizing and pooling ZSTD encoder and decoder instances, the system can now handle high-throughput scenarios with reduced memory allocations and lower garbage collection overhead. This change aims to enhance the overall efficiency and scalability of the cache server, particularly when dealing with numerous NAR files concurrently.

Highlights

  • New ZSTD Compression Pooling Package: Introduced a new pkg/compress package that provides a centralized, thread-safe sync.Pool-based mechanism for recycling ZSTD encoder and decoder instances. This significantly reduces allocation overhead and garbage collection pressure during high-concurrency compression/decompression operations.
  • Integration Across Core Components: The newly implemented ZSTD pooling has been integrated into critical parts of the project, including the local chunk store for NAR compression, the server middleware for HTTP response compression, and various NAR reading and test utilities, ensuring consistent performance improvements.
  • Improved Resource Management: The pooling mechanism offers both low-level (manual Get/Put) and high-level (wrapped PooledZstdWriter/PooledZstdReader with automatic Close handling) APIs, simplifying resource management and promoting cleaner code while ensuring instances are properly reset before reuse.
  • Comprehensive Documentation and Migration Guide: Detailed documentation (README_ZSTD.md, ZSTD_POOL.md, ZSTD_QUICK_REF.md) and a migration guide (MIGRATION.md) have been added to facilitate understanding and adoption of the new compression pooling system, including examples and a migration checklist.
Changelog
  • pkg/cache/cache_internal_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated ZSTD writer creation in testRunLRU to use compress.GetZstdWriter() and compress.PutZstdWriter().
  • pkg/cache/cache_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated ZSTD reader creation in testGetNarInfo to use compress.GetZstdReader() and compress.PutZstdReader().
  • pkg/cache/export_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated compressZstd function to use compress.NewPooledZstdWriter() for ZSTD compression.
  • pkg/compress/MIGRATION.md
    • Added a new markdown file detailing migration examples for various packages to use the new ZSTD pool, including before/after code snippets and a migration checklist.
  • pkg/compress/README_ZSTD.md
    • Added a new markdown file providing an overview and summary of the ZSTD pool implementation, its key features, usage examples, and integration points.
  • pkg/compress/ZSTD_POOL.md
    • Added a new markdown file offering complete API documentation for the ZSTD pool, covering low-level and high-level APIs, usage examples, configuration, and best practices.
  • pkg/compress/ZSTD_QUICK_REF.md
    • Added a new markdown file providing a quick reference guide for the ZSTD pool, including common patterns, an API cheat sheet, and best practices.
  • pkg/compress/zstd.go
    • Added a new Go file implementing zstdWriterPool and zstdReaderPool using sync.Pool.
    • Provided GetZstdWriter(), PutZstdWriter(), GetZstdReader(), and PutZstdReader() functions for manual pool management.
    • Introduced PooledZstdWriter and PooledZstdReader structs with NewPooledZstdWriter() and NewPooledZstdReader() constructors for automatic pool management and resource cleanup.
  • pkg/compress/zstd_test.go
    • Added a new Go test file with comprehensive tests for the ZSTD pooling functionality, covering get/put operations, pooled wrapper behavior, round-trip compression/decompression, error handling, and resource reuse.
  • pkg/nar/reader.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated DecompressReader function to use compress.NewPooledZstdReader() for ZSTD decompression.
  • pkg/nar/reader_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated ZSTD writer creation in TestDecompressReader to use compress.NewPooledZstdWriter().
  • pkg/server/server.go
    • Removed sync and github.com/klauspost/compress/zstd imports.
    • Removed the local zstdWriterPool global variable.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated getNar handler to use compress.NewPooledZstdWriter() for HTTP response compression.
  • pkg/server/server_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated ZSTD reader creation in TestGetNar_ZstdCompression to use compress.NewPooledZstdReader().
  • pkg/storage/chunk/local.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Refactored localReadCloser to embed *compress.PooledZstdReader instead of *zstd.Decoder.
    • Removed encoder and decoder fields from localStore struct.
    • Updated NewLocalStore to no longer initialize ZSTD encoder/decoder instances directly.
    • Modified GetChunk to use compress.NewPooledZstdReader() for decompression.
    • Modified PutChunk to use compress.GetZstdWriter() and compress.PutZstdWriter() for compression.
  • pkg/storage/chunk/s3.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Refactored s3ReadCloser to embed *compress.PooledZstdReader instead of *zstd.Decoder.
    • Removed encoder and decoder fields from s3Store struct.
    • Updated NewS3Store to no longer initialize ZSTD encoder/decoder instances directly.
    • Modified GetChunk to use compress.NewPooledZstdReader() for decompression.
    • Modified PutChunk to use compress.GetZstdWriter() and compress.PutZstdWriter() for compression.
  • testdata/server.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated compressMiddleware to use compress.NewPooledZstdWriter() for response compression.
  • testdata/server_test.go
    • Removed direct import of github.com/klauspost/compress/zstd.
    • Imported github.com/kalbasit/ncps/pkg/compress.
    • Updated ZSTD reader creation in TestNewTestServerWithZSTD to use compress.GetZstdReader() and compress.PutZstdReader().
Activity
  • No human activity has been recorded on this pull request yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from 7b8f402 to 39c40d5 Compare February 12, 2026 06:09
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a centralized ZSTD compression pool to improve performance by reducing allocation overhead, which is a great optimization. The implementation is well-structured with both low-level and high-level APIs for managing pooled encoders and decoders. The new pkg/compress is cleanly integrated across various parts of the project, including storage backends, server middleware, and test utilities, replacing previous ad-hoc compression logic. My main feedback concerns the new documentation files, which consistently reference an incorrect package path (pkg/helper instead of pkg/compress). The provided comments highlight these issues and suggest corrections, which are all valid and align with the project's standards.

@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch from 86f0d2b to a87eda2 Compare February 12, 2026 06:15
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch 2 times, most recently from 3361935 to 4cb376c Compare February 12, 2026 06:37
@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch 2 times, most recently from 1c56a4c to c058ccc Compare February 12, 2026 06:56
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch 2 times, most recently from a590296 to c60afc2 Compare February 12, 2026 07:22
@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch from c058ccc to 6ca79e5 Compare February 12, 2026 07:22
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from c60afc2 to ccca89c Compare February 12, 2026 07:39
@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch from 6ca79e5 to 8aa33ef Compare February 12, 2026 07:39
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch 3 times, most recently from c948891 to 8879a08 Compare February 12, 2026 08:32
@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch from 77b75c4 to 59a0c62 Compare February 12, 2026 08:32
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from 8879a08 to a731e95 Compare February 12, 2026 08:43
@kalbasit kalbasit force-pushed the 02-11-test_add_e2e_cdc_compression_test branch from 59a0c62 to a6f7400 Compare February 12, 2026 08:43
Base automatically changed from 02-11-test_add_e2e_cdc_compression_test to main February 12, 2026 08:54
@dosubot dosubot bot added size:XL This PR changes 500-999 lines, ignoring generated files. and removed size:XXL This PR changes 1000+ lines, ignoring generated files. labels Feb 12, 2026
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from a731e95 to 224fe6d Compare February 12, 2026 08:55
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Feb 12, 2026
@kalbasit
Copy link
Owner Author

/gemini review

@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from 224fe6d to c05576e Compare February 12, 2026 08:58
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new pkg/zstd package to pool ZSTD encoders and decoders, which is a great optimization for reducing allocation overhead in high-concurrency scenarios. The implementation is well-structured with wrappers for automatic resource management, and it has been integrated across various parts of the project, including chunk storage and server middleware. The changes are logical and improve performance. However, there is a widespread renaming of terms like 'compress' to 'zstded' and 'decompress' to 'dezstd', which makes the code awkward to read and less maintainable. I've added a few comments to highlight this issue.

I am having trouble creating individual review comments. Click here to see my feedback.

pkg/cache/cache.go (1349)

medium

The variable compressedSize has been renamed to zstdedSize. While the intent is to be specific about the compression algorithm, 'zstded' is not a standard English word and can be confusing. It also makes the code less generic. Consider using zstdCompressedSize or reverting to compressedSize and relying on context to understand the algorithm. This comment applies to similar renamings throughout the PR (e.g., zstdible, dezstded).

_, compressedSize, err := chunkStore.PutChunk(ctx, chunkMetadata.Hash, chunkMetadata.Data)

pkg/cache/cdc_test.go (336)

medium

The string 'compressible' has been changed to 'zstdible'. This is not a standard term and harms readability. Please consider using more conventional terms like 'compressible' or 'zstd_compressible'.

content := strings.Repeat("compressible", 1000)

pkg/zstd/README.md (24)

medium

The example code uses helper.NewPooledWriter. It seems helper should be zstd, as the package being documented is zstd. This appears to be a copy-paste error that is repeated throughout the README. Please correct this to avoid confusion for developers using this new package.

pw := zstd.NewPooledWriter(&buf)

@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch 3 times, most recently from 045bac5 to a69ed3b Compare February 12, 2026 09:07
@kalbasit
Copy link
Owner Author

/gemini review

@kalbasit kalbasit enabled auto-merge (squash) February 12, 2026 09:08
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a well-designed zstd package for pooling ZSTD encoders and decoders, which is a great optimization for reducing allocations and GC pressure. The integration across the codebase is clean and effectively uses the new pooling mechanism. The test suite has also been improved, particularly with more robust race condition testing for S3 storage. My feedback is minor, focusing on improving the new documentation and refining error handling in one of the test helpers to align with best practices for test cleanup.

@kalbasit kalbasit disabled auto-merge February 12, 2026 09:12
@kalbasit kalbasit enabled auto-merge (squash) February 12, 2026 09:20
This change introduces a new pkg/compress package that provides a pool
of ZSTD encoders and decoders. This optimization reduces allocation
overhead during high-concurrency compression and decompression
operations.

The implementation:
- Uses sync.Pool for managing zstd.Encoder and zstd.Decoder instances.
- Provides PooledZstdWriter and PooledZstdReader wrappers for easy
  resource management.
- Integrates the pool into the local chunk store for chunk compression.
- Integrates the pool into the server middleware for HTTP response
  compression.
- Updates NAR reading and test utilities to use the centralized
  compression package.

This was needed to improve the efficiency of the cache server when
handling many small and large NAR files simultaneously, minimizing
garbage collection pressure.
@kalbasit kalbasit force-pushed the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch from 0903582 to ff9c82d Compare February 12, 2026 09:32
@codecov
Copy link

codecov bot commented Feb 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 3.96%. Comparing base (42ad9bb) to head (ff9c82d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #865   +/-   ##
=====================================
  Coverage   3.96%   3.96%           
=====================================
  Files          6       6           
  Lines        429     429           
=====================================
  Hits          17      17           
  Misses       409     409           
  Partials       3       3           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit kalbasit merged commit 275527d into main Feb 12, 2026
16 checks passed
@kalbasit kalbasit deleted the 02-11-feat_implement_zstd_compression_pooling_and_integrate_across_project branch February 12, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request go Pull requests that update go code size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant