Skip to content

Commit

Permalink
Minor runtime optimizations and code readability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Amarok79 committed Mar 22, 2024
1 parent d56228d commit 6da5df7
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 138 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>0.73.0</Version>
<Version>0.73.1</Version>
</PropertyGroup>

<PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

#pragma warning disable CA1822 // Mark members as static

Expand Down Expand Up @@ -32,7 +32,6 @@ AnyValue value
AnyValue.ValuesOneofCase.Uint32 => value.Uint32,
AnyValue.ValuesOneofCase.Uint64 => value.Uint64,
AnyValue.ValuesOneofCase.Decimal => _DeserializeDecimal(value.Decimal),
AnyValue.ValuesOneofCase.None => throw _MakeUnexpectedCaseException(value.ValuesCase),
_ => throw _MakeUnexpectedCaseException(value.ValuesCase),
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
// Copyright (c) 2023, Olaf Kober <[email protected]>
// Copyright (c) 2024, Olaf Kober <[email protected]>

namespace Amarok.Diagnostics.Persistence.Tracing.Reader.Internal;


internal abstract class InterningMapBase<T>
where T : class
{
private readonly Int32 mCapacity;

private T?[] mArray;


protected InterningMapBase(
Int32 capacity
)
{
mCapacity = capacity;
mArray = new T[capacity];
}


public void Reset()
{
mArray = new T[mCapacity];
Array.Clear(mArray, 0, mArray.Length);
}


Expand All @@ -34,10 +31,9 @@ T item
if (id >= mArray.Length)
{
_ResizeTo(id);

mArray[id] = item;
}
else if (mArray[id] == null)

if (mArray[id] == null)
{
mArray[id] = item;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,41 +40,43 @@ public void Serialize(
Object? value
)
{
if (value is null)
switch (value)
{
any.Null = false;
}
else if (value is String stringValue)
{
_SerializeString(any, stringValue);
}
else if (value is Boolean booleanValue)
{
any.Bool = booleanValue;
}
else if (value is Int32 int32Value)
{
any.Int32 = int32Value;
}
else if (value is Int64 int64Value)
{
any.Int64 = int64Value;
}
else if (value is Double doubleValue)
{
any.Double = doubleValue;
}
else if (value is DateTime dateTimeValue)
{
_SerializeDateTime(any, dateTimeValue);
}
else if (value is DateTimeOffset dateTimeOffset)
{
_SerializeDateTimeOffset(any, dateTimeOffset);
}
else
{
_SerializeSlow(any, value);
case null:
any.Null = false;
break;

case String stringValue:
_SerializeString(any, stringValue);
break;

case Boolean booleanValue:
any.Bool = booleanValue;
break;

case Int32 int32Value:
any.Int32 = int32Value;
break;

case Int64 int64Value:
any.Int64 = int64Value;
break;

case Double doubleValue:
any.Double = doubleValue;
break;

case DateTime dateTimeValue:
_SerializeDateTime(any, dateTimeValue);
break;

case DateTimeOffset dateTimeOffset:
_SerializeDateTimeOffset(any, dateTimeOffset);
break;

default:
_SerializeSlow(any, value);
break;
}
}

Expand All @@ -84,84 +86,92 @@ private void _SerializeSlow(
Object value
)
{
if (value is Byte byteValue)
{
any.Int32 = byteValue;
}
else if (value is Guid guidValue)
{
_SerializeGuid(any, guidValue);
}
else if (value is UInt16 uint16Value)
{
any.Uint32 = uint16Value;
}
else if (value is UInt32 uint32Value)
{
any.Uint32 = uint32Value;
}
else if (value is UInt64 uint64Value)
{
any.Uint64 = uint64Value;
}
else if (value is SByte sbyteValue)
{
any.Int32 = sbyteValue;
}
else if (value is Int16 int16Value)
{
any.Int32 = int16Value;
}
else if (value is Byte[] byteArray)
{
var count = Math.Min(byteArray.Length, mMaxBytesLength);
any.Bytes = ByteString.CopyFrom(byteArray, 0, count);
}
else if (value is Memory<Byte> byteMemory)
switch (value)
{
var span = byteMemory.Span;
var count = Math.Min(span.Length, mMaxBytesLength);
case Byte byteValue:
any.Int32 = byteValue;
break;

any.Bytes = ByteString.CopyFrom(span[..count]);
}
else if (value is ReadOnlyMemory<Byte> readOnlyByteMemory)
{
var span = readOnlyByteMemory.Span;
var count = Math.Min(span.Length, mMaxBytesLength);
case Guid guidValue:
_SerializeGuid(any, guidValue);
break;

any.Bytes = ByteString.CopyFrom(span[..count]);
}
else if (value is DateOnly dateOnlyValue)
{
_SerializeDateOnly(any, dateOnlyValue);
}
else if (value is TimeOnly timeOnlyValue)
{
_SerializeTimeOnly(any, timeOnlyValue);
}
else if (value is Half halfValue)
{
any.Double = (Double)halfValue;
}
else if (value is Single singleValue)
{
any.Double = singleValue;
}
else if (value is Char charValue)
{
any.String = charValue.ToString();
}
else if (value is Decimal decimalValue)
{
_SerializeDecimal(any, decimalValue);
}
else if (value is DBNull)
{
any.Null = false;
}
else
{
_SerializeObject(any, value);
case UInt16 uint16Value:
any.Uint32 = uint16Value;
break;

case UInt32 uint32Value:
any.Uint32 = uint32Value;
break;

case UInt64 uint64Value:
any.Uint64 = uint64Value;
break;

case SByte sbyteValue:
any.Int32 = sbyteValue;
break;

case Int16 int16Value:
any.Int32 = int16Value;
break;

case Byte[] byteArray:
{
var count = Math.Min(byteArray.Length, mMaxBytesLength);
any.Bytes = ByteString.CopyFrom(byteArray, 0, count);
break;
}

case Memory<Byte> byteMemory:
{
var span = byteMemory.Span;
var count = Math.Min(span.Length, mMaxBytesLength);

any.Bytes = ByteString.CopyFrom(span[..count]);
break;
}

case ReadOnlyMemory<Byte> readOnlyByteMemory:
{
var span = readOnlyByteMemory.Span;
var count = Math.Min(span.Length, mMaxBytesLength);

any.Bytes = ByteString.CopyFrom(span[..count]);
break;
}

case DateOnly dateOnlyValue:
_SerializeDateOnly(any, dateOnlyValue);
break;

case TimeOnly timeOnlyValue:
_SerializeTimeOnly(any, timeOnlyValue);
break;

case Half halfValue:
any.Double = (Double)halfValue;
break;

case Single singleValue:
any.Double = singleValue;
break;

case Char charValue:
any.String = charValue.ToString();
break;

case Decimal decimalValue:
_SerializeDecimal(any, decimalValue);
break;

case DBNull:
any.Null = false;
break;

default:
_SerializeObject(any, value);
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2024, Olaf Kober <[email protected]>

using System.Buffers;
using System.Buffers.Binary;
using System.Diagnostics;
using System.IO.Compression;
Expand Down Expand Up @@ -283,7 +284,7 @@ String filePath

using (var compressed = new DeflateStream(target, CompressionLevel.Fastest))
{
var bytes = new Byte[8192];
var bytes = ArrayPool<Byte>.Shared.Rent(8192);

while (true)
{
Expand All @@ -296,6 +297,8 @@ String filePath

compressed.Write(bytes, 0, read);
}

ArrayPool<Byte>.Shared.Return(bytes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal abstract class SerializerBase<T>
private readonly Int32 mMaxNumberOfItems;
private readonly ObjectsPool mObjectsPool;

private readonly Dictionary<T, Int32> mItems = new();
private readonly Dictionary<T, Int32> mItems;
private Int32 mNextId = InitialId;


Expand All @@ -26,6 +26,7 @@ protected SerializerBase(
ObjectsPool objectsPool
)
{
mItems = new Dictionary<T, Int32>(maxNumberOfItems);
mMaxNumberOfItems = maxNumberOfItems;
mObjectsPool = objectsPool;
}
Expand Down Expand Up @@ -79,11 +80,7 @@ TraceRecords records
_ResetDueToOverrun(records);
}

var id = mNextId;

mNextId++;

return id;
return mNextId++;
}

private void _ResetDueToOverrun(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ private Int32 _InternKey(
TraceRecords records
)
{
if (mKeys.TryGetValue(key, out var id))
if (!mKeys.TryGetValue(key, out var id))
{
return id;
id = _InternKeySlow(key, records);
}

return _InternKeySlow(key, records);
return id;
}

private Int32 _InternKeySlow(
Expand All @@ -98,11 +98,7 @@ TraceRecords records
_ResetDueToOverrun(records);
}

var id = mNextId;

mNextId++;

return id;
return mNextId++;
}

private void _ResetDueToOverrun(
Expand Down
Loading

0 comments on commit 6da5df7

Please sign in to comment.