1

So I am working with Protobufs in .NET and I am trying to incorporate them with a buffer pool and AsyncSocketEventArgs pool. The buffer pool assigns sections of a huge byte array to the event args.

So, the problem, I can't figure out a way to have Protobufs serialize directly onto one of my buffers. Instead all methods appear to serialize onto there own buffers, which wastes time/memory... Any way to do what I'm looking for?

EDIT: I have created a proto scheme and I generate messages that contain deltas not entirely serialized classes, so I believe using the attributes/Serializer class won't help me. I want to write the bytes directly into one of my buffers. I believe MemoryStream, from what I have read will still just point to a created byte array, which will still waste a lot of time/memory.

guitar80
  • 716
  • 6
  • 19
  • This has nothing to do with the question but event-driven socket IO is almost obsolete since the arrival of await. The CPU saving are very small and the architectural impact is huge. – usr Jun 14 '15 at 15:53
  • Good to know I think I'm working on a few out dated sites haha. I'll do more research. – guitar80 Jun 14 '15 at 17:30

1 Answers1

1

Use a memory stream

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
    {
    }

}
​
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 1
    This doesn't use Protobufs though which I need for their serialization efficiency. Are you trying to link this to Protobufs somehow in your answer? – guitar80 Jun 14 '15 at 01:35
  • 1
    Was looking at following webpage : http://code.google.com/p/protobuf-net/. Haven't used protobuf before but know you can serialize using a memorystream. – jdweng Jun 14 '15 at 01:41
  • See the Getting Started link on the webpage. – jdweng Jun 14 '15 at 01:45
  • I'll check on that and let you know. I didn't realize that they did, based on my installations interfaces – guitar80 Jun 14 '15 at 02:09
  • Hmm I probably need to edit my question to be more specific, because this still doesn't seem to fit with what I need. But +1 for the help so far! – guitar80 Jun 14 '15 at 02:16
  • Please check out my comment. I read more about this and I don't think it will help my particular case. – guitar80 Jun 14 '15 at 15:11
  • 1
    Stream classes are buffers that normally will speed up a program. The stream will only read a limited amount of data (based on the buffer size) and not read the entire input at one time. It will fill and parse in background mode and not effect the execution time of your program. – jdweng Jun 14 '15 at 15:34
  • Cool, well thanks. After reading more about it, I think I can get it to fit, I won't be using the serializer class, but the memory streams helped me track down what I needed! – guitar80 Jun 14 '15 at 17:31
  • You don't need your buffers if you are using the memorystream. You can pass a memory stream to an Async Socket and avoid any buffer arrays. – jdweng Jun 14 '15 at 17:52