Salar.BinaryBuffers 3.4.0

dotnet add package Salar.BinaryBuffers --version 3.4.0
                    
NuGet\Install-Package Salar.BinaryBuffers -Version 3.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Salar.BinaryBuffers" Version="3.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Salar.BinaryBuffers" Version="3.4.0" />
                    
Directory.Packages.props
<PackageReference Include="Salar.BinaryBuffers" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Salar.BinaryBuffers --version 3.4.0
                    
#r "nuget: Salar.BinaryBuffers, 3.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Salar.BinaryBuffers@3.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Salar.BinaryBuffers&version=3.4.0
                    
Install as a Cake Addin
#tool nuget:?package=Salar.BinaryBuffers&version=3.4.0
                    
Install as a Cake Tool

BinaryBuffers

BinaryBuffers logo

NuGet

BinaryBuffers is a high-performance .NET library for reading and writing primitive values directly from binary buffers. It gives you BinaryReader/BinaryWriter-style APIs without requiring an intermediate Stream, which reduces allocations and improves throughput in buffer-heavy workloads.

Why BinaryBuffers?

  • Work directly with byte[] buffers
  • Reuse existing buffers with ResetBuffer(...)
  • Read from ReadOnlyMemory<byte> and ReadOnlySequence<byte>
  • Use shared abstractions through IBufferReader and IBufferWriter
  • Swap in stream-based compatibility types when you still need a Stream

Installation

dotnet add package Salar.BinaryBuffers

Quick start

using Salar.BinaryBuffers;

var buffer = new byte[32];

var writer = new BinaryBufferWriter(buffer);
writer.Write(2022);
writer.Write(8.11);

var bytesWritten = writer.WrittenLength;

var reader = new BinaryBufferReader(buffer, 0, bytesWritten);
var year = reader.ReadInt32();
var value = reader.ReadDouble();

BinarySpanBufferWriter

BinarySpanBufferWriter is a zero-allocation, high-performance writer that operates directly on a Span<byte>. As a ref struct, it can work with stack-allocated memory (stackalloc) for maximum performance with no heap allocations.

// Stack-allocated buffer — no heap allocation
Span<byte> buffer = stackalloc byte[1024];
var writer = new BinarySpanBufferWriter(buffer);

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

// Get the written bytes as a ReadOnlySpan<byte>
ReadOnlySpan<byte> written = writer.ToReadOnlySpan();

BinarySpanBufferWriter implements IBufferWriter and works seamlessly with generic methods:

void Serialize<TBufferWriter>(TBufferWriter writer, int id) where TBufferWriter : IBufferWriter
{
    writer.Write(id);
}

Span<byte> buffer = stackalloc byte[1024];
var writer = new BinarySpanBufferWriter(buffer);
Serialize(writer, 42); // Works via generic constraint — no boxing

Because it is a ref struct, BinarySpanBufferWriter cannot be stored as a class field, used in async methods, or boxed to an interface directly. Use BinaryBufferWriter when those capabilities are needed.

Additional Goodies

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

BinaryBufferWriter

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

using Salar.BinaryBuffers;

var buffer = new byte[128];
var writer = new BinaryBufferWriter(buffer);

writer.Write(42);
writer.Write(123.45m);

writer.ResetBuffer();
writer.Write(7);

BinaryBufferReader

Use BinaryBufferReader to read primitive values from a byte[] or ArraySegment<byte>.

using Salar.BinaryBuffers;

var payload = new byte[16];
var writer = new BinaryBufferWriter(payload);
writer.Write(42);
writer.Write(2.5f);

var reader = new BinaryBufferReader(payload);
var id = reader.ReadInt32();
var amount = reader.ReadSingle();

Additional readers and compatibility types

  • BinaryBufferMemoryReader reads from ReadOnlyMemory<byte>
  • SequenceBufferReader reads from ReadOnlySequence<byte>
  • StreamBufferWriter is a stream-based writer that implements the same writer abstraction
  • StreamBufferReader is a stream-based reader that integrates with the same reader abstraction

This makes it easier to program against IBufferReader and IBufferWriter instead of tying your code to a single storage model.

When to use it

BinaryBuffers is a good fit when you:

  • already own the underlying byte buffer
  • want to avoid wrapping buffers in MemoryStream
  • need predictable, low-allocation binary serialization of primitive values
  • want to reuse the same buffer across repeated operations

Benchmarks

Benchmarks in this repository show substantial improvements for common primitive reads and writes when compared to BinaryReader and BinaryWriter.

Read benchmarks

Lower is better.

Method Mean Error StdDev Relative time
BinaryReader_ReadInt 42.23 ms 0.1487 ms 0.1318 ms 1.00x
BufferReader_ReadInt 5.53 ms 0.0265 ms 0.0221 ms 0.13x
BinaryReader_ReadDecimal 48.28 ms 0.2038 ms 0.1906 ms 1.00x
BufferReader_ReadDecimal 34.75 ms 0.3921 ms 0.3476 ms 0.72x
BinaryReader_ReadFloat 25.76 ms 0.1012 ms 0.0947 ms 1.00x
BufferReader_ReadFloat 3.75 ms 0.0209 ms 0.0195 ms 0.15x

Write benchmarks

Lower is better.

Method Mean Error StdDev Relative time
BinaryWriter_WriteInt 62.71 ms 0.5090 ms 0.4761 ms 1.00x
BufferWriter_WriteInt 11.05 ms 0.0307 ms 0.0240 ms 0.18x
BinaryWriter_WriteDecimal 42.07 ms 0.1556 ms 0.1455 ms 1.00x
BufferWriter_WriteDecimal 7.79 ms 0.0191 ms 0.0169 ms 0.19x
BinaryWriter_WriteFloat 33.38 ms 0.1869 ms 0.1561 ms 1.00x
BufferWriter_WriteFloat 7.79 ms 0.0191 ms 0.0169 ms 0.23x

These benchmark results were last recorded with the benchmark project in this repository using .NET 7.0.5 on:

AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

  • net10.0

    • No dependencies.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Salar.BinaryBuffers:

Package Downloads
Salar.Bois

The most compact, extermly fast binary serializer for .NET Code and .NET Framework. More info: https://github.com/salarcode/Bois

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.4.0 39 3/24/2026
3.3.0 46,754 7/7/2023
3.2.0 16,012 7/1/2023
3.1.0 2,695 11/30/2022
3.0.1 528 11/27/2022
3.0.0 539 11/26/2022
2.0.0 676 7/30/2022
1.1.0 3,010 9/30/2019
1.0.0 766 8/29/2019

* Added BinarySpanBufferWriter: a zero-allocation ref struct writer that operates directly on Span<byte>, with support for stackalloc buffers.