Faster cbor serialization with caching

This commit is contained in:
2025-07-03 18:13:02 +03:00
parent b27dfa0b0a
commit faf73e7798
10 changed files with 41 additions and 16 deletions
+14 -4
View File
@@ -13,6 +13,7 @@ namespace mROA.Cbor
{
public class CborSerializationToolkit : IContextualSerializationToolKit
{
private static Dictionary<Type, List<PropertyInfo>> _propertiesCache = new();
public static TimeSpan SerializationTime = TimeSpan.Zero;
public byte[] Serialize(object objectToSerialize, IEndPointContext context)
{
@@ -231,7 +232,7 @@ namespace mROA.Cbor
return;
}
var properties = FilterProperties(type.GetProperties());
var properties = GetAvalibleProperties(type);
var values = properties.Select(property => property.GetValue(obj)).ToList();
WriteList(values, writer, context);
}
@@ -402,9 +403,7 @@ namespace mROA.Cbor
private void FillObject(object obj, Type type, CborReader reader, IEndPointContext? context)
{
var propertyInfos = type.GetProperties();
var properties = FilterProperties(propertyInfos);
var properties = GetAvalibleProperties(type);
var length = reader.ReadStartArray();
try
{
@@ -441,5 +440,16 @@ namespace mROA.Cbor
return finalProperties;
}
private List<PropertyInfo> GetAvalibleProperties(Type type)
{
if (_propertiesCache.TryGetValue(type, out var properties))
{
return properties;
}
var propertiesList = FilterProperties(type.GetProperties());
_propertiesCache[type] = propertiesList;
return propertiesList;
}
}
}
+9 -1
View File
@@ -4,8 +4,9 @@
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.0.4</Version>
<Version>2.0.5</Version>
<LangVersion>9</LangVersion>
<PackageIcon>mroaLogo.png</PackageIcon>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
@@ -24,4 +25,11 @@
<PackageReference Include="System.Formats.Cbor" Version="9.0.2" />
</ItemGroup>
<ItemGroup>
<None Update="mroaLogo.png">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB