Comments (5)
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.
Can it be optimized? In the case of frequent transmission, there will be a lot of overhead.
from memorypack.
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.
Thanks, this helps me a lot!
from memorypack.
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)
- .Net 8 NativeAOT Crash when serialize any type contains generic property. HOT 1
- BrotliDecompressor take too many CPU HOT 5
- NativeAOT compatibility: publish warnings and how to make MemoryPack fully NativeAOT compatible? HOT 1
- C# Generic class support HOT 2
- Unexpected sequence reached end exception HOT 4
- MemoryPackStreamingSerializer.DeserializeAsync doesn't work HOT 1
- UnmanagedType Specialization HOT 1
- Question: Does MemoryPackStreamingSerializer.DeserializeAsync function works like "lazy load"? HOT 2
- Bug : Generic Struct with Unmanaged Constraint
- MemoryPack.Generator.Roslyn3.dll is seems to be old HOT 1
- MemoryPack does not work in LinqPad HOT 2
- inherit class in another project, the parent class properties will not generate in source generator code! HOT 1
- How to use built primitive type serialization instead of writing wrapper types for every possible value HOT 3
- VersionTolerant serialization of abstract class fails to compile HOT 1
- memorypack is missing NuGet package README file
- MemoryPack not fully compatible with Visual Studio "Find references" HOT 1
- Make ContentType a static const string HOT 1
- .NET 8 class with primary constructors not supported HOT 2
- Support types that aren't known at compile time HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from memorypack.