Static arrays problem

Denis Koroskin 2korden at gmail.com
Thu Dec 18 07:03:51 PST 2008


On Thu, 18 Dec 2008 00:52:43 +0300, bearophile <bearophileHUGS at lycos.com>  
wrote:

> At the bottom of this post there are two little test programs that may  
> show a problem with static arrays.
>
> D code compiled with DMD 1.038:
> dmd test_d.d
> Compile time: 8.29 s
> Resulting exe and obj:
> 17.756.188 test_d.exe
>      2.390 test_d.map
> 17.734.261 test_d.obj
>
>
> C code compiled with DMC 8.42n:
> dmc test_c.c
> Compile time: 0.04 s
> Resulting exe:
>  51.740 test_c.exe
>
>
> C code compiled with GCC (4.2.1):
> gcc test_c.c -o test_c
> Compile time: 0.16 s
> Resulting exe:
>     16.781 test_c.exe
>
>
> C code compiled with LLVM-GCC (2.5):
> llvm-gcc test_c.c -o test_c
> Compile time: 0.11 s
> Resulting exe:
>     13.462 test_c.exe
>
>
> (The following code is just reduced toy, but the full program runs up to  
> about 13 times faster with static arrays, so I'd like to use them  
> instead of the dynamic ones.)
>
> Using obj2asm on the test_d.obj it's made mostly of:
>
> _D6test_d1aG3G65G65G130f:
> 	db	000h,000h,0c0h,07fh,000h,000h,0c0h,07fh
> 	db	000h,000h,0c0h,07fh,000h,000h,0c0h,07fh
> 	db	000h,000h,0c0h,07fh,000h,000h,0c0h,07fh
> 	db	000h,000h,0c0h,07fh,000h,000h,0c0h,07fh
> ...
>
> -------------------------
>
> // C code:
>
> #include "stdio.h"
>
> #define N 65
> #define M (N+N)
>
> float a[3][N][N][M];
> float b[4][N][N][M];
> float c[N][N][M];
>
> int main() {
>     int i, j, k;
>
>     for (i = 0; i < N; i++)
>         for (j = 0; j < N; j++)
>             for (k = 0; k < M; k++) {
>                 a[0][i][j][k] = 0.0;
>                 a[1][i][j][k] = 0.0;
>                 a[2][i][j][k] = 0.0;
>                 b[0][i][j][k] = 0.0;
>                 b[1][i][j][k] = 0.0;
>                 b[2][i][j][k] = 0.0;
>                 b[3][i][j][k] = 0.0;
>                 c[i][j][k] = 0.0;
>             }
>
>     printf("%f %f %f\n", a[2][10][10][10], a[2][10][10][10],  
> c[10][10][10]);
>     return 0;
> }
>
> -------------------------
>
> // D code, it's the same,
> // I have just changed the first 3 lines.
>
> import std.c.stdio: printf;
>
> const int N = 65;
> const int M = (N+N);
>
> float a[3][N][N][M];
> float b[4][N][N][M];
> float c[N][N][M];
>
> int main() {
>     int i, j, k;
>
>     for (i = 0; i < N; i++)
>         for (j = 0; j < N; j++)
>             for (k = 0; k < M; k++) {
>                 a[0][i][j][k] = 0.0;
>                 a[1][i][j][k] = 0.0;
>                 a[2][i][j][k] = 0.0;
>                 b[0][i][j][k] = 0.0;
>                 b[1][i][j][k] = 0.0;
>                 b[2][i][j][k] = 0.0;
>                 b[3][i][j][k] = 0.0;
>                 c[i][j][k] = 0.0;
>             }
>
>     printf("%f %f %f\n", a[2][10][10][10], a[2][10][10][10],  
> c[10][10][10]);
>     return 0;
> }
>
> Bye,
> bearophile


This is a linker issue. DMD stores a, b,c AND a.init, b.init, c.init  
(doubling data size).
Intelligent linker could strip a.init, b.init and c.init if they are never  
accessed.



More information about the Digitalmars-d mailing list