Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/xljiulang/memoryextensions
- Owner: xljiulang
- License: mit
- Created: 2021-01-14T11:28:44.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-17T12:07:47.000Z (almost 4 years ago)
- Last Synced: 2024-10-04T06:25:16.093Z (3 months ago)
- Language: C#
- Homepage:
- Size: 73.2 KB
- Stars: 28
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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();
```