Code Monkey home page Code Monkey logo

Comments (5)

neuecc avatar neuecc commented on July 4, 2024

Although it seems at first glance that making something nullable would only increase its size by 1 byte, there's a possibility for a significant increase due to padding.
In the worst-case scenario with a long, making it nullable adds 1 byte and 7 bytes of padding.

from memorypack.

Alistairot avatar Alistairot commented on July 4, 2024

Can it be optimized? In the case of frequent transmission, there will be a lot of overhead.

from memorypack.

neuecc avatar neuecc commented on July 4, 2024

make the custom formatter.

[MemoryPackable]
// [GenerateTypeScript]
public partial class MemoryPackableTestClassB
{
    public int Field1 { get; set; }

    [CompactNullableFormatter<long>]
    public long? Field2 { get; set; }
}

public sealed class CompactNullableFormatter<T> : MemoryPackCustomFormatterAttribute<T?>
    where T : struct
{
    static IMemoryPackFormatter<T?> formatter = new Formatter();

    public override IMemoryPackFormatter<T?> GetFormatter()
    {
        return formatter;
    }

    sealed class Formatter : IMemoryPackFormatter<T?>
    {
        public void Serialize<TBufferWriter>(ref MemoryPackWriter<TBufferWriter> writer, scoped ref T? value) where TBufferWriter : IBufferWriter<byte>
        {
            if (value.HasValue)
            {
                writer.WriteObjectHeader(1);
                writer.WriteValue(value.Value);
            }
            else
            {
                writer.WriteNullObjectHeader();
            }
        }

        public void Deserialize(ref MemoryPackReader reader, scoped ref T? value)
        {
            if (reader.TryReadObjectHeader(out var _))
            {
                value = reader.ReadValue<T>();
            }
            else
            {
                value = null;
            }
        }
    }
}

However, it might be difficult to coexist with TypeScriptGenerate.

If minimizing the payload size is the highest priority, replacing it with a dummy class can be effective.

[MemoryPackable]
[GenerateTypeScript]
public partial class CompactNullable<T>
    where T : struct
{
    public bool HasValue;
    public T Value;
}

from memorypack.

Alistairot avatar Alistairot commented on July 4, 2024

Thanks, this helps me a lot!

from memorypack.

Alistairot avatar Alistairot commented on July 4, 2024

According to your solution, i wrap the type with a new class and rewrite the operator. This works well.

[MemoryPackable]
[GenerateTypeScript]
public partial class WrapLong
{
	public long Value;

	public static implicit operator WrapLong(long value)
	{
		return new WrapLong { Value = value };
	}

	public static implicit operator long(WrapLong data)
	{
		return data.Value;
	}
}


[MemoryPackable]
[GenerateTypeScript]
public partial class MemoryPackableTestClassB
{
	public int Field1 { get; set; }

	public WrapLong Field2 { get; set; }
}

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.