in reply to Re^3: Circular buffer
in thread Circular buffer
Perl uses a flat array (SV*[]) for arrays. It over-allocates, and it keeps track of where the visible array starts and end.
In your example, if there's at least one extra spare element at the end of the underlying array, it will move the pointers at indexes 3..999 to indexes 4..1000. I expect this to be a really fast memcpy. The new element is then assigned.
If there isn't any free room, Perl allocates a new array roughly twice its old size and copies everything over.
push works the same way, but keep in mind there will usually be extra spare elements to push into.
unshift is the same as well. Perl leaves spare elements at the start of the array. The catch is that it starts with no spare elements and any reallocation of the array that isn't caused by unshift will remove the spare elements.
|
|---|