[Issue 2356] array literal as non static initializer generates horribly inefficient code.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 10 19:34:13 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=2356



--- Comment #16 from Kenji Hara <k.hara.pg at gmail.com> 2013-04-10 19:34:08 PDT ---
(In reply to comment #15)
> (In reply to comment #14)
> > New D2 fix:
> > https://github.com/D-Programming-Language/dmd/pull/1883
> 
> From the pull request (dmd -O -inline -g test  after):
> 
> 
> c:\d\test.d:18     int[3] y = [n, n, n];
> 004020aa: 6a03                    push 0x3
> 004020ac: 6a05                    push 0x5
> 004020ae: 8d4c241c                lea ecx, [esp+0x1c]
> 004020b2: 51                      push ecx
> 004020b3: e880020000              call 0x402338 __memset32
> 
> Isn't calling memset for just 3 integers slower than inlining their
> assignments? I suggest to not call memset if the number of bytes to be copied
> is so small (I think LDC is already doing similar optimizations). Maybe a
> benchmark is also useful here.

It is lowered to:
  int[3] y = void;
  y[] = n;

And currently dmd uses memset for `y[] = n;`. It is another optimization issue.

> c:\d\test.d:20     S[3] z = [s2, s2, s2];
> 004020b8: 8d542418                lea edx, [esp+0x18]
> 004020bc: 52                      push edx
> 004020bd: 8d442430                lea eax, [esp+0x30]
> 004020c1: e86affffff              call 0x402030 test.S.__cpctor c:\d\test.d:3
> 004020c6: 8d5c2418                lea ebx, [esp+0x18]
> 004020ca: 53                      push ebx
> 004020cb: 8d442434                lea eax, [esp+0x34]
> 004020cf: e85cffffff              call 0x402030 test.S.__cpctor c:\d\test.d:3
> 004020d4: 53                      push ebx
> 004020d5: 8d442438                lea eax, [esp+0x38]
> 004020d9: e852ffffff              call 0x402030 test.S.__cpctor c:\d\test.d:3
> 004020de: 83c40c                  add esp, 0xc
> 004020e1: 31c0                    xor eax, eax
> 
> If the s2 variable already contains the struct, then what's the purpose of
> those calls to 0x402030?
>
>
> In the "before" there are no calls to struct constructors:
> 
> c:\d\test.d:20     S[3] z = [s2, s2, s2];
> 00403913: 8d542474                lea edx, [esp+0x74]
> 00403917: b960014200              mov ecx, 0x420160
> 0040391c: 52                      push edx
> 0040391d: 6a03                    push 0x3
> 0040391f: 6a03                    push 0x3
> 00403921: 51                      push ecx
> 00403922: e8fd0a0000              call 0x404424 __d_arrayliteralTX
> 00403927: 83c408                  add esp, 0x8
> 0040392a: 8d542470                lea edx, [esp+0x70]
> 0040392e: 52                      push edx
> 0040392f: 89c6                    mov esi, eax

Before, cpctor(==postblit) calls are done in __d_arrayliteralTX, so they are
hidden. Now they are directly called on the stack memory z[0..3].

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list