SlicePool

Namespace: SnowBank.Buffers · class

Slice generator that use one or more underlying buffers to store the produced bytes, in order to reduce memory allocations.

Remarks

This is faster than a SliceWriter when writing a lot of keys that will not survive the lifetime of the pool itself. Warning: Since the pool can reuse its internal buffer between sessions, this breaks the immutability contract for long-lived Slices, and may introduce corruption if not used properly. Warning: instances of this type are NOT thread-safe. In multithreaded contexts, each thread should either use locking, or have its own pool instance.

Constructors

SlicePool

SlicePool()

Create a new slice pool with the default initial capacity

SlicePool(byte[] buffer)

Create a new slice pool using the provided initial buffer

SlicePool(int capacity)

Create a new slice pool using the specific initial capacity

Methods

AllocateSpan

Span<byte> AllocateSpan(int count)

Allocate a new slice from the pool

  • count — Size of the slice

Returns: Slice that maps the allocated region

There is NO guarantee that the allocated slice is filled with zero! The caller should manually clear the slice before using it, if necessary

Ascii

Slice Ascii(string text)

Create a slice from an ASCII string, where all the characters map directory into bytes (0..255). The string will be checked before being encoded.

This method will check each character and fail if at least one is greater than 255. Slices encoded by this method are only guaranteed to roundtrip if decoded with . If the original string only contained ASCII characters (0..127) then it can also be decoded by . The only difference between this method and is that the later will truncate non-ASCII characters to their lowest 8 bits, while the former will throw an exception.

Bytes

Slice Bytes(byte[] bytes)

Copies an array of bytes

Slice Bytes(Slice bytes)

Copies a slice of bytes

Slice Bytes(byte[] bytes, int offset, int count)

Copies a section of an array of bytes

Fixed

Slice Fixed(int value, int ks)

Format a number into a binary slice of variable size

  • value — Number fo format
  • ks — Size of the key

Fixed128

Slice Fixed128(long lo, long hi)

Return a fixed size, little-endian encoded, 128-bit signed integer

Slices produced by this method are NOT ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0xEF 0xCD 0xAB 0x89 0x67 0x45 0x23 0x01 }

Slice Fixed128(ulong lo, ulong hi)

Return a fixed size, little-endian encoded, 128-bit unsigned integer

Slices produced by this method are NOT ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0xEF 0xCD 0xAB 0x89 0x67 0x45 0x23 0x01 }

Fixed128BE

Slice Fixed128BE(long lo, long hi)

Return a fixed size, big-endian encoded, 128-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed128BE(0x0011223344556677, 0x8899AABBCCDDEEFF) => { 0x00 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 0x99 0xAA 0xBB 0xCC 0xDD 0xEE 0xFF }

Slice Fixed128BE(ulong lo, ulong hi)

Return a fixed size, big-endian encoded, 128-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed128BE(0x0011223344556677, 0x8899AABBCCDDEEFF) => { 0x00 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88 0x99 0xAA 0xBB 0xCC 0xDD 0xEE 0xFF }

Fixed16

Slice Fixed16(short value)

Return a fixed size, little-endian encoded, 16-bit signed integer

Slices produced by this method are NOT ordered lexicographically

Fixed16(0x1234) => { 0x34 0x12 }

Slice Fixed16(ushort value)

Return a fixed size, little-endian encoded, 16-bit unsigned integer

Slices produced by this method are NOT ordered lexicographically

Fixed16(0x1234) => { 0x34 0x12 }

Fixed16BE

Slice Fixed16BE(short value)

Return a fixed size, big-endian encoded, 16-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed16BE(0x1234) => { 0x12 0x34 }

Slice Fixed16BE(ushort value)

Return a fixed size, big-endian encoded, 16-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed16BE(0x1234) => { 0x12 0x34 }

Fixed32

Slice Fixed32(int value)

Return a fixed size, little-endian encoded, 32-bit signed integer

Slices produced by this method are NOT ordered lexicographically

Fixed32(0x12345678) => { 0x78 0x56 0x34 0x12 }

Slice Fixed32(uint value)

Return a fixed size, little-endian encoded, 32-bit unsigned integer

Slices produced by this method are NOT ordered lexicographically

Fixed32(0x12345678) => { 0x78 0x56 0x34 0x12 }

Fixed32BE

Slice Fixed32BE(int value)

Return a fixed size, big-endian encoded, 32-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed32BE(0x12345678) => { 0x12 0x34 0x56 0x78 }

Slice Fixed32BE(uint value)

Return a fixed size, big-endian encoded, 32-bit unsigned integer

Slices produced by this method ARE ordered lexicographically

Fixed32BE(0x12345678) => { 0x12 0x34 0x56 0x78 }

Fixed64

Slice Fixed64(long value)

Return a fixed size, little-endian encoded, 64-bit signed integer

Slices produced by this method are NOT ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0xEF 0xCD 0xAB 0x89 0x67 0x45 0x23 0x01 }

Slice Fixed64(ulong value)

Return a fixed size, little-endian encoded, 64-bit unsigned integer

Slices produced by this method are NOT ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0xEF 0xCD 0xAB 0x89 0x67 0x45 0x23 0x01 }

Fixed64BE

Slice Fixed64BE(long value)

Return a fixed size, big-endian encoded, 64-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF }

Slice Fixed64BE(ulong value)

Return a fixed size, big-endian encoded, 64-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed64(0x0123456789ABCDEF) => { 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF }

Fixed8

Slice Fixed8(byte value)

Return a 1-byte slice from an 8-bit integer

Slices produced by this method ARE ordered lexicographically

Fixed8(0x12) => { 0x12 }

Slice Fixed8(sbyte value)

Return a 1-byte slice from an 8-bit signed integer

Slices produced by this method ARE ordered lexicographically

Fixed8(0x12) => { 0x12 }

Keyword

Slice Keyword(string text)

Create a slice from an byte string, where all the characters map directly into bytes (0..255), without performing any validation

This method does not make any effort to detect characters above 255, which will be truncated to their lower 8 bits, introducing corruption when the string will be decoded. Please MAKE SURE to not call this with untrusted data. Slices encoded by this method are ONLY compatible with UTF-8 encoding if all characters are between 0 and 127. If this is not the case, then decoding it as an UTF-8 sequence may introduce corruption.

Number

Slice Number(int value, string format)

Format a number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Slice Number(uint value, string format)

Format a number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Slice Number(long value, string format)

Format a number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Slice Number(ulong value, string format)

Format a number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Slice Number(float value, string format)

Format a decimal number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Slice Number(double value, string format)

Format a decimal number into a slice

  • value — Number to format
  • format — Format as would be used with ToString

Reset

void Reset()

Reset the slice pool

Any slice previously created from this pool should not be in use anymore, or they will be overwritten!

void Reset(int capacity)

Reset the slice pool, and ensure that it can hold a specific capacity

Any slice previously created from this pool should not be in use anymore, or they will be overwritten!

Utf8

Slice Utf8(string text)

Create a slice containing the UTF-8 bytes of the string text.

DO NOT call this method to encode special strings that contain binary prefixes, like "\xFF/some/system/path" or "\xFE\x01\x02\x03", because they do not map to UTF-8 directly. For these case, or when you known that the string only contains ASCII only (with 100% certainty), you should use .

Uuid128

Slice Uuid128(Guid value)

Return a fixed size, big-endian encoded, 128-bit UUID

Slices produced by this method ARE ordered lexicographically

Slice Uuid128(Uuid128 value)

Return a fixed size, big-endian encoded, 128-bit UUID

Slices produced by this method ARE ordered lexicographically

Uuid64

Slice Uuid64(Uuid64 value)

Return a fixed size, big-endian encoded, 64-bit UUID

Slices produced by this method ARE ordered lexicographically