Code Monkey home page Code Monkey logo

Comments (3)

tedbarth avatar tedbarth commented on June 25, 2024

@neuecc Proposal: Add API to get an index (or id) of a formatter and a formatter by index (or id):

  public class KnownObjectFormatter : MemoryPackFormatter<Object?> {
    public override void Serialize<TBufferWriter>(
      ref MemoryPackWriter<TBufferWriter> writer,
      scoped ref Object? value) {
      if (value == null) {
        writer.WriteNullObjectHeader();
        return;
      }

      Type type = value.GetType();
      ushort index = MemoryPackFormatterProvider.GetFormatterIndex(type);
      var formatter = MemoryPackFormatterProvider.GetFormatter(type);

      writer.WriteValue(index);
      formatter.Serialize(writer, value);
    }

    public override void Deserialize(ref MemoryPackReader reader, scoped ref Object? value) {
      if (!reader.TryReadObjectHeader(out var count)) {
        value = null;
        return;
      }

      ushort index = reader.ReadValue<ushort>();
      var formatter = MemoryPackFormatterProvider.GetFormatterByIndex();

      value = formatter.Deserialize(reader);
    }
  }

It is slower than when using static types, but not that much. I would expect it to be much faster that when having to wrap every value into an object on the heap. And there is no need to write boiler plate wrapper classes for primitives.

from memorypack.

tedbarth avatar tedbarth commented on June 25, 2024

I helped myself with the following formatter:

  public class KnownObjectFormatter : MemoryPackFormatter<Object?> {
    public class KnownObjectFormatterException(string message) : Exception(message);
    private static readonly Bictionary<Type, ushort> RegisteredTypes = new Bictionary<Type, ushort>();

    public static void RegisterType(Type type) {
      if (!RegisteredTypes.ContainsKeyForward(type)) {
        ushort index = (ushort)RegisteredTypes.Count;
        RegisteredTypes.Add(type, index);
      }
    }

    public override void Serialize<TBufferWriter>(
      ref MemoryPackWriter<TBufferWriter> writer,
      scoped ref Object? value) {
      if (value == null) {
        writer.WriteNullObjectHeader();
        return;
      }

      Type type = value.GetType();

      if (!RegisteredTypes.TryGetValueForward(type, out ushort index)) {
        throw new KnownObjectFormatterException($"Type {type} not registered");
      }

      writer.WriteUnmanaged<ushort>(index);

      var formatter = writer.GetFormatter(type);
      formatter.Serialize(ref writer, ref value);
    }

    public override void Deserialize(ref MemoryPackReader reader, scoped ref Object? value) {
      if (reader.PeekIsNull()) {
        reader.Advance(1);
        value = null;
        return;
      }

      ushort index = reader.ReadUnmanaged<ushort>();
      if (!RegisteredTypes.TryGetValueReverse(index, out var type)) {
        throw new KnownObjectFormatterException($"No type with index {index} registered");
      }

      var formatter = reader.GetFormatter(type!);
      formatter.Deserialize(ref reader, ref value);
    }
    

    public class Attribute : MemoryPackCustomFormatterAttribute<object?> {
      private static readonly KnownObjectFormatter Formatter = new KnownObjectFormatter();

      public override IMemoryPackFormatter<object?> GetFormatter() {
        return Formatter;
      }
    }
  }

That boils down to usage:

KnownObjectFormatter.RegisterType(typeof(int));
KnownObjectFormatter.RegisterType(typeof(int?));
KnownObjectFormatter.RegisterType(typeof(int[]));
KnownObjectFormatter.RegisterType(typeof(bool));
KnownObjectFormatter.RegisterType(typeof(bool?));
KnownObjectFormatter.RegisterType(typeof(bool[]));
KnownObjectFormatter.RegisterType(typeof(string));
KnownObjectFormatter.RegisterType(typeof(string[]));
...
KnownObjectFormatter.RegisterType(typeof(MyCustomType));
KnownObjectFormatter.RegisterType(typeof(MyCustomType?));
KnownObjectFormatter.RegisterType(typeof(MyCustomType[]));
...

It still is tedious to do that for every possible already mapped type, but it at least is better than having to write wrapper classes.

So proposal as changed to:

  • Expose types defined in src/MemoryPack.Core/MemoryPackFormatterProvider.WellknownTypes.cs to be consumed by such formatter (above code)

from memorypack.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.