google / sxg-rs

A set of tools for generating signed exchanges at serve time.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reduce payload buffer size in cloudflare_worker

twifkak opened this issue · comments

Per this TODO:

As a performance optimization, maybe start with a Content-Length sized buffer and resize exponentially if necessary. Alternatively, use the limitBytes() transformer in streamFrom, and construct a flyweight Response object here in order to call arrayBuffer().

We can't rely on Content-Length for most payloads. It appears CF strips the header unless Cache-Control: no-transform.

We can't use TransformStreams because of another Workers limitation.

My next attempt is to dig into Chromium to see what buffer allocation/growth strategy it uses and mimic it.

Failing that, I'll try to look at a distribution of body sizes (e.g. from HTTP archive) and hopefully a decent shelf emerges.

Response.arrayBuffer() calls FetchDataLoader::CreateLoaderAsArrayBuffer(), which in turn: