Search code examples
pythonqueuebufferstructure

Python difference between ring buffer and simultaneous pop-push in queue?


I'm working on an application that in a part requires to save the latest n values from a stream of data for processing the whole stream at once in intervals, overwriting the oldest value, I'm familiar with ring buffers in C which are used in low memory constrains scenarios.

Yet, In python, whats the advantage of this implementation rather than just having a queue object and at each insertion of data just performing a pop() and a push(). Are there memory vulnerabilities with this approach?


Solution

  • In any language a ring buffer implementation offers a significant performance advantage: no data is being moved around when adding or removing elements and also the memory where those are stored is contiguous. In python you can use collections.deque with maxlen argument, to get the same behavior and performance. Still using a plain list it is going to reasonably fast. What do you mean by memory vulnerabilities ?