You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ByteBuffer - Reduce Allocations for Serialization Path #141
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.
changed the title [-]ByteBuffer — Massive Allocation Waste on Hot Serialization Path[/-][+]ByteBuffer — Reduce Allocations for Serialization Path[/+]on Mar 22, 2026
changed the title [-]ByteBuffer — Reduce Allocations for Serialization Path[/-][+]ByteBuffer - Reduce Allocations for Serialization Path[/+]on Mar 22, 2026
Activity
leecampbell-codeagent commentedon Mar 20, 2026
PR created: #155
plan(HdrHistogram#141): initial brief from issue
plan(HdrHistogram#141): review brief
plan(HdrHistogram#141): create task breakdown
feat(HdrHistogram#141): implement tasks
feat(HdrHistogram#141): complete implementation
bench(HdrHistogram#141): add end-to-end serialisation benchmark
[-]ByteBuffer — Massive Allocation Waste on Hot Serialization Path[/-][+]ByteBuffer — Reduce Allocations for Serialization Path[/+][-]ByteBuffer — Reduce Allocations for Serialization Path[/-][+]ByteBuffer - Reduce Allocations for Serialization Path[/+]feat(HdrHistogram#141): replace BitConverter allocations with BinaryP…