Jump to content

  • Log In with Google      Sign In   
  • Create Account


Circular buffer usage


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 greenzone   Members   -  Reputation: 672

Like
0Likes
Like

Posted 29 June 2013 - 12:01 PM

So i am reading about the Circular Buffer. I personally have never used one but it seemed intriguing. Could some one share some scenarios for its optimal usage vs regular delineated buffer, for those of you that are familiar with its usefulness. Like when is it appropriate vs not appropriate?


J-GREEN

Greenpanoply

Sponsor:

#2 Paradigm Shifter   Crossbones+   -  Reputation: 5253

Like
2Likes
Like

Posted 29 June 2013 - 12:18 PM

Use it when you want a double-ended queue rather than an array. Input buffers are a good use case for this (you read from one end and new data [i.e. fresh input] is added at the other end).


"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#3 Mussi   Crossbones+   -  Reputation: 1762

Like
2Likes
Like

Posted 29 June 2013 - 12:52 PM

Lets say you have a game with a rewind functionality, where your character can travel back in time. You'll most likely have a cap on how far one can rewind. At any point in time, you'll have to know the x steps taken before the current step. Now imagine how easy this becomes with a circular buffer smile.png.



#4 SillyCow   Members   -  Reputation: 849

Like
3Likes
Like

Posted 29 June 2013 - 02:21 PM

Having a circular buffer prevents you from allocating new memory.

It also prevents memory  fragmentation.

Both of these problems will cause performance drops.

 

Any task that involving queues where performance is an issue will benefit from a circular buffer.

 

They are also in use in hardware and embedded programming.

Here, memory allocation (new / malloc ) is sometimes not available.


My new android game : Enemies of the Crown

My previous android game : Killer Bees


#5 NightCreature83   Crossbones+   -  Reputation: 2738

Like
2Likes
Like

Posted 29 June 2013 - 05:29 PM

Perfect for use with producer and consumer queues as well, think chat messages and stuff like that.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 Paradigm Shifter   Crossbones+   -  Reputation: 5253

Like
1Likes
Like

Posted 29 June 2013 - 05:39 PM

Yeah, as I said anything a double ended queue is good for is ideal (as long as the queue has a fixed maximum size - although it can be resized of course. For efficiency reasons you will probably prefer a buffer size which is a power of 2 so you can use logical AND rather than modulo arithmetic to wrap the buffers).

 

More use-cases:

 

Message system (read messages from front of queue add new messages to the back of the queue).

Bubble Bobble (Puzzle Bobble? Maybe?) scrolling-type games where new tiles are added to the top of the screen and tiles are removed from the bottom of the screen.

Rewind buffer (already mentioned)

Text window with limited history (already mentioned)

Undo buffer with limited undo

 

etc.


Edited by Paradigm Shifter, 29 June 2013 - 06:03 PM.

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

#7 prh99   Members   -  Reputation: 501

Like
1Likes
Like

Posted 29 June 2013 - 09:39 PM

I know they are used in LZMA and LZSS compression as way to create a sliding window for the algorithm and in various aspects of digital signal processing.


Edited by prh99, 29 June 2013 - 09:47 PM.

Patrick

#8 Khatharr   Crossbones+   -  Reputation: 2961

Like
1Likes
Like

Posted 30 June 2013 - 01:38 PM

Circular queues are common when decoding audio, which is an example of a producer/consumer queue like Night mentioned.
void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS