estimate_flatbuffer_capacity

Function estimate_flatbuffer_capacity 

Source
pub fn estimate_flatbuffer_capacity(
    function_name: &str,
    args: &[ParameterValue],
) -> usize
Expand description

Estimates the required buffer capacity for encoding a FunctionCall with the given parameters. This helps avoid reallocation during FlatBuffer encoding when passing large slices and strings.

The function aims to be lightweight and fast and run in O(1) as long as the number of parameters is limited (which it is since hyperlight only currently supports up to 12).

Note: This estimates the capacity needed for the inner vec inside a FlatBufferBuilder. It does not necessarily match the size of the final encoded buffer. The estimation always rounds up to the nearest power of two to match FlatBufferBuilder’s allocation strategy.

The estimations are numbers used are empirically derived based on the tests below and vaguely based on https://flatbuffers.dev/internals/ and https://github.com/dvidelabs/flatcc/blob/f064cefb2034d1e7407407ce32a6085c322212a7/doc/binary-format.md#flatbuffers-binary-format