On Friday, 10 July 2020 at 11:13:51 UTC, Stanislav Blinov wrote:
On Friday, 10 July 2020 at 10:13:23 UTC, wjoe wrote:
So many awesome answers, thank you very much everyone!

Less overhead,
Using/needing it to interface with something else, and
Efficiency are very good points.

However stack memory needs to be allocated at program start. I don't see a huge benefit in allocation speed vs. heap pre-allocation, or is there?

Stack is allocated by the OS for the process when it's started. Reserving space for stack variables, including arrays, is effectively free, since the compiler assigns offsets statically at compile time.

I mean 1 allocation vs 2 isn't going to noticeably improve overall performance.

A GC allocation is way more complex than a mere bump-the-pointer. If your program is trivial enough you may actually find that one extra GC allocation is significant in its runtime. Of course, if you only ever allocate once and your program runs for ages, you won't really notice that allocation.


I think that memory for the program must be allocated when the process is created, this includes the stack, etc. but it is an allocation in computer RAM nonetheless.

A pre-allocated amount of memory for a dynamic array once is one additional allocation throughout the entire run time of the program. Now I can't make any guess about how this allocation takes place - it might be allocated via malloc, a pool, new, etc. - but it is one additional allocation. What I'm saying is even if this allocation is slow let's say 5ms, but it only happens once, that wouldn't matter to overall performance at all.

But performance isn't the focus of this question.

No. A slice is just a pointer/length pair - a contiguous view into *some* memory, regardless of where that memory came from:

Metadata is data that describes other data - a pointer/length pair, which describs a continuous view into memory, is matching that criteria, doesn't it ? An array is, by definition, a length of the same kind of element mapped into continuous memory, no ?

A dynamic array is a pointer/length pair, a slice is a pointer/length pair, too. Can a function, which is passed both, a dynamic array and a slice, tell the two apart, as in distinguish between the two ? Also, can this function distinguish a slice of a dynamic array from a slice of a static array ?

void takeASlice(scope void[] data) // can take any slice since any slice converts to void[]
{
    import std.stdio;
    writefln("%x %d", data.ptr, data.length);
}

int[10] a;
takeASlice(a); // a[]
takeASlice(a[1 .. $-1]); // a[1 .. 9]

struct S
{
    float x, y, z;
    float dx, dy, dz;
}

S s;
takeASlice((&s)[0 .. 1]); // Slicing a pointer, not @safe but can be done.
takeASlice(new int[10]); // Array, GC allocation
takeASlice([1, 2, 3, 4]); // Array literal, may or may not be GC-allocated

`takeASlice` has no knowledge of where the memory came from.

Dynamic arrays only ever come into the picture if you try to manipulate the slice itself: resize it, append to it, etc.

that it's not possible to slice a static array because the slice would technically be akin to a dynamic array and hence be incompatible.

Incompatible to what?

void foo(int[4] a){}

int[4] x;
auto slice = x[];
foo(slice); // that's incompatible, isn't it?

Thank you for your explanations :)

Reply via email to