Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient C# byte queue for parsing stream of bytes for binary message packets

I'm trying to replace what I would usually implement as a circular-buffer+. The function of the queue is to buffer incoming bytes (eg. from serial port or some other stream of data), whilst a parser examines bytes in the queue and detects and extracts message packets.

Criteria:

  • can grow (ie not fixed-sized)
  • = 1 bytes can be enqueued at a time

  • = 1 bytes can be dequeued at a time

  • efficient

I'm tempted just to use

System.Collections.Generic.Queue<byte>

... but I'm not sure if this is the most efficient type to use. Any suggestions?

Are there any smarter ways of doing what I'm trying to do? (Eg. Interesting suggestions here)

Thanks for your suggestions & input.

Prembo.

like image 799
Prembo Avatar asked Jul 18 '10 14:07

Prembo


3 Answers

Well, Queue<byte> will be efficient in terms of memory. It will basically be a byte[] behind the scenes. It may be slightly awkward to work with if you want to dequeue or enqueue whole chunks at a time though. Each operation should be amortized O(1) for a single byte, leading to O(n) to enqueue or dequeue a chunk of size n... but the scaling factor will be higher than (say) a buffer block copy.

like image 59
Jon Skeet Avatar answered Oct 07 '22 02:10

Jon Skeet


Queue<byte> is backed by a byte[], but you would see better performance if copying to/from the underlying buffer using Array.Copy rather than looping through Enqueue/Dequeue methods. So personally, if Queue<byte> doesn't give you the performance you want, then you might implement your own queue class that provides QueueMultiple and DequeueMultiple methods.

like image 27
Drew Noakes Avatar answered Oct 07 '22 00:10

Drew Noakes


Depending upon how the incoming bytes are received and how the parser examines them, you might consider a Queue<byte[]>.

like image 23
Eric Dahlvang Avatar answered Oct 07 '22 00:10

Eric Dahlvang