Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

ByteBuffer - Reduce Allocations for Serialization Path

Open
#141 1 comment 0 reactions 0 assignees View on GitHub

@LeeCampbell is already working on this.

Since Mar 24, 2026.

  • #165 by @LeeCampbell — open

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
45/100
Issue type
Refactor
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
performance

Research direction

Start in Utilities/ByteBuffer.cs by inspecting PutInt, PutLong, GetInt, and GetLong, then review how the buffer manages its internal storage. Compare before and after allocation behavior using benchmark results. Done means the serialization paths avoid the described per-call allocations and supporting before/after benchmarks are provided; the optional Memory or Span refactor is broader scope.

Written by the indexing model from the issue text.

Description

agent enhancement

File: Utilities/ByteBuffer.cs

PutInt and PutLong both call BitConverter.GetBytes(...) which allocates a new byte[] on every call, then immediately Array.Copy it into the buffer. This is avoidable entirely using BinaryPrimitives and MemoryMarshal from System.Buffers:

// BEFORE - allocates byte[] every call
public void PutLong(long value)
{
    var longAsBytes = BitConverter.GetBytes(IPAddress.NetworkToHostOrder(value));
    Array.Copy(longAsBytes, 0, _internalBuffer, Position, longAsBytes.Length);
    Position += longAsBytes.Length;
}

// AFTER - zero allocation
public void PutLong(long value)
{
BinaryPrimitives.WriteInt64BigEndian(_internalBuffer.AsSpan(Position), value);
Position += sizeof(long);
}

public long GetLong()
{
var result = BinaryPrimitives.ReadInt64BigEndian(_internalBuffer.AsSpan(Position));
Position += sizeof(long);
return result;
}

The IPAddress.HostToNetworkOrder / NetworkToHostOrder dance exists solely to handle big-endian byte ordering — BinaryPrimitives.WriteInt64BigEndian does this directly, with no allocation. This applies equally to PutInt/GetInt. On serialization-heavy workloads this is high-impact.

The ByteBuffer itself could also be refactored to work over Memory<byte> or Span<byte> to allow callers to supply pooled or stack-allocated memory rather than always allocating internally.


Any pull requests addressing this change should provide supporting before and after benchmark results.

Dominant language
C#
Stars
185
Forks
31
PR merge metrics
No merged PRs in 30d

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from HdrHistogram/HdrHistogram.NET

All issues in HdrHistogram/HdrHistogram.NET

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.