Skip to content

Commit

Permalink
Slice span to avoid extra copy in BufferReader
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon committed Feb 1, 2024
1 parent 73143a7 commit 945686f
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions MetadataExtractor/IO/BufferReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,13 @@ public bool TryAdvance(int n)

public sbyte GetSByte()
{
return unchecked((sbyte)GetByte());
return unchecked((sbyte)_bytes[_position++]);
}

public ushort GetUInt16()
{
Span<byte> bytes = stackalloc byte[2];

GetBytes(bytes);
var bytes = _bytes.Slice(_position, 2);
_position += 2;

return _isBigEndian
? BinaryPrimitives.ReadUInt16BigEndian(bytes)
Expand All @@ -85,9 +84,8 @@ public ushort GetUInt16()

public short GetInt16()
{
Span<byte> bytes = stackalloc byte[2];

GetBytes(bytes);
var bytes = _bytes.Slice(_position, 2);
_position += 2;

return _isBigEndian
? BinaryPrimitives.ReadInt16BigEndian(bytes)
Expand All @@ -96,9 +94,8 @@ public short GetInt16()

public uint GetUInt32()
{
Span<byte> bytes = stackalloc byte[4];

GetBytes(bytes);
var bytes = _bytes.Slice(_position, 4);
_position += 4;

return _isBigEndian
? BinaryPrimitives.ReadUInt32BigEndian(bytes)
Expand All @@ -107,9 +104,8 @@ public uint GetUInt32()

public int GetInt32()
{
Span<byte> bytes = stackalloc byte[4];

GetBytes(bytes);
var bytes = _bytes.Slice(_position, 4);
_position += 4;

return _isBigEndian
? BinaryPrimitives.ReadInt32BigEndian(bytes)
Expand All @@ -118,8 +114,8 @@ public int GetInt32()

public long GetInt64()
{
Span<byte> bytes = stackalloc byte[8];
GetBytes(bytes);
var bytes = _bytes.Slice(_position, 8);
_position += 8;

return _isBigEndian
? BinaryPrimitives.ReadInt64BigEndian(bytes)
Expand All @@ -128,8 +124,8 @@ public long GetInt64()

public ulong GetUInt64()
{
Span<byte> bytes = stackalloc byte[8];
GetBytes(bytes);
var bytes = _bytes.Slice(_position, 8);
_position += 8;

return _isBigEndian
? BinaryPrimitives.ReadUInt64BigEndian(bytes)
Expand Down

0 comments on commit 945686f

Please sign in to comment.