Skip to content

Send already-encoded tunnel response bodies without permessage-deflate - #8223

Merged
ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf-tunnel-skip-body-deflate
Nov 30, 2026
Merged

ymichael merged 2 commits into
get-bb:mainfrom
lanjak:perf-tunnel-skip-body-deflate

Conversation

@lanjak

@lanjak lanjak commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Human comments

What was wrong

The bb Connect tunnel socket offers permessage-deflate (the ws default in plugins/connect/src/tunnel.ts), and TunnelSession.sendRaw called tunnel.send(data) with no options, so ws deflated every frame. Each origin response chunk goes through chunkBody and send as a body-chunk frame. The bb server already encodes its compressible responses (brotli or gzip API JSON, hono compress(), precompressed assets), so the tunnel client compressed those bodies a second time. I measured the call path on the real TunnelSession against a local deflate-capable ws server before the change: an 8 MB brotli body cost 229-280 ms of client CPU (median 258 ms), and the wire was 3.6 KB larger than with no deflate.

What changed

  • packages/tunnel-client/src/session.ts: executeHttp decides once per response whether its body-chunk frames are deflated. They are deflated only when the response has no Content-Encoding (or identity) and a Content-Type that matches hono's COMPRESSIBLE_CONTENT_TYPE_REGEX (the rule the bb server's compress() uses). Encoded bodies and incompressible types (images, archives, octet streams) go out with compress: false. All other frames keep the ws default (compress: true). @bb/tunnel-client now depends on hono for that regex.
  • ws keeps frame order when compressed and uncompressed sends mix, because its sender queues a send while a deflate is in progress. The frame format does not change.
  • No wire, CLI or protocol changes. No HOST_DAEMON_PROTOCOL_VERSION or tunnel PROTOCOL_VERSION bump.

Deviation from the issue proposal: the issue suggests compressing only ws-data frames. This PR keeps compression for bodies with no Content-Encoding, because the measurement shows a large gain there: an 8 MB identity JSON body crosses the wire as about 750 KB with deflate. Origins behind a shared port (for example a dev server) can send such bodies. The choice by Content-Encoding can only change CPU, not the response the visitor gets.

Not in this PR: the Durable Object also deflates its sends to the tunnel (uploads). Stopping that needs perMessageDeflate: false on the tunnel, which also stops compression of realtime pushes over the host uplink. That is a trade-off for a maintainer to decide.

How you verified

New test packages/tunnel-client/test/tunnel-compression.test.ts: a real TunnelSession on a real ws client connected to a WebSocketServer with perMessageDeflate: true, which counts the bytes on its TCP socket. It relays a 1 MB body with content-encoding: br, a 1 MB body with no encoding, and a 1 MB origin WebSocket message.

  • Before the change: sends an already content-encoded response body uncompressed fails with AssertionError: expected 1727 to be greater than 1048576 (the encoded body was deflated). The other 3 tests pass.
  • After the change: 4 of 4 pass.

Benchmark (script outside the commit): the gate and the origin run in a child process, so the measured CPU is only the tunnel client process. 8 MB body, 64 KB origin chunks.

body metric before after
8 MB, content-encoding: br (7 runs) client CPU 229-280 ms (median 258) 8-49 ms (median 37)
wall time to body-end 176-227 ms (median 205) 9-32 ms (median 20)
wire bytes 8,395,583 8,391,961
8 MB JSON, no encoding (5 runs) client CPU 61-84 ms (median 71) 63-101 ms (median 65)
wire bytes 742,866-753,955 742,975-753,955

Commands:

  • pnpm exec turbo run typecheck test lint lint:type-aware --continue --concurrency=6 --filter=@bb/tunnel-client --filter=bb-plugin-connect: 13 of 13 tasks pass (tunnel-client 31 tests, connect plugin 158 tests).
  • pnpm exec turbo run typecheck --filter=@bb/server --filter=@bb/host-daemon: pass.
  • apps/server/test/security/connect-request-guard.test.ts (uses TunnelSession): pass.

Through a real tunnel on bb-connect-staging (the Cloudflare gate negotiates permessage-deflate), real TunnelSession dialing the gate, curl as the visitor, client CPU per request (medians of 3 interleaved rounds × 5; final row set from the content-type follow-up):

body CPU before CPU after wire before → after
8 MB random, content-encoding: br 305–344 ms 40–144 ms 8.39 MB → 8.39 MB
687 KB brotli JSON 29–31 ms 4–13 ms 655 KB → 687 KB (synthetic; real bb brotli timelines gain 0.0% from deflate)
8 MB JSON, no encoding 64–95 ms 56–93 ms 630 KB → 630 KB (still deflated)
8 MB image/png, no encoding 189–332 ms 99–142 ms 8.39 MB → 8.39 MB

The test also covers an image/png body with no encoding (sent uncompressed).

Fixes #8222

AGENT GENERATED

The bb Connect tunnel negotiates permessage-deflate with the gate, and
TunnelSession sent every frame with the ws default compress: true. The
bb server already encodes its compressible responses with brotli or
gzip, so the tunnel client deflated those body chunks again for no size
gain. For 8 MB of brotli body this cost 230-280 ms of client CPU and
made the wire 3.6 KB larger.

Send body-chunk frames with compress: false when the origin response has
a Content-Encoding. Bodies without one, and all other frames, keep the
default compression.
Body frames are deflated only when the origin response has no content
encoding (or identity) and a content type that hono's compress() would
compress, so images and other already-compressed media are not deflated
for no gain. Frames other than response bodies keep compression.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ymichael
ymichael merged commit b9b63e3 into get-bb:main Nov 30, 2026
51 checks passed
scowalt pushed a commit to scowalt/bb that referenced this pull request Dec 1, 2026
get-bb#8223)

## Human comments

## What was wrong

The bb Connect tunnel socket offers permessage-deflate (the `ws` default
in `plugins/connect/src/tunnel.ts`), and `TunnelSession.sendRaw` called
`tunnel.send(data)` with no options, so `ws` deflated every frame. Each
origin response chunk goes through `chunkBody` and `send` as a
`body-chunk` frame. The bb server already encodes its compressible
responses (brotli or gzip API JSON, hono `compress()`, precompressed
assets), so the tunnel client compressed those bodies a second time. I
measured the call path on the real `TunnelSession` against a local
deflate-capable `ws` server before the change: an 8 MB brotli body cost
229-280 ms of client CPU (median 258 ms), and the wire was 3.6 KB larger
than with no deflate.

## What changed

- `packages/tunnel-client/src/session.ts`: `executeHttp` decides once
per response whether its `body-chunk` frames are deflated. They are
deflated only when the response has no `Content-Encoding` (or
`identity`) and a `Content-Type` that matches hono's
`COMPRESSIBLE_CONTENT_TYPE_REGEX` (the rule the bb server's `compress()`
uses). Encoded bodies and incompressible types (images, archives, octet
streams) go out with `compress: false`. All other frames keep the `ws`
default (`compress: true`). `@bb/tunnel-client` now depends on `hono`
for that regex.
- `ws` keeps frame order when compressed and uncompressed sends mix,
because its sender queues a send while a deflate is in progress. The
frame format does not change.
- No wire, CLI or protocol changes. No `HOST_DAEMON_PROTOCOL_VERSION` or
tunnel `PROTOCOL_VERSION` bump.

Deviation from the issue proposal: the issue suggests compressing only
`ws-data` frames. This PR keeps compression for bodies with no
`Content-Encoding`, because the measurement shows a large gain there: an
8 MB identity JSON body crosses the wire as about 750 KB with deflate.
Origins behind a shared port (for example a dev server) can send such
bodies. The choice by `Content-Encoding` can only change CPU, not the
response the visitor gets.

Not in this PR: the Durable Object also deflates its sends to the tunnel
(uploads). Stopping that needs `perMessageDeflate: false` on the tunnel,
which also stops compression of realtime pushes over the host uplink.
That is a trade-off for a maintainer to decide.

## How you verified

New test `packages/tunnel-client/test/tunnel-compression.test.ts`: a
real `TunnelSession` on a real `ws` client connected to a
`WebSocketServer` with `perMessageDeflate: true`, which counts the bytes
on its TCP socket. It relays a 1 MB body with `content-encoding: br`, a
1 MB body with no encoding, and a 1 MB origin WebSocket message.

- Before the change: `sends an already content-encoded response body
uncompressed` fails with `AssertionError: expected 1727 to be greater
than 1048576` (the encoded body was deflated). The other 3 tests pass.
- After the change: 4 of 4 pass.

Benchmark (script outside the commit): the gate and the origin run in a
child process, so the measured CPU is only the tunnel client process. 8
MB body, 64 KB origin chunks.

| body | metric | before | after |
|---|---|---|---|
| 8 MB, `content-encoding: br` (7 runs) | client CPU | 229-280 ms
(median 258) | 8-49 ms (median 37) |
| | wall time to `body-end` | 176-227 ms (median 205) | 9-32 ms (median
20) |
| | wire bytes | 8,395,583 | 8,391,961 |
| 8 MB JSON, no encoding (5 runs) | client CPU | 61-84 ms (median 71) |
63-101 ms (median 65) |
| | wire bytes | 742,866-753,955 | 742,975-753,955 |

Commands:

- `pnpm exec turbo run typecheck test lint lint:type-aware --continue
--concurrency=6 --filter=@bb/tunnel-client --filter=bb-plugin-connect`:
13 of 13 tasks pass (tunnel-client 31 tests, connect plugin 158 tests).
- `pnpm exec turbo run typecheck --filter=@bb/server
--filter=@bb/host-daemon`: pass.
- `apps/server/test/security/connect-request-guard.test.ts` (uses
`TunnelSession`): pass.

Through a real tunnel on bb-connect-staging (the Cloudflare gate
negotiates `permessage-deflate`), real `TunnelSession` dialing the gate,
curl as the visitor, client CPU per request (medians of 3 interleaved
rounds × 5; final row set from the content-type follow-up):

| body | CPU before | CPU after | wire before → after |
|---|---|---|---|
| 8 MB random, `content-encoding: br` | 305–344 ms | 40–144 ms | 8.39 MB
→ 8.39 MB |
| 687 KB brotli JSON | 29–31 ms | 4–13 ms | 655 KB → 687 KB (synthetic;
real bb brotli timelines gain 0.0% from deflate) |
| 8 MB JSON, no encoding | 64–95 ms | 56–93 ms | 630 KB → 630 KB (still
deflated) |
| 8 MB `image/png`, no encoding | 189–332 ms | 99–142 ms | 8.39 MB →
8.39 MB |

The test also covers an `image/png` body with no encoding (sent
uncompressed).

Fixes get-bb#8222

> AGENT GENERATED

---------

Co-authored-by: Michael Yong <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
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.

bb Connect tunnel deflates response bodies that the server already compressed

2 participants