【问题标题】:C# Protobuf .NET Using Preexisting Byte ArrayC# Protobuf .NET 使用预先存在的字节数组
【发布时间】:2015-06-14 01:18:45
【问题描述】:

所以我正在使用 .NET 中的 Protobufs,并尝试将它们与缓冲池和 AsyncSocketEventArgs 池合并。缓冲池将大字节数组的各个部分分配给事件 args。

所以,问题是,我想不出一种方法让 Protobufs 直接序列化到我的一个缓冲区上。相反,所有方法似乎都在自己的缓冲区上序列化,这会浪费时间/内存......有什么方法可以做我正在寻找的事情吗?

编辑:我创建了一个原型方案,并生成了包含不完全序列化类的增量的消息,所以我相信使用属性/序列化器类对我没有帮助。我想将字节直接写入我的缓冲区之一。我相信 MemoryStream,从我读过的内容来看,仍然只是指向一个创建的字节数组,这仍然会浪费大量的时间/内存。

【问题讨论】:

  • 这与问题无关,但事件驱动的套接字IO自从await到来以来几乎已经过时了。 CPU 节省非常小,架构影响巨大。
  • 很高兴知道我正在处理一些过时的网站哈哈。我会做更多的研究。

标签: c# networking serialization protobuf-net objectpool


【解决方案1】:

使用内存流

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {

        static void Main(string[] args)
        {
            Person person = new Person();
            XmlSerializer serializer = new XmlSerializer(typeof(Person));
            MemoryStream stream = new MemoryStream();

            serializer.Serialize(stream, person); 
        }


    }
    public class Person
    {
    }

}
​

【讨论】:

  • 这不使用 Protobufs,但我需要它们的序列化效率。您是否试图在答案中以某种方式将此链接到 Protobufs?
  • 正在查看以下网页:code.google.com/p/protobuf-net。以前没有使用过 protobuf,但知道可以使用 memorystream 进行序列化。
  • 查看网页上的入门链接。
  • 我会检查并通知您。根据我的安装界面,我没有意识到他们做到了
  • 流类是通常会加速程序的缓冲区。流只会读取有限数量的数据(基于缓冲区大小),而不是一次读取整个输入。它将在后台模式下填充和解析,不会影响程序的执行时间。
猜你喜欢
  • 1970-01-01
  • 2012-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
相关资源
最近更新 更多