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:
- initializes with
WTF::SharedBuffer::Create()
(defaults to size 0) - appends with
WTF::SharedBuffer::Append()
(builds a list of 4KB-sized segments) - converts to ArrayBuffer with
memcpy
from each of those segments