Skip to content

Lightpanda: the headless browser designed for AI and automation

`brotli`: prefer compression quality of mod_brotli of apache

Halil Durakbd5188356f80
diff --git a/src/browser/tests/compression/compression_stream.html b/src/browser/tests/compression/compression_stream.html
index fe3ff367f..142c415a7 100644
--- a/src/browser/tests/compression/compression_stream.html
+++ b/src/browser/tests/compression/compression_stream.html
@@ -391,12 +391,12 @@
state.resolve();
await state.done(() => {
- // Byte-for-byte what `brotli -q 11 -w 22` (v1.2.0) gives, the encoder's
- // defaults. 'hello' is too short to compress, so it's stored as is.
+ // Byte-for-byte what `brotli -q 5 -w 22` (v1.2.0) gives, the quality we
+ // use. 'hello' is too short to compress, so it's stored as is.
testing.expectEqual([11, 2, 128, 104, 101, 108, 108, 111, 3], results.hello);
testing.expectEqual([
- 27, 111, 0, 248, 29, 9, 54, 78, 116, 111, 36, 134, 215, 98, 101, 104, 114,
- 26, 132, 133, 141, 78, 178, 165, 189, 5, 130, 241, 0, 60, 51, 252, 18,
+ 27, 111, 0, 0, 196, 113, 108, 221, 252, 238, 234, 69, 68, 170, 65, 112,
+ 171, 199, 115, 122, 14, 173, 143, 9,
], results.repeated);
testing.expectEqual([59], results.empty);
});
@@ -432,7 +432,7 @@
const state = await testing.async();
// The encoder holds input back until it has a whole meta-block; for this
- // noise that's after 1.25 MiB, so the write produces output by itself. It
+ // noise that's after 1 MiB, so the write produces output by itself. It
// then stops taking input until that output is taken, so the rest of the
// write needs another call into the encoder.
const input = noise(1664 * 1024);
diff --git a/src/browser/webapi/compression/CompressionStream.zig b/src/browser/webapi/compression/CompressionStream.zig
index 0db834139..457cfa0d9 100644
--- a/src/browser/webapi/compression/CompressionStream.zig
+++ b/src/browser/webapi/compression/CompressionStream.zig
@@ -63,7 +63,5 @@ pub const JsApi = struct {
const testing = @import("../../../testing.zig");
test "WebApi: CompressionStream" {
- // brotli_output_before_close compresses 1.3 MiB at quality 11; that's
- // slow under TSAN.
- try testing.htmlRunner("compression/compression_stream.html", .{ .timeout_ms = 8000 });
+ try testing.htmlRunner("compression/compression_stream.html", .{});
}
diff --git a/src/browser/webapi/compression/brotli.zig b/src/browser/webapi/compression/brotli.zig
index 1d538c5f1..31931b670 100644
--- a/src/browser/webapi/compression/brotli.zig
+++ b/src/browser/webapi/compression/brotli.zig
@@ -147,6 +147,13 @@ pub const EncodingStream = struct {
const stream = brotli.BrotliEncoderCreateInstance(alloc, free, @constCast(self.exec)) orelse {
return error.OutOfMemory;
};
+ // brotli defaults to its maximum quality (11), which is ~60x
+ // slower than deflate at level 6. Quality 5 is faster than
+ // deflate at level 6 and still compresses better. Same
+ // default as Apache's mod_brotli.
+ // https://httpd.apache.org/docs/2.4/mod/mod_brotli.html#brotlicompressionquality
+ const default_quality: u32 = 5;
+ _ = brotli.BrotliEncoderSetParameter(stream, .quality, default_quality);
self.stream = stream;
self.state = .active;
continue :state_machine self.state;
diff --git a/src/sys/brotli.zig b/src/sys/brotli.zig
index 8296e2925..0cc5627ab 100644
--- a/src/sys/brotli.zig
+++ b/src/sys/brotli.zig
@@ -28,13 +28,6 @@ const brotli_free_func = ?*const fn (@"opaque": ?*anyopaque, address: ?*anyopaqu
// Encoder.
-pub const BROTLI_MIN_QUALITY: u32 = 0;
-pub const BROTLI_MAX_QUALITY: u32 = 11;
-pub const BROTLI_DEFAULT_QUALITY: u32 = 11;
-pub const BROTLI_MIN_WINDOW_BITS: u32 = 10;
-pub const BROTLI_MAX_WINDOW_BITS: u32 = 24;
-pub const BROTLI_DEFAULT_WINDOW: u32 = 22;
-
pub const BrotliEncoderState = opaque {};
pub const BrotliEncoderOperation = enum(c_int) {