From a C++/JS benchmark

bearophile bearophileHUGS at lycos.com
Sat Aug 6 16:45:53 PDT 2011


Walter:

> A dynamic array is two values being passed, a pointer is one.

I know, but I think there are many optimization opportunities. An example:


private void foo(int[] a2) {}
void main() {
    int[100] a1;
    foo(a1);
}


In code like that I think a D compiler is free to compile like this, because foo is private, so it's free to perform optimizations based on just the code inside the module:

private void foo(ref int[100] a2) {}
void main() {
    int[100] a1;
    foo(a1);
}


I think there are several cases where a D compiler is free to replace the two values with just a pointer.


Another example, to optimize code like this:

private void foo(int[] a1, int[] a2) {}
void main() {
    int n = 100; // run-time value
    auto a3 = new int[n];
    auto a4 = new int[n];
    foo(a3, a4);
}


Into something like this:

private void foo(int* a1, int* a2, size_t a1a2len) {}
void main() {
    int n = 100;
    auto a3 = new int[n];
    auto a4 = new int[n];
    foo(a3.ptr, a4.ptr, n);
}

Bye,
bearophile


More information about the Digitalmars-d mailing list