Giter Club home page Giter Club logo

binarybuffers's Introduction

BinaryBuffers

logo

NuGet

BinaryBuffers offers a highly performant implementation of BinaryReader and BinaryWriter, working directly on a byte array, thus eliminating the need for an intermediate Stream object.

How to use

BinaryBufferReader and BinaryBufferWriter are the respective names of the reader and writer. Both classes operate on a byte[] as its underlying data buffer.

// Provide a buffer to the reader/writer
var buffer = new byte[100];

// Write to the buffer
var writer = new BinaryBufferWriter(buffer);

writer.Write(2022);
writer.Write(8.11);

// Read from the buffer
var reader = new BinaryBufferReader(buffer);

var year = reader.ReadInt32();
var time = reader.ReadDouble();

Additional Goodies

Use StreamBufferWriter as a drop in replacement for BinaryWriter to gain ~10% improvement in performance.

Use StreamBufferReader as a drop in replacement for BinaryReader. Note that there is no performance benefit in using StreamBufferReader, it just helps widen the use of IBufferReader.

Use ResetBuffer method in BinaryBufferReader and BinaryBufferWriter instead of creating a new one and have less allocations!

Benchmarks

Benchmarks shows up to 92% improvement in writing and 84% in reading.

BinaryBufferReader
Method Mean Error StdDev Ratio
BinaryReader_ReadInt 42.23 ms 0.1487 ms 0.1318 ms baseline
BufferReader_ReadInt 5.53 ms 0.0265 ms 0.0221 ms -89%
BinaryReader_ReadDecimal 48.28 ms 0.2038 ms 0.1906 ms baseline
BufferReader_ReadDecimal 34.75 ms 0.3921 ms 0.3476 ms -28%
BinaryReader_ReadFloat 25.76 ms 0.1012 ms 0.0947 ms baseline
BufferReader_ReadFloat 3.75 ms 0.0209 ms 0.0195 ms -92%
BinaryBufferWriter
Method Mean Error StdDev Ratio
BinaryWriter_WriteInt 62.71 ms 0.5090 ms 0.4761 ms baseline
BufferWriter_WriteInt 11.05 ms 0.0307 ms 0.0240 ms -77%
BinaryWriter_WriteDecimal 42.07 ms 0.1556 ms 0.1455 ms baseline
BufferWriter_WriteDecimal 7.79 ms 0.0191 ms 0.0169 ms -84%
BinaryWriter_WriteFloat 33.38 ms 0.1869 ms 0.1561 ms baseline
BufferWriter_WriteFloat 7.79 ms 0.0191 ms 0.0169 ms -84%

Performance tests were generated using .NET 7.0.5 on:

AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores

binarybuffers's People

Contributors

salarcode avatar silkfire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

radtek cmadh

binarybuffers's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.