Skip to content

flate: Add examples - #1102

Merged
klauspost merged 1 commit into
masterfrom
add-examples
Sep 19, 2025
Merged

flate: Add examples#1102
klauspost merged 1 commit into
masterfrom
add-examples

Conversation

@klauspost

@klauspost klauspost commented Sep 19, 2025

Copy link
Copy Markdown
Owner

Fixes minor difference to stdlib, which doesn't do a 0 byte write.

Summary by CodeRabbit

  • Bug Fixes

    • Prevents unnecessary zero-byte writes during compression flushes, improving stability and reducing I/O overhead.
  • Tests

    • Added example-based tests demonstrating reset and reuse of compressor/decompressor.
    • Added example for dictionary-based compression and decompression.
    • Added example simulating synchronized streaming over pipes.
Fixes minor difference to stdlib, which doesn't do a 0 byte write.
@coderabbitai

coderabbitai Bot commented Sep 19, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Adds example tests in flate/example_test.go demonstrating Reset reuse, dictionary-based compression, and streaming synchronization. Modifies huffmanBitWriter.flush to conditionally write buffered bytes only when present (n > 0). No exported API changes.

Changes

Cohort / File(s) Summary
Examples and tests
flate/example_test.go
New example tests: Example_reset (reuse Reader/Writer via Reset), Example_dictionary (use of NewWriterDict/NewReaderDict), Example_synchronization (streaming over io.Pipe with framing and goroutines).
Huffman bit writer flush logic
flate/huffman_bit_writer.go
flush now writes the remaining buffered bytes only if count n > 0; other flush steps unchanged.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant C as Caller
  participant BW as huffmanBitWriter
  participant W as underlying io.Writer

  C->>BW: flush()
  BW->>BW: finalize bits/EOB, compute n
  alt n > 0
    BW->>W: Write(buffer[0:n])
    W-->>BW: bytes written / error
  else n == 0
    BW-->>BW: skip final write
  end
  BW-->>C: return error (if any)
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "flate: Add examples" is concise and clearly describes the primary change — adding three example tests under flate/example_test.go — and is easy for a reviewer to scan in history. It is specific and not noisy, though it does not mention a small additional fix in huffman_bit_writer.go to avoid a 0-byte write. Overall the title accurately reflects a major, visible part of the changeset.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-examples

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
flate/example_test.go (1)

128-129: Use consistent Reader source for clarity.

Mixing bytes.NewReader(b.Bytes()) and &b works, but using bytes.NewReader in both places avoids relying on bytes.Buffer read state if this example is edited later.

-	zr = flate.NewReaderDict(&b, hashDict)
+	zr = flate.NewReaderDict(bytes.NewReader(b.Bytes()), hashDict)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f44517c and c158c11.

📒 Files selected for processing (2)
  • flate/example_test.go (1 hunks)
  • flate/huffman_bit_writer.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
flate/example_test.go (1)
flate/deflate.go (1)
  • NewWriterDict (905-913)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (15)
  • GitHub Check: build (1.23.x, windows-latest)
  • GitHub Check: build (1.25.x, ubuntu-latest)
  • GitHub Check: build (1.25.x, windows-latest)
  • GitHub Check: build (1.23.x, ubuntu-latest)
  • GitHub Check: build (1.24.x, macos-latest)
  • GitHub Check: build (1.23.x, macos-latest)
  • GitHub Check: fuzz-s2 ("noasm,nounsafe")
  • GitHub Check: build (1.25.x, macos-latest)
  • GitHub Check: build (1.24.x, windows-latest)
  • GitHub Check: fuzz-other ("noasm,nounsafe")
  • GitHub Check: build (1.24.x, ubuntu-latest)
  • GitHub Check: fuzz-other (nounsafe)
  • GitHub Check: fuzz-s2 (nounsafe)
  • GitHub Check: fuzz-zstd (nounsafe)
  • GitHub Check: build-special
🔇 Additional comments (4)
flate/huffman_bit_writer.go (1)

214-216: Great: avoid zero-byte writes in flush.

Conditionally writing only when n > 0 matches stdlib behavior and avoids spurious writes to underlying writers (e.g., pipes/sockets) with no downside.

flate/example_test.go (3)

22-63: Examples: Reset usage looks solid.

Clear demonstration of reusing writer/reader with shared buffers and proper Close/Reset ordering.


189-193: Flush semantics align with the writer change.

Per-message Flush is correct here; combined with the flush fix, there are no zero-byte writes on empty buffers.


158-246: Synchronization example reads well.

Good use of io.Pipe, framing, and graceful shutdown with wg.Wait and Close/EOF handling.

@klauspost
klauspost merged commit bda824b into master Sep 19, 2025
22 checks passed
@klauspost
klauspost deleted the add-examples branch September 19, 2025 13:44
hantang pushed a commit to qundao/mirror-forgejo that referenced this pull request Oct 21, 2025
This PR contains the following updates:

| Package | Change | Age | Confidence |
|---|---|---|---|
| [github.com/klauspost/compress](https://github.com/klauspost/compress) | `v1.18.0` -> `v1.18.1` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fklauspost%2fcompress/v1.18.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fklauspost%2fcompress/v1.18.0/v1.18.1?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>klauspost/compress (github.com/klauspost/compress)</summary>

### [`v1.18.1`](https://github.com/klauspost/compress/releases/tag/v1.18.1)

[Compare Source](klauspost/compress@v1.18.0...v1.18.1)

#### What's Changed

- zstd: Fix incorrect buffer size in dictionary encodes by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1059](klauspost/compress#1059)
- s2: check for cap, not len of buffer in EncodeBetter/Best by [@&#8203;vdarulis](https://github.com/vdarulis) in [#&#8203;1080](klauspost/compress#1080)
- zstd: Add simple zstd EncodeTo/DecodeTo functions by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1079](klauspost/compress#1079)
- zlib: Avoiding extra allocation in zlib.reader.Reset by [@&#8203;travelpolicy](https://github.com/travelpolicy) in [#&#8203;1086](klauspost/compress#1086)
- gzhttp: remove redundant err check in zstdReader by [@&#8203;ryanfowler](https://github.com/ryanfowler) in [#&#8203;1090](klauspost/compress#1090)
- Run modernize. Deprecate Go 1.22 by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1095](klauspost/compress#1095)
- flate: Simplify matchlen by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1101](klauspost/compress#1101)
- flate: Add examples by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1102](klauspost/compress#1102)
- flate: Use exact sizes for huffman tables by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1103](klauspost/compress#1103)
- flate: Faster load+store by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1104](klauspost/compress#1104)
- Add notice to S2 about MinLZ by [@&#8203;klauspost](https://github.com/klauspost) in [#&#8203;1065](klauspost/compress#1065)

#### New Contributors

- [@&#8203;wooffie](https://github.com/wooffie) made their first contribution in [#&#8203;1069](klauspost/compress#1069)
- [@&#8203;vdarulis](https://github.com/vdarulis) made their first contribution in [#&#8203;1080](klauspost/compress#1080)
- [@&#8203;travelpolicy](https://github.com/travelpolicy) made their first contribution in [#&#8203;1086](klauspost/compress#1086)
- [@&#8203;ryanfowler](https://github.com/ryanfowler) made their first contribution in [#&#8203;1090](klauspost/compress#1090)

**Full Changelog**: <klauspost/compress@v1.18.0...v1.18.1>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC), Automerge - Between 12:00 AM and 03:59 AM ( * 0-3 * * * ) (UTC).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0MS4xNTIuOSIsInVwZGF0ZWRJblZlciI6IjQxLjE1Mi45IiwidGFyZ2V0QnJhbmNoIjoiZm9yZ2VqbyIsImxhYmVscyI6WyJkZXBlbmRlbmN5LXVwZ3JhZGUiLCJ0ZXN0L25vdC1uZWVkZWQiXX0=-->

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9786
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: Renovate Bot <forgejo-renovate-action@forgejo.org>
Co-committed-by: Renovate Bot <forgejo-renovate-action@forgejo.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant