Skip to content

Commit

Permalink
Merge pull request Azure#265 from szehetner/master
Browse files Browse the repository at this point in the history
Optimize and inline ByteBuffer.Validate
  • Loading branch information
xinchen10 authored Apr 9, 2018
2 parents 4b02981 + d0643e3 commit 5fa4f9d
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 34 deletions.
24 changes: 12 additions & 12 deletions src/AmqpBitConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static class AmqpBitConverter
/// <returns></returns>
public static sbyte ReadByte(ByteBuffer buffer)
{
buffer.Validate(false, FixedWidth.UByte);
buffer.ValidateRead(FixedWidth.UByte);
sbyte data = (sbyte)buffer.Buffer[buffer.Offset];
buffer.Complete(FixedWidth.UByte);
return data;
Expand All @@ -64,7 +64,7 @@ public static byte ReadUByte(ByteBuffer buffer)
/// <returns></returns>
public static short ReadShort(ByteBuffer buffer)
{
buffer.Validate(false, FixedWidth.Short);
buffer.ValidateRead(FixedWidth.Short);
short data = (short)((buffer.Buffer[buffer.Offset] << 8) | buffer.Buffer[buffer.Offset + 1]);
buffer.Complete(FixedWidth.Short);
return data;
Expand All @@ -87,7 +87,7 @@ public static ushort ReadUShort(ByteBuffer buffer)
/// <returns></returns>
public static int ReadInt(ByteBuffer buffer)
{
buffer.Validate(false, FixedWidth.Int);
buffer.ValidateRead(FixedWidth.Int);
int data = ReadInt(buffer.Buffer, buffer.Offset);
buffer.Complete(FixedWidth.Int);
return data;
Expand Down Expand Up @@ -121,7 +121,7 @@ public static uint ReadUInt(ByteBuffer buffer)
/// <returns></returns>
public static long ReadLong(ByteBuffer buffer)
{
buffer.Validate(false, FixedWidth.Long);
buffer.ValidateRead(FixedWidth.Long);
long high = ReadInt(buffer.Buffer, buffer.Offset);
long low = (uint)ReadInt(buffer.Buffer, buffer.Offset + 4);
long data = (high << 32) | low;
Expand Down Expand Up @@ -168,7 +168,7 @@ public static unsafe double ReadDouble(ByteBuffer buffer)
/// <returns></returns>
public static Guid ReadUuid(ByteBuffer buffer)
{
buffer.Validate(false, FixedWidth.Uuid);
buffer.ValidateRead(FixedWidth.Uuid);
byte[] d = new byte[FixedWidth.Uuid];
if (AmqpBitConverter.IsLittleEndian)
{
Expand Down Expand Up @@ -204,7 +204,7 @@ public static Guid ReadUuid(ByteBuffer buffer)
/// <param name="count">The number of bytes to read.</param>
public static void ReadBytes(ByteBuffer buffer, byte[] data, int offset, int count)
{
buffer.Validate(false, count);
buffer.ValidateRead(count);
Array.Copy(buffer.Buffer, buffer.Offset, data, offset, count);
buffer.Complete(count);
}
Expand All @@ -216,7 +216,7 @@ public static void ReadBytes(ByteBuffer buffer, byte[] data, int offset, int cou
/// <param name="data">The data to write.</param>
public static void WriteByte(ByteBuffer buffer, sbyte data)
{
buffer.Validate(true, FixedWidth.Byte);
buffer.ValidateWrite(FixedWidth.Byte);
buffer.Buffer[buffer.WritePos] = (byte)data;
buffer.Append(FixedWidth.Byte);
}
Expand All @@ -238,7 +238,7 @@ public static void WriteUByte(ByteBuffer buffer, byte data)
/// <param name="data">The data to write.</param>
public static void WriteShort(ByteBuffer buffer, short data)
{
buffer.Validate(true, FixedWidth.Short);
buffer.ValidateWrite(FixedWidth.Short);
buffer.Buffer[buffer.WritePos] = (byte)(data >> 8);
buffer.Buffer[buffer.WritePos + 1] = (byte)data;
buffer.Append(FixedWidth.Short);
Expand All @@ -261,7 +261,7 @@ public static void WriteUShort(ByteBuffer buffer, ushort data)
/// <param name="data">The data to write.</param>
public static void WriteInt(ByteBuffer buffer, int data)
{
buffer.Validate(true, FixedWidth.Int);
buffer.ValidateWrite(FixedWidth.Int);
WriteInt(buffer.Buffer, buffer.WritePos, data);
buffer.Append(FixedWidth.Int);
}
Expand Down Expand Up @@ -340,7 +340,7 @@ public static unsafe void WriteDouble(ByteBuffer buffer, double data)
/// <param name="data">The data to write.</param>
public static void WriteUuid(ByteBuffer buffer, Guid data)
{
buffer.Validate(true, FixedWidth.Uuid);
buffer.ValidateWrite(FixedWidth.Uuid);
byte[] p = data.ToByteArray();
int pos = buffer.WritePos;

Expand Down Expand Up @@ -376,9 +376,9 @@ public static void WriteUuid(ByteBuffer buffer, Guid data)
/// <param name="count">The number of bytes to write.</param>
public static void WriteBytes(ByteBuffer buffer, byte[] data, int offset, int count)
{
buffer.Validate(true, count);
buffer.ValidateWrite(count);
Array.Copy(data, offset, buffer.Buffer, buffer.WritePos, count);
buffer.Append(count);
}
}
}
}
71 changes: 49 additions & 22 deletions src/ByteBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,69 @@ public int WritePos
}

/// <summary>
/// Verifies that if the buffer has enough bytes for read or enough room for write and grow the buffer if needed.
/// Verifies that the buffer has enough bytes for read or enough room for write and grow the buffer if needed.
/// </summary>
/// <param name="write">Operation to verify. True for write and false for read.</param>
/// <param name="dataSize">The size to read or write.</param>
public void Validate(bool write, int dataSize)
{
bool valid = false;
if (write)
{
if (this.Size < dataSize && this.autoGrow)
{
int newSize = Math.Max(this.Capacity * 2, this.Capacity + dataSize);
byte[] newBuffer;
int offset;
int count;
this.DuplicateBuffer(newSize, this.write - this.start, out newBuffer, out offset, out count);

int bufferOffset = this.start - offset;
this.buffer = newBuffer;
this.start = offset;
this.read -= bufferOffset;
this.write -= bufferOffset;
this.end = offset + count;
}

valid = this.Size >= dataSize;
ValidateWrite(dataSize);
}
else
{
valid = this.Length >= dataSize;
ValidateRead(dataSize);
}
}

if (!valid)
/// <summary>
/// Verifies that the buffer has enough bytes for read.
/// </summary>
/// <param name="dataSize">The size to read.</param>
public void ValidateRead(int dataSize)
{
if (this.Length < dataSize)
ThrowBufferTooSmallException();
}

/// <summary>
/// Verifies that the buffer has enough room for write and grow the buffer if needed.
/// </summary>
/// <param name="dataSize">The size to write.</param>
public void ValidateWrite(int dataSize)
{
if (this.Size < dataSize)
TryAutoGrowBuffer(dataSize);
}

private void TryAutoGrowBuffer(int dataSize)
{
if (this.Size < dataSize && this.autoGrow)
{
throw new InvalidOperationException("buffer too small");
int newSize = Math.Max(this.Capacity * 2, this.Capacity + dataSize);
byte[] newBuffer;
int offset;
int count;
this.DuplicateBuffer(newSize, this.write - this.start, out newBuffer, out offset, out count);

int bufferOffset = this.start - offset;
this.buffer = newBuffer;
this.start = offset;
this.read -= bufferOffset;
this.write -= bufferOffset;
this.end = offset + count;
}

bool valid = this.Size >= dataSize;

if (!valid)
ThrowBufferTooSmallException();
}

private static void ThrowBufferTooSmallException()
{
throw new InvalidOperationException("buffer too small");
}

/// <summary>
Expand Down

0 comments on commit 5fa4f9d

Please sign in to comment.