COROIO: NNet::NActors::TOutputBuffer Class Reference
COROIO
Loading...
Searching...
No Matches
NNet::NActors::TOutputBuffer Class Reference

Byte buffer of the outbound stream of TNode: grows without zero-filling. More...

#include <output_buffer.hpp>

Public Member Functions

 TOutputBuffer (const TOutputBuffer &)=delete
TOutputBuffer & operator= (const TOutputBuffer &)=delete
 TOutputBuffer (TOutputBuffer &&other) noexcept
 Takes the block; other is left empty.
TOutputBuffer & operator= (TOutputBuffer &&other) noexcept
 Frees the block of this and takes the block of other, which is left empty.
char * Data ()
 The start of the block, null while the capacity is 0. Valid until the next Reserve or Swap.
size_t Capacity () const
 The size of the block in bytes.
void Reserve (size_t used, size_t size)
void Swap (TOutputBuffer &other) noexcept
 Exchanges the blocks of the two buffers.

Static Public Attributes

static constexpr size_t MinCapacity = 4096
 The smallest block Reserve allocates: small frames do not cause a chain of tiny reallocations.
static constexpr size_t GrowthFactor = 2
 A block grows to this many times its size at least: the total copy stays proportional to the final size.
static constexpr size_t StaleRatio = 2

Detailed Description

Byte buffer of the outbound stream of TNode: grows without zero-filling.

std::vector<char>::resize writes zeros into every byte it adds, and the writers overwrite them right away. This buffer only owns memory: Reserve makes room, the caller tracks how many bytes are in use and writes every byte before it reads it. The memory is not initialized, so a serializer must not leave gaps in the range it was given.

It grows geometrically and never shrinks. A block that is mostly full is grown by realloc, which lets the allocator extend or remap it, and copies the whole old block only when it cannot (at most twice the bytes in use). A block that holds mostly stale bytes is replaced by a new one and only the bytes in use are copied.

Member Function Documentation

◆ Reserve()

void NNet::NActors::TOutputBuffer::Reserve ( size_t used,
size_t size )
inline

Makes room for size bytes and keeps the first used bytes (used <= size). Throws std::bad_alloc and leaves the buffer as it was.

Member Data Documentation

◆ StaleRatio

size_t NNet::NActors::TOutputBuffer::StaleRatio = 2
staticconstexpr

A block with less than 1/StaleRatio of its bytes in use is mostly stale (left from the previous use), so it is cheaper to copy the used bytes than to let realloc copy all of them.


The documentation for this class was generated from the following file: