Skip to content

Commit

Permalink
MemoryStreamFast cleanup and perf
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed Sep 18, 2023
1 parent 1467340 commit a7f0c78
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 75 deletions.
2 changes: 1 addition & 1 deletion Ude/Latin1Prober.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ internal override ProbingState HandleData(byte[] buf, int offset, int len, UdeCo

for (int i = 0; i < context.MemoryStream.Length; i++)
{
byte charClass = _latin1_CharToClass[context.MemoryStream[i]];
byte charClass = _latin1_CharToClass[context.MemoryStream.Buffer[i]];
byte freq = _latin1ClassModel[(_lastCharClass * CLASS_NUM) + charClass];
if (freq == 0)
{
Expand Down
80 changes: 6 additions & 74 deletions Ude/MemoryStreamFast.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Runtime.CompilerServices;
using AL_Common;
using System.Runtime.CompilerServices;

namespace Ude.NetStandard;

Expand All @@ -17,17 +15,11 @@ public sealed class MemoryStreamFast

public MemoryStreamFast(int capacity)
{
if (capacity < 0)
{
ThrowHelper.ArgumentOutOfRange(nameof(capacity), "NegativeCapacity");
}

Buffer = new byte[capacity];

_capacity = capacity;
}

private bool EnsureCapacity(int value)
private void EnsureCapacity(int value)
{
if (value > _capacity)
{
Expand All @@ -47,11 +39,7 @@ private bool EnsureCapacity(int value)
}
Buffer = newBuffer;
_capacity = newCapacity;

return true;
}

return false;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -62,59 +50,19 @@ internal void ResetToCapacity(int capacity)
_position = 0;
}

/// <summary>Writes a block of bytes to the current stream using data read from a buffer.</summary>
/// <param name="buffer">The buffer to write data from.</param>
/// <param name="offset">The zero-based byte offset in <paramref name="buffer" /> at which to begin copying bytes to the current stream.</param>
/// <param name="count">The maximum number of bytes to write.</param>
/// <exception cref="T:System.NotSupportedException">The stream does not support writing. For additional information see <see cref="P:System.IO.Stream.CanWrite" />.
/// -or-
/// The current position is closer than <paramref name="count" /> bytes to the end of the stream, and the capacity cannot be modified.</exception>
/// <exception cref="T:System.ArgumentException">
/// <paramref name="offset" /> subtracted from the buffer length is less than <paramref name="count" />.</exception>
/// <exception cref="T:System.ArgumentOutOfRangeException">
/// <paramref name="offset" /> or <paramref name="count" /> are negative.</exception>
/// <exception cref="T:System.IO.IOException">An I/O error occurs.</exception>
/// <exception cref="T:System.ObjectDisposedException">The current stream instance is closed.</exception>
internal void Write(byte[] buffer, int offset, int count)
{
if (offset < 0)
{
ThrowHelper.ArgumentOutOfRange(nameof(offset), "NeedNonNegNum");
}
if (count < 0)
{
ThrowHelper.ArgumentOutOfRange(nameof(count), "NeedNonNegNum");
}
if (buffer.Length - offset < count)
{
ThrowHelper.ArgumentException("Argument_InvalidOffLen");
}

int i = _position + count;
if (i < 0)
{
ThrowHelper.IOException("StreamTooLong");
}

if (i > Length)
{
bool mustZero = _position > Length;
if (i > _capacity && EnsureCapacity(i))
{
mustZero = false;
}
if (mustZero)
{
Array.Clear(Buffer, Length, i - Length);
}
if (i > _capacity) EnsureCapacity(i);
Length = i;
}
if (count <= 8 && buffer != Buffer)
{
int byteCount = count;
while (--byteCount >= 0)
while (--count >= 0)
{
Buffer[_position + byteCount] = buffer[offset + byteCount];
Buffer[_position + count] = buffer[offset + count];
}
}
else
Expand All @@ -124,30 +72,14 @@ internal void Write(byte[] buffer, int offset, int count)
_position = i;
}

/// <summary>Writes a byte to the current stream at the current position.</summary>
/// <param name="value">The byte to write.</param>
/// <exception cref="T:System.NotSupportedException">The stream does not support writing. For additional information see <see cref="P:System.IO.Stream.CanWrite" />.
/// -or-
/// The current position is at the end of the stream, and the capacity cannot be modified.</exception>
/// <exception cref="T:System.ObjectDisposedException">The current stream is closed.</exception>
internal void WriteByte(byte value)
{
if (_position >= Length)
{
int newLength = _position + 1;
bool mustZero = _position > Length;
if (newLength >= _capacity && EnsureCapacity(newLength))
{
mustZero = false;
}
if (mustZero)
{
Array.Clear(Buffer, Length, _position - Length);
}
if (newLength >= _capacity) EnsureCapacity(newLength);
Length = newLength;
}
Buffer[_position++] = value;
}

internal byte this[int index] => Buffer[index];
}

0 comments on commit a7f0c78

Please sign in to comment.