Re: Efficiency of using array as stack

2013-03-25 Thread Ivan Kazmenko
You will likely get better performance if you use: a ~= int.init; instead of: a.length++; So that's a relief, a dynamic array in D actually *is* an efficient queue implementation out of the box, I just did it improperly in the example. Thank you for the correction. With that, the number

Re: Efficiency of using array as stack

2013-03-25 Thread Steven Schveighoffer
If you happened to get that accidental blank send, sorry, I tried to cancel it. On Sat, 23 Mar 2013 19:45:21 -0400, Ivan Kazmenko wrote: On Saturday, 23 March 2013 at 19:33:06 UTC, Jonathan M Davis wrote: You might want to check out this article where someone ran into similar issues: htt

Re: Efficiency of using array as stack

2013-03-24 Thread Ali Çehreli
On 03/23/2013 03:28 PM, Ivan Kazmenko wrote: > Still, I am missing something here. After assumeSafeAppend(s), the > capacity of s is restored to the original value (in my case, 2_000_891). assumeSafeAppend allows the programmer to say "I am sure there is no other reference to the rest of this s

Re: Efficiency of using array as stack

2013-03-23 Thread Jonathan M Davis
On Sunday, March 24, 2013 00:45:21 Ivan Kazmenko wrote: > So, what is the exact strategy of growing an array? Maybe you > could just kindly point me to some more interesting reading in > druntime source? I'd start by looking in src/Object_.d, but it looks like the information that you'd be looki

Re: Efficiency of using array as stack

2013-03-23 Thread Ivan Kazmenko
On Saturday, 23 March 2013 at 19:33:06 UTC, Jonathan M Davis wrote: You might want to check out this article where someone ran into similar issues: https://www.semitwist.com/articles/article/view/don-t-use-arrays-as-stacks And if you haven't read Steven's article on arrays, you should do so:

Re: Efficiency of using array as stack

2013-03-23 Thread Ivan Kazmenko
On Saturday, 23 March 2013 at 13:55:57 UTC, H. S. Teoh wrote: Alternatively, you should not shrink the array, but keep another counter on how much of the array is being used, so that you can reuse the space immediately without risking reallocation. Here's an example array-based stack implementat

Re: Efficiency of using array as stack

2013-03-23 Thread Ivan Kazmenko
On Saturday, 23 March 2013 at 12:37:04 UTC, bearophile wrote: Call assumeSafeAppend every time you want to append with no reallocations. Thank you for the suggestion! This seems to work fast enough and never relocate: - import core.memory; import std.range; import std.stdio; void main

Re: Efficiency of using array as stack

2013-03-23 Thread Jonathan M Davis
On Saturday, March 23, 2013 13:10:56 Ivan Kazmenko wrote: > Today, I had trouble using D built-in dynamic array as a stack: > it timed out badly. I have then tried to reduce the problem down > to a minimal example. I am using DMD 2.062. You might want to check out this article where someone ran

Re: Efficiency of using array as stack

2013-03-23 Thread H. S. Teoh
On Sat, Mar 23, 2013 at 01:10:56PM +0100, Ivan Kazmenko wrote: > Hello again! > > Today, I had trouble using D built-in dynamic array as a stack: it > timed out badly. Using arrays as stack *can* be efficient -- but you should not be appending/shrinking it constantly. Here's why. Consider this c

Re: Efficiency of using array as stack

2013-03-23 Thread bearophile
Ivan Kazmenko: so, formally, everything is by the letter of the law so far. I think here D arrays are working according to their specs. Call assumeSafeAppend every time you want to append with no reallocations. Bye, bearophile

Efficiency of using array as stack

2013-03-23 Thread Ivan Kazmenko
Hello again! Today, I had trouble using D built-in dynamic array as a stack: it timed out badly. I have then tried to reduce the problem down to a minimal example. I am using DMD 2.062. Now, I have this sample program. It creates an array of length one million, and then keeps adding an el