Bug 4631 – const array literal rebuilt on every usage
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-08-12T00:52:00Z
Last change time
2010-09-08T20:11:39Z
Keywords
performance, wrong-code
Assigned to
nobody
Creator
r.sagitario
Comments
Comment #0 by r.sagitario — 2010-08-12T00:52:32Z
This assertion in this code fails:
module test;
const int[] arr = [ 1, 2, 3 ];
void main()
{
assert(arr.ptr == arr.ptr);
}
Here's the assembly for main:
__Dmain comdat
assume CS:__Dmain
L0: push EBX
push ESI
push 3
push 2
push 1
push 3
mov EAX,offset FLAT:_D12TypeInfo_xAi6__initZ
push EAX
call near ptr __d_arrayliteralT
add ESP,014h
mov ECX,EAX
mov EBX,3
push ECX
push 3
push 2
push 1
push 3
mov EDX,offset FLAT:_D12TypeInfo_xAi6__initZ
push EDX
call near ptr __d_arrayliteralT
add ESP,014h
mov ECX,EAX
mov EAX,3
mov ESI,ECX
pop ECX
cmp ECX,ESI
je L4E
mov EAX,7
call near ptr _D4test8__assertFiZv
L4E: xor EAX,EAX
pop ESI
pop EBX
ret
__Dmain ends
The array literal is constructed on every use, even though the constant array is written to the _DATA segment of the object file. I tried increasing the array size if there is some threshold, but with about 50 elements it's still the same.
using "const(int)[] arr = [ 1, 2, 3 ];" works as expected.
This looks similar to bug 2237, but the code here works ok for D1 while bug 2237 is D1 only.
Comment #1 by dsimcha — 2010-09-08T20:11:39Z
*** This issue has been marked as a duplicate of issue 4298 ***