Skip to main content

MAX_PREALLOC_BYTES

Constant MAX_PREALLOC_BYTES 

Source
const MAX_PREALLOC_BYTES: usize = _; // 67_108_864usize
Expand description

Upper bound on how much memory a single message length is allowed to reserve before any of the bytes it promises have been read.

Message lengths come from the stream itself, so they cannot be trusted: a truncated or corrupted stream can declare a body of arbitrary size. Reserving that up front turns a malformed input into an allocation failure, which aborts the process rather than returning an error the caller can handle. Beyond this size the buffer grows as the data arrives instead, so an implausible length costs one bounded allocation and then fails as a short read.

The value trades the size of that bounded allocation against how large a body still gets read in a single allocation: bodies up to this size behave exactly as before, larger ones start here and the buffer doubles as the data arrives, so a body of n bytes costs log2(n / MAX_PREALLOC_BYTES) reallocations and the buffer never runs more than a factor of two ahead of the bytes actually received.