Skip to content

Compression: add zstd, switch backend to bytesrw, compress by default in serve mode#303

Open
balat wants to merge 6 commits into
masterfrom
compression
Open

Compression: add zstd, switch backend to bytesrw, compress by default in serve mode#303
balat wants to merge 6 commits into
masterfrom
compression

Conversation

@balat

@balat balat commented Jun 26, 2026

Copy link
Copy Markdown
Member

Summary

Improves Ocsigen Server's content compression, in the spirit of the recent
quick-wins bringing it closer to mainstream static servers (Caddy, nginx).

  • zstd content-encoding (RFC 8878) is now negotiated alongside gzip and
    deflate, preferred at equal qvalue.
  • The Deflatemod backend is switched from camlzip to bytesrw (a single,
    modern, composable writer-filter abstraction for gzip/deflate/zstd). This
    removes ~90 lines of hand-written gzip framing (header, CRC32, trailer) and
    drops the camlzip dependency (it was used nowhere else).
  • Compression is enabled by default in the one-command serve mode
    (ocsigenserver ./public), via a small registry mirroring the static-server
    one, so it matches caddy file-server.

Brotli is intentionally not included: there is currently no maintained
OCaml 5 binding to libbrotli (the brotli opam package requires ocaml < 4.10).
Left as a follow-up.

Notes

  • HTTP deflate maps to the zlib format (Bytesrw_zlib.Zlib), matching the
    previous camlzip behaviour (it emitted a zlib header), not raw deflate.
  • New dependencies: bytesrw, conf-zlib, conf-zstd (system depexts:
    libzstd-dev, zlib1g-dev).

Tests

  • deflatemod.t: per-codec Content-Encoding (gzip, deflate, zstd) and the zstd
    preference.
  • serve.t: serve mode compresses by default.
  • dune build @check and @doc are green.

balat added 6 commits June 26, 2026 14:53
Replace the hand-written gzip framing (header, CRC32, trailer) and the
camlzip Zlib calls with bytesrw writer filters. gzip and deflate (zlib
format) behaviour is unchanged; the streaming model now bridges the
synchronous bytesrw compressor to the Lwt output through an in-memory
buffer flushed after each chunk.
Negotiate the "zstd" Content-Encoding (RFC 8878) alongside gzip and
deflate, using bytesrw.zstd. zstd is preferred over gzip and deflate at
equal qvalue.
Add a compression registry mirroring the static-server one. The
Deflatemod extension publishes a compression instruction with safe
defaults (text-based content types), loaded on demand by serve mode and
applied after the file server. Matches the on-the-fly compression of
`caddy file-server`.
Assert per-codec Content-Encoding (gzip, deflate, zstd) and the zstd
preference in deflatemod.t, and that serve mode compresses by default.
Update the Deflatemod manual page for the zstd content-encoding, the
codec preference order, the bytesrw dependency, the lack of a Brotli
binding, and on-by-default compression in serve mode. Add a CHANGES
entry.

@Julow Julow 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.

Very nice! Do you have an idea of the changes in performances ?

@balat

balat commented Jun 26, 2026

Copy link
Copy Markdown
Member Author

No :)

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.

2 participants