Skip to content

Commit

Permalink
release v2.1.9
Browse files Browse the repository at this point in the history
## Nino.Serialization v2.1.9
- [Optimization] Faster deserialization
  • Loading branch information
JasonXuDeveloper committed Nov 11, 2024
1 parent abc72ff commit c9911a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
32 changes: 16 additions & 16 deletions src/Nino.Core/Reader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Reader(Span<byte> buffer)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Read<T>(out T value) where T : unmanaged
{
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(_data));
value = Unsafe.ReadUnaligned<T>(ref _data[0]);
_data = _data.Slice(Unsafe.SizeOf<T>());
}

Expand Down Expand Up @@ -52,13 +52,12 @@ public void Read<T>(out T[] ret) where T : unmanaged
Read(out int length);
GetBytes(length * Unsafe.SizeOf<T>(), out var bytes);
#if NET5_0_OR_GREATER
ret = bytes.Length > 2048 ? GC.AllocateUninitializedArray<T>(length) : new T[length];
Span<T> span = ret;
Span<byte> byteSpan = MemoryMarshal.Cast<T, byte>(span);
Unsafe.CopyBlockUnaligned(ref byteSpan[0], ref bytes[0], (uint)bytes.Length);
ret = bytes.Length <= 2048 ? new T[length] : GC.AllocateUninitializedArray<T>(length);
#else
ret = MemoryMarshal.Cast<byte, T>(bytes).ToArray();
ret = new T[length];
#endif
ref byte first = ref Unsafe.As<T, byte>(ref ret[0]);
Unsafe.CopyBlockUnaligned(ref first, ref bytes[0], (uint)bytes.Length);
return;
default:
throw new InvalidOperationException($"Invalid type id {typeId}");
Expand Down Expand Up @@ -101,19 +100,17 @@ public void Read<T>(out List<T> ret) where T : unmanaged
case TypeCollector.CollectionTypeId:
Read(out int length);
GetBytes(length * Unsafe.SizeOf<T>(), out var bytes);
ret = new List<T>(length);
#if NET5_0_OR_GREATER
ret = new List<T>();
ref var lst = ref Unsafe.As<List<T>, TypeCollector.ListView<T>>(ref ret);
lst._size = length;
Span<byte> byteSpan = MemoryMarshal.Cast<T, byte>(lst._items);
Unsafe.CopyBlockUnaligned(ref byteSpan[0], ref bytes[0], (uint)bytes.Length);
#if NET5_0_OR_GREATER
var arr = bytes.Length <= 2048 ? new T[length] : GC.AllocateUninitializedArray<T>(length);
#else
ReadOnlySpan<T> span = MemoryMarshal.Cast<byte, T>(bytes);
for (int i = 0; i < length; i++)
{
ret.Add(span[i]);
}
var arr = new T[length];
#endif
ref byte first = ref Unsafe.As<T, byte>(ref arr[0]);
Unsafe.CopyBlockUnaligned(ref first, ref bytes[0], (uint)bytes.Length);
lst._items = arr;

return;
default:
Expand Down Expand Up @@ -207,7 +204,10 @@ public void Read(out string ret)
Read(out int length);
GetBytes(length * sizeof(char), out var bytes);
#if NET5_0_OR_GREATER
ret = new string(MemoryMarshal.Cast<byte, char>(bytes));
ret = new string(
MemoryMarshal.CreateReadOnlySpan(
ref Unsafe.As<byte, char>(ref bytes[0]),
length));
#else
ret = MemoryMarshal.Cast<byte, char>(bytes).ToString();
#endif
Expand Down
10 changes: 6 additions & 4 deletions src/Nino.Generator/EmbedTypeDeserializerGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ bool IsTypeParameter(ITypeSymbol typeSymbol)
{
var elemType = ((IArrayTypeSymbol)type).ElementType.ToDisplayString();
if (addedElemType.Add(elemType))
sb.AppendLine(GenerateArrayCollectionSerialization(
sb.AppendLine(GenerateArraySerialization(
((IArrayTypeSymbol)type).ElementType.GetDeserializePrefix(),
elemType, " "));
sb.GenerateClassDeserializeMethods(typeFullName);
Expand All @@ -268,7 +268,7 @@ bool IsTypeParameter(ITypeSymbol typeSymbol)
{
var elemType = s.TypeArguments[0].ToDisplayString();
if (addedElemType.Add(elemType))
sb.AppendLine(GenerateArrayCollectionSerialization(s.TypeArguments[0].GetDeserializePrefix(),
sb.AppendLine(GenerateArraySerialization(s.TypeArguments[0].GetDeserializePrefix(),
elemType,
" "));
if (type.TypeKind != TypeKind.Interface)
Expand Down Expand Up @@ -323,7 +323,7 @@ public static partial class Deserializer
context.AddSource("NinoDeserializerExtension.Ext.g.cs", code);
}

private static string GenerateArrayCollectionSerialization(string prefix, string elemType, string indent)
private static string GenerateArraySerialization(string prefix, string elemType, string indent)
{
var creationDecl = elemType.EndsWith("[]")
? elemType.Insert(elemType.IndexOf("[]", StringComparison.Ordinal), "[length]")
Expand All @@ -342,9 +342,11 @@ public static void Deserialize(out {{elemType}}[] value, ref Reader reader)
case TypeCollector.CollectionTypeId:
reader.Read(out int length);
value = new {{creationDecl}};
var span = value.AsSpan();
for (int i = 0; i < length; i++)
{
{{prefix}}(out value[i], ref reader);
{{prefix}}(out {{elemType}} val, ref reader);
span[i] = val;
}
break;
default:
Expand Down

0 comments on commit c9911a9

Please sign in to comment.