Skip to content

Commit

Permalink
Perf tweaks in Ude / MemoryStreamFast
Browse files Browse the repository at this point in the history
  • Loading branch information
FenPhoenix committed Aug 20, 2023
1 parent 4ec7ec2 commit a265256
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 39 deletions.
58 changes: 30 additions & 28 deletions Ude/MemoryStreamFast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ namespace Ude.NetStandard;
public sealed class MemoryStreamFast
{
public byte[] Buffer;
private int _position;
private int _length;

/// <summary>Do not modify!</summary>
public int Position;

/// <summary>Do not modify!</summary>
public int Length;

private int _capacity;

public MemoryStreamFast(int capacity)
Expand Down Expand Up @@ -92,9 +97,9 @@ public int Capacity
if (value > 0)
{
byte[] dst = new byte[value];
if (_length > 0)
if (Length > 0)
{
System.Buffer.BlockCopy(Buffer, 0, dst, 0, _length);
System.Buffer.BlockCopy(Buffer, 0, dst, 0, Length);
}
Buffer = dst;
}
Expand All @@ -106,11 +111,7 @@ public int Capacity
}
}

/// <summary>Gets the length of the stream in bytes.</summary>
/// <returns>The length of the stream in bytes.</returns>
/// <exception cref="T:System.ObjectDisposedException">The stream is closed.</exception>
internal int Length => _length;

#if false
/// <summary>Gets or sets the current position within the stream.</summary>
/// <returns>The current position within the stream.</returns>
/// <exception cref="T:System.ArgumentOutOfRangeException">The position is set to a negative value or a value greater than <see cref="F:System.Int32.MaxValue" />.</exception>
Expand All @@ -129,6 +130,7 @@ internal long Position
}
#endif
}
#endif

/// <summary>Sets the length of the current stream to the specified value.</summary>
/// <param name="value">The value at which to set the length.</param>
Expand All @@ -144,16 +146,16 @@ internal void SetLength(long value)
ThrowHelper.ArgumentOutOfRange(nameof(value), "StreamLength");
}
int num = (int)value;
if (!EnsureCapacity(num) && num > _length)
if (!EnsureCapacity(num) && num > Length)
{
Array.Clear(Buffer, _length, num - _length);
Array.Clear(Buffer, Length, num - Length);
}
_length = num;
if (_position <= num)
Length = num;
if (Position <= num)
{
return;
}
_position = num;
Position = num;
}

/// <summary>Writes a block of bytes to the current stream using data read from a buffer.</summary>
Expand Down Expand Up @@ -183,35 +185,35 @@ internal void Write(byte[] buffer, int offset, int count)
{
ThrowHelper.ArgumentException("Argument_InvalidOffLen");
}
int num1 = _position + count;
int num1 = Position + count;
if (num1 < 0)
{
ThrowHelper.IOException("StreamTooLong");
}
if (num1 > _length)
if (num1 > Length)
{
bool flag = _position > _length;
bool flag = Position > Length;
if (num1 > _capacity && EnsureCapacity(num1))
{
flag = false;
}
if (flag)
{
Array.Clear(Buffer, _length, num1 - _length);
Array.Clear(Buffer, Length, num1 - Length);
}
_length = num1;
Length = num1;
}
if (count <= 8 && buffer != Buffer)
{
int num2 = count;
while (--num2 >= 0)
Buffer[_position + num2] = buffer[offset + num2];
Buffer[Position + num2] = buffer[offset + num2];
}
else
{
System.Buffer.BlockCopy(buffer, offset, Buffer, _position, count);
System.Buffer.BlockCopy(buffer, offset, Buffer, Position, count);
}
_position = num1;
Position = num1;
}

/// <summary>Writes a byte to the current stream at the current position.</summary>
Expand All @@ -222,21 +224,21 @@ internal void Write(byte[] buffer, int offset, int count)
/// <exception cref="T:System.ObjectDisposedException">The current stream is closed.</exception>
internal void WriteByte(byte value)
{
if (_position >= _length)
if (Position >= Length)
{
int num = _position + 1;
bool flag = _position > _length;
int num = Position + 1;
bool flag = Position > Length;
if (num >= _capacity && EnsureCapacity(num))
{
flag = false;
}
if (flag)
{
Array.Clear(Buffer, _length, _position - _length);
Array.Clear(Buffer, Length, Position - Length);
}
_length = num;
Length = num;
}
Buffer[_position++] = value;
Buffer[Position++] = value;
}

internal byte this[int index] => Buffer[index];
Expand Down
6 changes: 3 additions & 3 deletions Ude/SBCharsetProber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ internal override ProbingState HandleData(byte[] buf, int offset, int len, UdeCo

for (int i = offset; i < max; i++)
{
byte order = _model.GetOrder(buf[i]);
byte order = _model.CharToOrderMap[buf[i]];

if (order < SYMBOL_CAT_ORDER)
{
Expand All @@ -102,11 +102,11 @@ internal override ProbingState HandleData(byte[] buf, int offset, int len, UdeCo
_totalSeqs++;
if (!_reversed)
{
++_seqCounters[_model.GetPrecedence((_lastOrder * SAMPLE_SIZE) + order)];
++_seqCounters[_model.PrecedenceMatrix[(_lastOrder * SAMPLE_SIZE) + order]];
}
else // reverse the order of the letters in the lookup
{
++_seqCounters[_model.GetPrecedence((order * SAMPLE_SIZE) + _lastOrder)];
++_seqCounters[_model.PrecedenceMatrix[(order * SAMPLE_SIZE) + _lastOrder]];
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions Ude/SequenceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,13 @@ namespace Ude.NetStandard;
internal abstract class SequenceModel
{
// [256] table use to find a char's order
private readonly byte[] _charToOrderMap;
/// <summary>Do not modify!</summary>
public readonly byte[] CharToOrderMap;

// [SAMPLE_SIZE][SAMPLE_SIZE] table to find a 2-char sequence's
// frequency
private readonly byte[] _precedenceMatrix;
/// <summary>Do not modify!</summary>
public readonly byte[] PrecedenceMatrix;

// freqSeqs / totalSeqs

Expand All @@ -59,13 +61,9 @@ protected SequenceModel(
float typicalPositiveRatio,
Charset charsetName)
{
_charToOrderMap = charToOrderMap;
_precedenceMatrix = precedenceMatrix;
CharToOrderMap = charToOrderMap;
PrecedenceMatrix = precedenceMatrix;
TypicalPositiveRatio = typicalPositiveRatio;
CharsetName = charsetName;
}

internal byte GetOrder(byte b) => _charToOrderMap[b];

internal byte GetPrecedence(int pos) => _precedenceMatrix[pos];
}

0 comments on commit a265256

Please sign in to comment.