From 945686f346ec73293f36377904cafe20ff05ccbc Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 1 Feb 2024 09:32:24 -0800 Subject: [PATCH] Slice span to avoid extra copy in BufferReader --- MetadataExtractor/IO/BufferReader.cs | 30 ++++++++++++---------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/MetadataExtractor/IO/BufferReader.cs b/MetadataExtractor/IO/BufferReader.cs index 455f47b8a..0c298cb61 100644 --- a/MetadataExtractor/IO/BufferReader.cs +++ b/MetadataExtractor/IO/BufferReader.cs @@ -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 bytes = stackalloc byte[2]; - - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 2); + _position += 2; return _isBigEndian ? BinaryPrimitives.ReadUInt16BigEndian(bytes) @@ -85,9 +84,8 @@ public ushort GetUInt16() public short GetInt16() { - Span bytes = stackalloc byte[2]; - - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 2); + _position += 2; return _isBigEndian ? BinaryPrimitives.ReadInt16BigEndian(bytes) @@ -96,9 +94,8 @@ public short GetInt16() public uint GetUInt32() { - Span bytes = stackalloc byte[4]; - - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 4); + _position += 4; return _isBigEndian ? BinaryPrimitives.ReadUInt32BigEndian(bytes) @@ -107,9 +104,8 @@ public uint GetUInt32() public int GetInt32() { - Span bytes = stackalloc byte[4]; - - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 4); + _position += 4; return _isBigEndian ? BinaryPrimitives.ReadInt32BigEndian(bytes) @@ -118,8 +114,8 @@ public int GetInt32() public long GetInt64() { - Span bytes = stackalloc byte[8]; - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 8); + _position += 8; return _isBigEndian ? BinaryPrimitives.ReadInt64BigEndian(bytes) @@ -128,8 +124,8 @@ public long GetInt64() public ulong GetUInt64() { - Span bytes = stackalloc byte[8]; - GetBytes(bytes); + var bytes = _bytes.Slice(_position, 8); + _position += 8; return _isBigEndian ? BinaryPrimitives.ReadUInt64BigEndian(bytes)