ByteBuffer - Reduce Allocations for Serialization Path
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
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
Starts the project's dev container in your browser, under your own GitHub account.
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from HdrHistogram/HdrHistogram.NET
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
HdrHistogram/HdrHistogram.NET#167 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
HdrHistogram/HdrHistogram.NET#166 ·
-
WriterReaderPhaser.FlipPhase - Task.Yield().GetAwaiter().GetResult() is a Thread Pool Anti-PatternOpenagent enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
HdrHistogram/HdrHistogram.NET#144 ·
-
agent enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
HdrHistogram/HdrHistogram.NET#142 ·
-
Difficulty 3/5 1-2 days Newbie friendliness 48/100
HdrHistogram/HdrHistogram.NET#156 ·
All issues in HdrHistogram/HdrHistogram.NET
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
MicrosoftLearning/PL-400_Microsoft-Power-Platform-Developer#231 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
joinrpg/joinrpg-net#5313 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Maintainers usually reply within 1 day