Skip to content

Commit

Permalink
WIP on fixing some benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
halgari committed Jan 23, 2024
1 parent 5c6e4b9 commit ffcbcf3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions benchmarks/NexusMods.EventSourcing.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@

#if DEBUG
var benchmarks = new EntityContextBenchmarks();
benchmarks.EventCount = 10000;
benchmarks.EntityCount = 10;
benchmarks.EventCount = 100;
benchmarks.EntityCount = 1000;
benchmarks.EventStoreType = typeof(RocksDBEventStore<BinaryEventSerializer>);
benchmarks.Setup();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ public int ReadFrom(ref ReadOnlySpan<byte> input, ISerializationRegistry registr

for (var idx = 0; idx < count; idx++)
{
var written = registry.Deserialize(data, out TKey key);
data = data.Slice(written);
written = registry.Deserialize(data, out EntityId<TOther> value);
data = data.Slice(written);
var amountRead = registry.Deserialize(data, out TKey key);
data = data.Slice(amountRead);
amountRead = registry.Deserialize(data, out EntityId<TOther> value);
data = data.Slice(amountRead);
_values.Add(key, value);
_keys.Add(value, key);
}
Expand Down
10 changes: 7 additions & 3 deletions src/NexusMods.EventSourcing/AEventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using NexusMods.EventSourcing.Abstractions;
using NexusMods.EventSourcing.Abstractions.Serialization;
using NexusMods.EventSourcing.Serialization;
using Reloaded.Memory.Extensions;

namespace NexusMods.EventSourcing;
Expand All @@ -12,7 +13,7 @@ namespace NexusMods.EventSourcing;
/// </summary>
public abstract class AEventStore : IEventStore
{
private readonly IVariableSizeSerializer<string> _stringSerializer;
private readonly StringSerializer _stringSerializer;
private readonly IFixedSizeSerializer<EntityDefinition> _entityDefinitionSerializer;
private readonly ISerializationRegistry _serializationRegistry;
private readonly PooledMemoryBufferWriter _writer;
Expand All @@ -23,7 +24,7 @@ public abstract class AEventStore : IEventStore
/// <param name="serializationRegistry"></param>
public AEventStore(ISerializationRegistry serializationRegistry)
{
_stringSerializer = (serializationRegistry.GetSerializer(typeof(string)) as IVariableSizeSerializer<string>)!;
_stringSerializer = (StringSerializer)(serializationRegistry.GetSerializer(typeof(string)) as IVariableSizeSerializer<string>)!;
_entityDefinitionSerializer = (serializationRegistry.GetSerializer(typeof(EntityDefinition)) as IFixedSizeSerializer<EntityDefinition>)!;
_serializationRegistry = serializationRegistry;
_writer = new PooledMemoryBufferWriter();
Expand All @@ -41,6 +42,9 @@ public AEventStore(ISerializationRegistry serializationRegistry)
protected bool DeserializeSnapshot(out IAccumulator loadedDefinition,
out (IAttribute Attribute, IAccumulator Accumulator)[] loadedAttributes, ReadOnlySpan<byte> snapshot)
{

var snapshotOld = snapshot;

var entityDefinition = _entityDefinitionSerializer.Deserialize(snapshot.SliceFast(0, 18));

var typeAccumulator = IEntity.TypeAttribute.CreateAccumulator();
Expand Down Expand Up @@ -72,7 +76,7 @@ protected bool DeserializeSnapshot(out IAccumulator loadedDefinition,
snapshot = snapshot.SliceFast(read);

if (!attributes.TryGetValue(attributeName, out var attribute))
throw new Exception("Entity definition does not match the current structure registry.");
throw new Exception($"Entity definition does not match the current structure registry. No definition for {attributeName} found, valid options are {string.Join(",", attributes.Keys)}");

var accumulator = attribute.CreateAccumulator();

Expand Down
2 changes: 2 additions & 0 deletions src/NexusMods.EventSourcing/PooledMemoryBufferWriter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Reloaded.Memory.Extensions;

Expand All @@ -14,6 +15,7 @@ public sealed class PooledMemoryBufferWriter : IBufferWriter<byte>
private Memory<byte> _data;
private int _idx;
private int _size;
private readonly bool _isActive;

/// <summary>
/// Constructs a new pooled memory buffer writer, with the given initial capacity.
Expand Down
19 changes: 9 additions & 10 deletions src/NexusMods.EventSourcing/Serialization/ArraySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,12 @@ public override int Deserialize(ReadOnlySpan<byte> from, out TItem[] value)
from = from.SliceFast(lengthSize);
for (var i = 0; i < length; i++)
{
array[i] = itemSerializer.Deserialize(from.SliceFast(i * itemSize, itemSize));
array[i] = itemSerializer.Deserialize(from.SliceFast(0, itemSize));
from = from.SliceFast(itemSize);
}

value = array;
return lengthSize + itemSize * length;
return lengthSize + (itemSize * length);
}
}

Expand All @@ -113,7 +114,7 @@ public override int Deserialize(ReadOnlySpan<byte> from, out TItem[] value)
/// <param name="itemSerializer"></param>
/// <typeparam name="TItem"></typeparam>
/// <typeparam name="TItemSerializer"></typeparam>
public class VariableItemSizeSerializer<TItem, TItemSerializer>(TItemSerializer itemSerializer) : IVariableSizeSerializer<TItem[]>
public class VariableItemSizeSerializer<TItem, TItemSerializer>(TItemSerializer itemSerializer) : AVariableSizeSerializer<TItem[]>
where TItemSerializer : IVariableSizeSerializer<TItem>
{
/// <inheritdoc />
Expand All @@ -135,11 +136,9 @@ public bool TryGetFixedSize(Type valueType, out int size)
}

/// <inheritdoc />
public void Serialize<TWriter>(TItem[] value, TWriter output) where TWriter : IBufferWriter<byte>
public override void Serialize<TWriter>(TItem[] value, TWriter output)
{
var span = output.GetSpan(sizeof(ushort));
BinaryPrimitives.WriteUInt16BigEndian(span, (ushort)value.Length);
output.Advance(sizeof(ushort));
WriteLength(output, value.Length);

foreach (var item in value)
{
Expand All @@ -148,12 +147,12 @@ public void Serialize<TWriter>(TItem[] value, TWriter output) where TWriter : IB
}

/// <inheritdoc />
public int Deserialize(ReadOnlySpan<byte> from, out TItem[] value)
public override int Deserialize(ReadOnlySpan<byte> from, out TItem[] value)
{
var size = BinaryPrimitives.ReadUInt16BigEndian(from);
var lengthSize = ReadLength(from, out var size);
var array = GC.AllocateUninitializedArray<TItem>(size);

var offset = sizeof(ushort);
var offset = lengthSize;
for (var i = 0; i < size; i++)
{
offset += itemSerializer.Deserialize(from.SliceFast(offset), out var item);
Expand Down

0 comments on commit ffcbcf3

Please sign in to comment.