Skip to content

zstd: Add ResetWithOptions to encoder/decoder#1122

Merged
klauspost merged 5 commits intomasterfrom
add-reset-opts
Feb 5, 2026
Merged

zstd: Add ResetWithOptions to encoder/decoder#1122
klauspost merged 5 commits intomasterfrom
add-reset-opts

Conversation

@klauspost
Copy link
Owner

@klauspost klauspost commented Jan 13, 2026

New API Methods

  func (*Encoder) ResetWithOptions(w io.Writer, opts ...EOption) error
  func (*Decoder) ResetWithOptions(r io.Reader, opts ...DOption) error

New Options

  // Encoder - clears dictionary
  `WithEncoderDictDelete() EOption`

  // Decoder - removes dicts by ID; no args clears all
  `WithDecoderDictDelete(ids ...uint32) DOption`

Option Reset Compatibility

Can be changed with ResetWithOptions:

  • Encoder: WithEncoderCRC, WithEncoderPadding, WithZeroFrames, WithAllLitEntropyCompression, WithNoEntropyCompression, WithSingleSegment, WithEncoderDict, WithEncoderDictRaw, WithEncoderDictDelete
  • Decoder: WithDecoderMaxMemory, WithDecoderMaxWindow, WithDecoderDicts, WithDecoderDictRaw, WithDecoderDictDelete, WithDecodeAllCapLimit, IgnoreChecksum

Cannot be changed with ResetWithOptions:

  • Encoder: WithEncoderConcurrency, WithWindowSize, WithEncoderLevel, WithLowerEncoderMem
  • Decoder: WithDecoderLowmem, WithDecoderConcurrency, WithDecodeBuffersBelow

Summary by CodeRabbit

  • New Features

    • Added ResetWithOptions for decoders and encoders to apply option changes when reinitializing streams.
    • Added ability to delete specific or all dictionaries during reset.
  • Chores

    • Tightened option mutability rules so some settings cannot be changed during reset to preserve stability.
    • Switched dictionary storage to ID-keyed maps for more direct lookup.
  • Tests

    • Added tests covering reset-with-options behavior and dictionary deletion scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

## New API Methods

```
  func (*Encoder) ResetWithOptions(w io.Writer, opts ...EOption) error
  func (*Decoder) ResetWithOptions(r io.Reader, opts ...DOption) error

```

New Options

  // Encoder - clears dictionary
  `WithEncoderDictDelete() EOption`

  // Decoder - removes dicts by ID; no args clears all
  `WithDecoderDictDelete(ids ...uint32) DOption`

Option Reset Compatibility

  Can be changed with ResetWithOptions:
  - Encoder: WithEncoderCRC, WithEncoderPadding, WithZeroFrames, WithAllLitEntropyCompression, WithNoEntropyCompression, WithSingleSegment, WithEncoderDict, WithEncoderDictRaw, WithEncoderDictDelete
  - Decoder: WithDecoderMaxMemory, WithDecoderMaxWindow, WithDecoderDicts, WithDecoderDictRaw, WithDecoderDictDelete, WithDecodeAllCapLimit, IgnoreChecksum

  Cannot be changed with ResetWithOptions:
  - Encoder: WithEncoderConcurrency, WithWindowSize, WithEncoderLevel, WithLowerEncoderMem
  - Decoder: WithDecoderLowmem, WithDecoderConcurrency, WithDecodeBuffersBelow
@coderabbitai
Copy link

coderabbitai bot commented Jan 13, 2026

📝 Walkthrough

Walkthrough

Added ResetWithOptions to Encoder and Decoder to apply option changes during stream resets, moved decoder dictionary storage from per-decoder field to options-layer map, introduced resetOpt to restrict certain option changes during resets, and added dictionary deletion options and tests.

Changes

Cohort / File(s) Summary
Core Reset Methods
zstd/decoder.go, zstd/encoder.go
Added ResetWithOptions(r io.Reader, opts ...DOption) error and ResetWithOptions(w io.Writer, opts ...EOption) error. Decoder now reads dictionaries from options (d.o.dicts) instead of a per-decoder field.
Options & Dict Storage
zstd/decoder_options.go, zstd/encoder_options.go
Introduced resetOpt flag in options to mark reset context and prevent changing certain options during reset. Switched decoder dict storage to map[uint32]*dict. Added public options WithDecoderDictDelete(ids ...uint32) and WithEncoderDictDelete() to remove dictionaries. Several option setters now reject changes when reset is active.
Tests
zstd/decoder_test.go, zstd/encoder_options_test.go
Added tests: TestDecoderResetWithOptions, TestDecoderDictDelete, TestDecoderDictDeleteMultiple, TestEncoderResetWithOptions, and TestEncoderDictDelete to validate reset semantics and dict lifecycle operations.

Sequence Diagram(s)

mermaid
sequenceDiagram
participant Client
participant Decoder
participant Options
participant Reader
Client->>Decoder: Call ResetWithOptions(reader, opts...)
Decoder->>Options: set resetOpt = true
Options-->>Decoder: apply each DOption (may mutate o.dicts or other fields)
Decoder->>Decoder: call Reset(reader) (reinitialize stream using o)
Decoder->>Options: set resetOpt = false
Decoder-->>Client: return result

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 77.42% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'zstd: Add ResetWithOptions to encoder/decoder' directly and accurately summarizes the main change—introducing ResetWithOptions methods to both Encoder and Decoder types.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@klauspost
Copy link
Owner Author

@dsnet As per #1110

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
zstd/decoder_options.go (1)

95-96: Typo in error message.

The error message uses WithDecoderMaxmemory but the function is WithDecoderMaxMemory (capital M).

Proposed fix
-		return errors.New("WithDecoderMaxmemory must be less than 1 << 63")
+		return errors.New("WithDecoderMaxMemory must be less than 1 << 63")

@klauspost klauspost merged commit c03560f into master Feb 5, 2026
22 checks passed
@klauspost klauspost deleted the add-reset-opts branch February 5, 2026 15:29
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