RSCG – MemoryPack

RSCG – MemoryPack
 
 

name MemoryPack
nuget https://www.nuget.org/packages/MemoryPack/
link https://github.com/Cysharp/MemoryPack
author Cysharp, Inc

Efficient serializer

 

This is how you can use MemoryPack .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="MemoryPack" Version="1.9.16" />
  </ItemGroup>
	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>
</Project>


The code that you will use is


var v = new Person { Age = 53, Name = "Andrei Ignat" };

var bin = MemoryPackSerializer.Serialize(v);
var val = MemoryPackSerializer.Deserialize<Person>(bin);
Console.WriteLine(val.Name);


namespace MemoryPackDemo;

[MemoryPackable]
public partial class Person
{
    public int Age { get; set; }
    public string? Name { get; set; }
}


 

The code that is generated is


// <auto-generated/>
#nullable enable
#pragma warning disable CS0108 // hides inherited member
#pragma warning disable CS0162 // Unreachable code
#pragma warning disable CS0164 // This label has not been referenced
#pragma warning disable CS0219 // Variable assigned but never used
#pragma warning disable CS8600 // Converting null literal or possible null value to non-nullable type.
#pragma warning disable CS8601 // Possible null reference assignment
#pragma warning disable CS8602
#pragma warning disable CS8604 // Possible null reference argument for parameter
#pragma warning disable CS8619
#pragma warning disable CS8620
#pragma warning disable CS8631 // The type cannot be used as type parameter in the generic type or method
#pragma warning disable CS8765 // Nullability of type of parameter
#pragma warning disable CS9074 // The 'scoped' modifier of parameter doesn't match overridden or implemented member
#pragma warning disable CA1050 // Declare types in namespaces.

using System;
using MemoryPack;

namespace MemoryPackDemo;

/// <remarks>
/// MemoryPack GenerateType: Object<br/>
/// <code>
/// <b>int</b> Age<br/>
/// <b>string</b> Name<br/>
/// </code>
/// </remarks>
partial class Person : IMemoryPackable<Person>
{


    static Person()
    {
        global::MemoryPack.MemoryPackFormatterProvider.Register<Person>();
    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackFormatterRegister.RegisterFormatter()
    {
        if (!global::MemoryPack.MemoryPackFormatterProvider.IsRegistered<Person>())
        {
            global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MemoryPack.Formatters.MemoryPackableFormatter<Person>());
        }
        if (!global::MemoryPack.MemoryPackFormatterProvider.IsRegistered<Person[]>())
        {
            global::MemoryPack.MemoryPackFormatterProvider.Register(new global::MemoryPack.Formatters.ArrayFormatter<Person>());
        }

    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackable<Person>.Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref Person? value) 
    {

        if (value == null)
        {
            writer.WriteNullObjectHeader();
            goto END;
        }

        writer.WriteUnmanagedWithObjectHeader(2, value.@Age);
        writer.WriteString(value.@Name);

    END:

        return;
    }

    [global::MemoryPack.Internal.Preserve]
    static void IMemoryPackable<Person>.Deserialize(ref MemoryPackReader reader, scoped ref Person? value)
    {

        if (!reader.TryReadObjectHeader(out var count))
        {
            value = default!;
            goto END;
        }


        
        int __Age;
        string __Name;

        
        if (count == 2)
        {
            if (value == null)
            {
                reader.ReadUnmanaged(out __Age);
                __Name = reader.ReadString();


                goto NEW;
            }
            else
            {
                __Age = value.@Age;
                __Name = value.@Name;

                reader.ReadUnmanaged(out __Age);
                __Name = reader.ReadString();

                goto SET;
            }

        }
        else if (count > 2)
        {
            MemoryPackSerializationException.ThrowInvalidPropertyCount(typeof(Person), 2, count);
            goto READ_END;
        }
        else
        {
            if (value == null)
            {
               __Age = default!;
               __Name = default!;
            }
            else
            {
               __Age = value.@Age;
               __Name = value.@Name;
            }


            if (count == 0) goto SKIP_READ;
            reader.ReadUnmanaged(out __Age); if (count == 1) goto SKIP_READ;
            __Name = reader.ReadString(); if (count == 2) goto SKIP_READ;

    SKIP_READ:
            if (value == null)
            {
                goto NEW;
            }
            else            
            {
                goto SET;
            }

        }

    SET:
        
        value.@Age = __Age;
        value.@Name = __Name;
        goto READ_END;

    NEW:
        value = new Person()
        {
            @Age = __Age,
            @Name = __Name
        };
    READ_END:

    END:

        return;
    }
}

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/MemoryPack