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:
= 1 bytes can be enqueued at a time
= 1 bytes can be dequeued at a time
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.
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.
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.
Depending upon how the incoming bytes are received and how the parser examines them, you might consider a Queue<byte[]>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With