nbuff 0.0.4

Network buffer


To use this package, run the following command in your project's root directory:

Manual usage
Put the following dependency into your project's dependences section:

Memory buffer.

Basically buffer is immutable(immutable(ubyte)[])[], but it support some useful Range properties.

The main goal to have Buffer is to minimize data movement when receiving or sending data from/to network.

Buffer supports zero-copy (in most cases) append, split, slice, popFront and popBack, as long as some useful range primitives - find, indexOf, etc.

It allows safe transformation of data received from network without unneccesary data copy. For example you can easily split received HTTP response on headers Buffer and body Buffer, then apply any transformations oh headers and body.

Here is some examples.

    auto b = Buffer();
    b.append("abc");
    b.append("def".representation);
    b.append("123");

In memory this buffer looks like


        | _pos
        v
  +---+---------+
  | 0 | "abc"   |
  +---+---------+
  | 1 | "def"   |
  +---+---------+
  | 2 | "123"   |
  +---+---------+
            ^
            | _end_pos

now let we append one more chunk "456" to Buffer b


         | _pos
         v
  +---+---------+
  | 0 | "abc"   |
  +---+---------+
  | 1 | "def"   |
  +---+---------+
  | 2 | "123"   |
  +---+---------+
  | 3 | "456"   |
  +---+---------+
            ^
            | _end_pos

Let split b on '2', then we will have two buffers:


         | _pos                           | _pos
         v                                v
  +---+---------+                +---+---------+
  | 0 | "abc"   |                | 0 | "123"   |
  +---+---------+                +---+---------+
  | 1 | "def"   |                | 1 | "456"   |
  +---+---------+                +---+---------+
  | 2 | "123"   |                          ^
  +---+---------+                          | _end_pos
          ^
          | _end_pos

So we have two separate Buffers without any data copy.

popFront and popBack just move pos and end_pos. If any of data chunks become unreferenced, then we just pop this chunk, so it can be garbage collected. b.popFrontN(4) will give us

         | _pos
         v
  +---+---------+             | _pos
  | 0 | "abc"   |             v
  +---+---------+     +---+---------+
  | 1 | "def"   |     | 0 | "def    |
  +---+---------+     +---+---------+
  | 2 | "123"   |     | 1 | "123"   |
  +---+---------+     +---+---------+
  | 3 | "456"   |     | 2 | "456"   |
  +---+---------+     +---+---------+
            ^                   ^
            | _end_pos          | _end_pos

Chunk "abc" become unreferenced and can be collected by GC.

Similarly works popBack/popBackN.

Buffer supports next properties

isInputRange!Buffer,
isForwardRange!Buffer,
hasLength!Buffer,
hasSlicing!Buffer,
isBidirectionalRange!Buffer,
isRandomAccessRange!Buffer

so it can be used with a lot of range algorithms, but it supports several optimized methods like find, indexOf.

You can find examples in unittest section of buffer.d

Authors:
  • Igor Khasilev
Dependencies:
none
Versions:
0.1.14 2021-Jun-17
0.1.13 2021-May-25
0.1.12 2020-Jul-16
0.1.11 2020-Jun-17
0.1.10 2020-Jun-16
Show all 22 versions
Download Stats:
  • 0 downloads today

  • 2 downloads this week

  • 18 downloads this month

  • 2268 downloads total

Score:
1.8
Short URL:
nbuff.dub.pm