Using cbor writer without releasing memory
This commit is contained in:
@@ -31,7 +31,7 @@ Console.WriteLine("End waiting");
|
|||||||
var totalRequests = tasks.Sum(i => i.Result);
|
var totalRequests = tasks.Sum(i => i.Result);
|
||||||
Console.WriteLine($"Total requests: {totalRequests:N0}");
|
Console.WriteLine($"Total requests: {totalRequests:N0}");
|
||||||
Console.WriteLine($"Results: {totalRequests / time.TotalSeconds:N} RPS");
|
Console.WriteLine($"Results: {totalRequests / time.TotalSeconds:N} RPS");
|
||||||
File.AppendAllText("results.txt", $"[DIRECT TO EXE RUN] {totalRequests}\r\n");
|
File.AppendAllText("results.txt", $"[SINGLE CBOR WRITER ALLOC] {totalRequests}\r\n");
|
||||||
|
|
||||||
async Task<List<ILoadTest>> GetLoadEndpoints(int count)
|
async Task<List<ILoadTest>> GetLoadEndpoints(int count)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -2,10 +2,9 @@
|
|||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Formats.Cbor;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
using Micial.Cbor.Reader;
|
|
||||||
using Micial.Cbor.Writer;
|
|
||||||
using mROA.Abstract;
|
using mROA.Abstract;
|
||||||
using mROA.Implementation;
|
using mROA.Implementation;
|
||||||
using mROA.Implementation.Attributes;
|
using mROA.Implementation.Attributes;
|
||||||
@@ -15,12 +14,15 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
public class CborSerializationToolkit : IContextualSerializationToolKit
|
public class CborSerializationToolkit : IContextualSerializationToolKit
|
||||||
{
|
{
|
||||||
private readonly IOrdinaryStructureParser[] _parsers = {
|
private readonly CborWriter _writer = new(initialCapacity: 512);
|
||||||
|
|
||||||
|
private readonly IOrdinaryStructureParser[] _parsers =
|
||||||
|
{
|
||||||
new NetworkMessageHeaderParser(), new DefaultCallRequestParser(), new FinalCommandExecutionParser(),
|
new NetworkMessageHeaderParser(), new DefaultCallRequestParser(), new FinalCommandExecutionParser(),
|
||||||
new FinalCommandExecutionResultlessParser()
|
new FinalCommandExecutionResultlessParser()
|
||||||
};
|
};
|
||||||
// private Dictionary<Type, IOrdinaryStructureParser> _parsers = new(){{typeof(NetworkMessageHeader), new NetworkMessageHeaderParser()}, {typeof(DefaultCallRequest), new DefaultCallRequestParser()}, {typeof(FinalCommandExecution<object>), new FinalCommandExecutionParser()}, {typeof(FinalCommandExecution), new FinalCommandExecutionResultlessParser()}}; //
|
|
||||||
private Dictionary<Type, List<PropertyInfo>> _propertiesCache = new();
|
private readonly Dictionary<Type, List<PropertyInfo>> _propertiesCache = new();
|
||||||
public static TimeSpan SerializationTime = TimeSpan.Zero;
|
public static TimeSpan SerializationTime = TimeSpan.Zero;
|
||||||
|
|
||||||
private bool FindParser(Type t, out IOrdinaryStructureParser parser)
|
private bool FindParser(Type t, out IOrdinaryStructureParser parser)
|
||||||
@@ -52,22 +54,28 @@ namespace mROA.Cbor
|
|||||||
parser = null;
|
parser = null;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
|
||||||
{
|
{
|
||||||
var sw = Stopwatch.StartNew();
|
byte[] result;
|
||||||
var writer = new CborWriter(initialCapacity:64);
|
lock (_writer)
|
||||||
WriteData(objectToSerialize, writer, context);
|
{
|
||||||
var result = writer.Encode();
|
_writer.Reset();
|
||||||
sw.Stop();
|
WriteData(objectToSerialize, _writer, context);
|
||||||
SerializationTime = SerializationTime.Add(sw.Elapsed);
|
result = _writer.Encode();
|
||||||
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int Serialize(object objectToSerialize, Span<byte> destination, IEndPointContext context)
|
public int Serialize(object objectToSerialize, Span<byte> destination, IEndPointContext context)
|
||||||
{
|
{
|
||||||
var writer = new CborWriter(initialCapacity:64);
|
lock (_writer)
|
||||||
WriteData(objectToSerialize, writer, context);
|
{
|
||||||
return writer.Encode(destination);
|
_writer.Reset();
|
||||||
|
WriteData(objectToSerialize, _writer, context);
|
||||||
|
return _writer.Encode(destination);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public T Deserialize<T>(byte[] rawData, IEndPointContext? context)
|
public T Deserialize<T>(byte[] rawData, IEndPointContext? context)
|
||||||
@@ -116,50 +124,6 @@ namespace mROA.Cbor
|
|||||||
return Convert.ChangeType(nonCasted, type);
|
return Convert.ChangeType(nonCasted, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Inject(object dependency)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Serialize<T>(T objectToSerialize)
|
|
||||||
{
|
|
||||||
return Serialize(objectToSerialize, typeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte[] Serialize(object objectToSerialize, Type type)
|
|
||||||
{
|
|
||||||
return Serialize(objectToSerialize, context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Deserialize<T>(byte[] rawData)
|
|
||||||
{
|
|
||||||
return Deserialize<T>(rawData: rawData, context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public object? Deserialize(byte[] rawData, Type type)
|
|
||||||
{
|
|
||||||
return Deserialize(rawData: rawData, type, context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Deserialize<T>(Span<byte> rawData)
|
|
||||||
{
|
|
||||||
return Deserialize<T>(rawData.ToArray().AsMemory(), context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public object? Deserialize(Span<byte> rawData, Type type)
|
|
||||||
{
|
|
||||||
return Deserialize(rawData: rawData.ToArray(), type: type);
|
|
||||||
}
|
|
||||||
|
|
||||||
public T Cast<T>(object nonCasted)
|
|
||||||
{
|
|
||||||
return Cast<T>(nonCasted: nonCasted, context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public object Cast(object nonCasted, Type type)
|
|
||||||
{
|
|
||||||
return Cast(nonCasted: nonCasted, type: type, context: null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
|
public void WriteData(object? obj, CborWriter writer, IEndPointContext? context)
|
||||||
{
|
{
|
||||||
if (obj is not null && FindParser(obj.GetType(), out var parser))
|
if (obj is not null && FindParser(obj.GetType(), out var parser))
|
||||||
@@ -167,7 +131,7 @@ namespace mROA.Cbor
|
|||||||
parser.Write(writer, obj, context, this);
|
parser.Write(writer, obj, context, this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (obj)
|
switch (obj)
|
||||||
{
|
{
|
||||||
case int i:
|
case int i:
|
||||||
@@ -289,6 +253,7 @@ namespace mROA.Cbor
|
|||||||
{
|
{
|
||||||
return parser.Read(reader, context, this);
|
return parser.Read(reader, context, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
var state = reader.PeekState();
|
var state = reader.PeekState();
|
||||||
switch (state)
|
switch (state)
|
||||||
{
|
{
|
||||||
@@ -302,8 +267,6 @@ namespace mROA.Cbor
|
|||||||
return reader.ReadInt64();
|
return reader.ReadInt64();
|
||||||
if (type == typeof(uint))
|
if (type == typeof(uint))
|
||||||
return reader.ReadUInt32();
|
return reader.ReadUInt32();
|
||||||
if (type == typeof(ulong))
|
|
||||||
return reader.ReadUInt64();
|
|
||||||
|
|
||||||
return reader.ReadUInt64();
|
return reader.ReadUInt64();
|
||||||
case CborReaderState.ByteString:
|
case CborReaderState.ByteString:
|
||||||
@@ -482,15 +445,15 @@ namespace mROA.Cbor
|
|||||||
public static List<PropertyInfo> FilterProperties(PropertyInfo[] properties)
|
public static List<PropertyInfo> FilterProperties(PropertyInfo[] properties)
|
||||||
{
|
{
|
||||||
var finalProperties = new List<PropertyInfo>(properties.Length);
|
var finalProperties = new List<PropertyInfo>(properties.Length);
|
||||||
|
|
||||||
for (int i = 0; i < properties.Length; i++)
|
for (int i = 0; i < properties.Length; i++)
|
||||||
{
|
{
|
||||||
var property = properties[i];
|
var property = properties[i];
|
||||||
if (property is not { CanRead: true, CanWrite: true } || property.GetCustomAttribute<SerializationIgnoreAttribute>() != null)
|
if (property is not { CanRead: true, CanWrite: true } ||
|
||||||
|
property.GetCustomAttribute<SerializationIgnoreAttribute>() != null)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
finalProperties.Add(property);
|
finalProperties.Add(property);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return finalProperties;
|
return finalProperties;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
using Micial.Cbor.Reader;
|
using System.Formats.Cbor;
|
||||||
using Micial.Cbor.Writer;
|
|
||||||
using mROA.Abstract;
|
using mROA.Abstract;
|
||||||
using mROA.Implementation;
|
using mROA.Implementation;
|
||||||
using mROA.Implementation.CommandExecution;
|
using mROA.Implementation.CommandExecution;
|
||||||
|
|||||||
@@ -28,12 +28,8 @@
|
|||||||
</None>
|
</None>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Reference Include="System.Formats.Cbor.dll"></Reference>
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Micial.Cbor" Version="9.0.7" />
|
<PackageReference Include="System.Formats.Cbor" Version="9.0.7" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user