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

推荐订阅源

Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
O
OpenAI News
D
Docker
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
C
CXSECURITY Database RSS Feed - CXSecurity.com
阮一峰的网络日志
阮一峰的网络日志
K
Kaspersky official blog
Security Latest
Security Latest
T
Tailwind CSS Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Vulnerabilities – Threatpost
W
WeLiveSecurity
N
News and Events Feed by Topic
aimingoo的专栏
aimingoo的专栏
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
Help Net Security
Help Net Security
爱范儿
爱范儿
宝玉的分享
宝玉的分享
腾讯CDC
H
Heimdal Security Blog
Webroot Blog
Webroot Blog
AI
AI
WordPress大学
WordPress大学
Recorded Future
Recorded Future
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
C
Check Point Blog
TaoSecurity Blog
TaoSecurity Blog
Cisco Talos Blog
Cisco Talos Blog
The Cloudflare Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - Franky
云风的 BLOG
云风的 BLOG

博客园 - 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出来的, 就是我们想要的效果, 建议都升级一下.