Skip to content

Commit 68ab733

Browse files
authored
Merge pull request #1 from alrun3/main
perf: use `Seek` when possible to skip bytes efficiently
2 parents 62d0e4f + 65dda2a commit 68ab733

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

W3GNET/Extensions/BinaryReaderExtension.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@ internal static class BinaryReaderExtension
1111
{
1212
internal static void SkipBytes(this BinaryReader reader, uint count)
1313
{
14-
for (int i = 0; i < count; i++)
14+
var stream = reader.BaseStream;
15+
if (stream.CanSeek)
1516
{
16-
if (reader.BaseStream.Length <= reader.BaseStream.Position)
17+
stream.Seek(Math.Min(count, stream.Length - stream.Position), SeekOrigin.Current);
18+
}
19+
else
20+
{
21+
for (int i = 0; i < count; i++)
1722
{
18-
return;
19-
}
23+
if (stream.Length <= stream.Position)
24+
{
25+
return;
26+
}
2027

21-
reader.ReadByte();
28+
_ = reader.ReadByte();
29+
}
2230
}
2331
}
2432

0 commit comments

Comments
 (0)