惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

The Hacker News
The Hacker News
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
The Last Watchdog
The Last Watchdog
小众软件
小众软件
S
Security @ Cisco Blogs
S
Schneier on Security
Forbes - Security
Forbes - Security
S
Secure Thoughts
W
WeLiveSecurity
Latest news
Latest news
博客园 - Franky
Last Week in AI
Last Week in AI
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
美团技术团队
Schneier on Security
Schneier on Security
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
B
Blog
GbyAI
GbyAI
A
About on SuperTechFans
爱范儿
爱范儿
博客园 - 叶小钗
T
Tor Project blog
SecWiki News
SecWiki News
Blog — PlanetScale
Blog — PlanetScale
A
Arctic Wolf
博客园 - 聂微东
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
G
GRAHAM CLULEY
Webroot Blog
Webroot Blog
Google Online Security Blog
Google Online Security Blog
博客园 - 三生石上(FineUI控件)
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
The Register - Security
The Register - Security
C
CERT Recently Published Vulnerability Notes
K
Kaspersky official blog
U
Unit 42
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
V2EX - 技术
V2EX - 技术
Help Net Security
Help Net Security
阮一峰的网络日志
阮一峰的网络日志
Recent Announcements
Recent Announcements
J
Java Code Geeks

博客园 - egmkang

配眼镜最佳实践 [Python]解密pyc文件 CPU实现原子操作的原理 Flash----一种VirtualActor模式的分布式有状态系统原型 [投资]对价值投资新的理解 通过Consul Raft库打造自己的分布式系统 Dapr实现分布式有状态服务的细节 [06] 优化C#服务器的思路和工具的使用 [05] 通过P/Invoke加速C#程序 [04] C# Alloc Free编程之实践 [03] C# Alloc Free编程 [02] 多线程逻辑编程 [01] C#网络编程的最佳实践 C#如何正确的做深拷贝 C# Protobuf如何做到0分配内存的序列化 最近很火的namebase羊毛, 手把手教你怎么薅 DotNetty发送请求的最佳实践 C# protobuf自动更新cs文件 多读少写场景下多线程锁冲突的降低
C# Protobuf如何做到0分配内存的序列化/反序列化(2)
egmkang · 2020-12-22 · via 博客园 - egmkang

受限于当时的基础设施, 只能做到这样的程度: C# Protobuf如何做到0分配内存的序列化

但是Protobuf 3.13开始提供对Span的支持, 就意味着可以真正做到0分配内存, 对GC非常友好:

  • Add ParseFrom(ReadOnlySequence<byte>) method to enable GC friendly
    parsing with reduced allocations and buffer copies. (#7351)
  • Add support for serialization directly to a IBufferWriter<byte> or
    to a Span<byte> to enable GC friendly serialization.
    The new API is available as extension methods on the IMessage type. (#7576)

Protobuf 3.14也修改了ByteString:

  • annotate ByteString.CopyFrom(ReadOnlySpan) as SecuritySafeCritical (#7701)

代码的例子也很简单:

//反序列化
var span = new ReadOnlySequence<byte>(array);
var message = MessageTest.Parser.ParseFrom(span);

//序列化
var outputSpan = new Span<byte>(destArray, 0, message.CalculateSize());
message.WriteTo(outputSpan);

需要安装Protobuf 3.14.0以上版本

我们的测试代码, 序列化反序列化1W次, 看看内存分配:

static void Main(string[] args)
{
     var array = new byte[0];
     var destArray = new byte[1024];
     for (int i = 0; i < 10000; ++i) 
     {
          var span = new ReadOnlySequence<byte>(array);
          var message = MessageTest.Parser.ParseFrom(span);

          message.L1 = 1112121;
          message.L2 = 231243452324234;

          var outputSpan = new Span<byte>(destArray, 0, message.CalculateSize());
          message.WriteTo(outputSpan);
     }
}

然后sampling的结果:

只有那个反序列化的对象是被new出来的, 就是我们想要的效果, 建议都升级一下.