Bug 5788 – Better optimize [] (empty dynamic array literals)

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-03-27T14:42:14Z
Last change time
2024-08-14T23:16:24Z
Keywords
bootcamp, performance
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-03-27T14:42:14Z
A little D2 program: int[] foo() { return []; } int[] bar() { return null; } void main() {} The asm of the two functions, compiling it with DMD 2.052: dmd -O -release -inline test2.d _D5test23fooFZAi comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ push 0 push EAX call near ptr __d_arrayliteralT mov EDX,EAX add ESP,8 pop ECX xor EAX,EAX ret _D5test23barFZAi comdat xor EAX,EAX xor EDX,EDX ret I am not sure, but I think it's better to compile foo() to the same assembly as bar().
Comment #1 by dlang-bugzilla — 2011-04-01T19:03:12Z
No! Often it is extremely useful to distinguish an empty array from a null one. (You do that with "is null"). "Fixing" this will break a lot of my code.
Comment #2 by dlang-bugzilla — 2011-04-01T19:08:43Z
Erm, never mind, [] still creates a null array. I just remembered I had to do something more tricky in my code to make empty non-null arrays. https://github.com/CyberShadow/RABCDAsm/blob/cecff913/abcfile.d#L1094
Comment #3 by bearophile_hugs — 2013-07-03T11:19:34Z
currently this code: void main() { int[] emptyArray = []; } Produces: __Dmain comdat L0: push EAX mov EAX,offset FLAT:_D11TypeInfo_Ai6__initZ push 0 push EAX call near ptr __d_arrayliteralTX add ESP,8 xor EAX,EAX pop ECX ret
Comment #4 by dmitry.olsh — 2018-05-22T15:30:36Z
Still here with DMD 2.080: 0000000000000000 <_D4dbug3fooFZAi>: 0: 55 push %rbp 1: 48 8b ec mov %rsp,%rbp 4: 31 f6 xor %esi,%esi 6: 48 8b 3d 00 00 00 00 mov 0x0(%rip),%rdi # d <_D4dbug3fooFZAi+0xd> d: e8 00 00 00 00 callq 12 <_D4dbug3fooFZAi+0x12> 12: 48 89 c2 mov %rax,%rdx 15: 31 c0 xor %eax,%eax 17: 5d pop %rbp 18: c3 retq 19: 00 00 add %al,(%rax) ... Disassembly of section .text._D4dbug3barFZAi: 0000000000000000 <_D4dbug3barFZAi>: 0: 55 push %rbp 1: 48 8b ec mov %rsp,%rbp 4: 31 c0 xor %eax,%eax 6: 31 d2 xor %edx,%edx 8: 5d pop %rbp 9: c3 retq ...
Comment #5 by dkorpel — 2024-08-14T23:16:24Z
This has been fixed *** This issue has been marked as a duplicate of issue 22922 ***