Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xljiulang/memoryextensions

High performance buffer types such as IArrayOwner<T>, RecyclableBufferWriter<T>, ResizableBufferWriter<T>, MemoryBufferWriter<T>, BufferReader
https://github.com/xljiulang/memoryextensions

Last synced: 2 months ago
JSON representation

High performance buffer types such as IArrayOwner<T>, RecyclableBufferWriter<T>, ResizableBufferWriter<T>, MemoryBufferWriter<T>, BufferReader

Awesome Lists containing this project

README

        

## MemoryExtensions  
High performance buffer types such as `IArrayOwner`, `RecyclableBufferWriter`, `ResizableBufferWriter`, `MemoryBufferWriter`, `BufferReader`
```

```
### ArrayPool & IArrayOwner

```
var arrayOwner = ArrayPool.Rent(10);
// var arrayOwner = ArrayPool.Shared.RentArrayOwner(10);
for (var i = 0; i < arrayOwner.Length; i++)
{
arrayOwner.Array[i] = (byte)i;
}

// return the array to pool
arrayOwner.Dispose();
```

### RecyclableBufferWriter

```
var writer = new RecyclableBufferWriter(4);
writer.Write((byte)1);
writer.Write(new byte[] { 2, 3, 4 });
writer.WriteBigEndian(int.MaxValue);
var writtern = writer.WrittenSpan; // 1,2,3,4,127,255,255,255

// return the buffer to pool
writer.Dispose();
```

### ResizableBufferWriter

```
var writer = new ResizableBufferWriter(4);
writer.Write((byte)1);
writer.Write(new byte[] { 2, 3, 4 });
writer.WriteBigEndian(int.MaxValue);
var writtern = writer.WrittenSpan; // 1,2,3,4,127,255,255,255
```

### MemoryBufferWriter & BufferReader
```
var array = new byte[16];

var writer = array.CreateWriter();
// var writer = new MemoryBufferWriter(array);
writer.WriteBigEndian(18);
writer.WriteBigEndian(2.01f);

var reader = new BufferReader(array);
reader.ReadBigEndian(out int v1);
reader.ReadBigEndian(out float v2);
```

### IMemoryOwner Slice
```
var owner = MemoryPool.Shared.Rent(10);
new byte[] { 1, 2, 3, 4 }.CopyTo(owner.Memory.Span);
var sliced = owner.Slice(0, 2); // 1,2

// return the memory to pool
sliced.Dispose();
```