Don't use arrays as stacks

Steven Schveighoffer schveiguy at yahoo.com
Mon Sep 26 06:01:28 PDT 2011


On Sun, 25 Sep 2011 07:05:34 -0400, Nick Sabalausky <a at a.a> wrote:

> "Jonathan M Davis" <jmdavisProg at gmx.com> wrote in message
> news:mailman.160.1316939358.26225.digitalmars-d at puremagic.com...
>> On Sunday, September 25, 2011 03:18:29 Andrew Wiley wrote:
>>>
>>> Isn't this exactly what assumeSafeAppend is for?
>
> Hmm, I didn't know about that. (Actually, I remember hearing it mentioned
> before, but then totally forgot about it.)
>
>>
>> and if you're using assumeSafeAppend, then you
>> need to guarantee that nowhere else has a reference to that array
>> (otherwise
>> it's _not_ safe to assume that it's safe to append)
>>
>
> Would the consequences of failing to do that be any worse (or any  
> different
> at all?) than what I mentioned about:
>
> "One caveat about this method: If you save a slice of the stack, pop
> elements off the stack, and then push new values back on, the old slice  
> you
> took will likely reflect the new values, not the original ones."
>
> ...?
>

The caveat is the same, however, you should continue doing it the way you  
are doing it.  Using the runtime is somewhat magic, and there is a cost  
for being that magical -- too many runtime calls :)

Every call to capacity and assumeSafeAppend *cannot* be inlined or  
optimized, since they end up calling extern(C) function prototypes.

BTW, if you want to compare your performance with a stack implementation  
that uses assumeSafeAppend, try out dcollections' ArrayList, in which  
popBack does use assumeSafeAppend.

-Steve


More information about the Digitalmars-d mailing list