Thread overview
[Issue 4298] New: Constant array translated to unnecessary array literal creation
Jun 09, 2010
Shin Fujishiro
Jun 09, 2010
Shin Fujishiro
Jun 15, 2010
Shin Fujishiro
Jun 15, 2010
Shin Fujishiro
Jul 25, 2010
David Simcha
Sep 09, 2010
David Simcha
Feb 02, 2012
yebblies
June 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298

           Summary: Constant array translated to unnecessary array literal
                    creation
           Product: D
           Version: 2.041
          Platform: x86
        OS/Version: All
            Status: NEW
          Keywords: performance
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: rsinfu@gmail.com


--- Comment #0 from Shin Fujishiro <rsinfu@gmail.com> 2010-06-08 17:57:52 PDT ---
Using constant (const or immutable) dynamic array causes unnecessary array literal creation if the constant array has an initializer.

This behavior degrades performance, and also causes the bug 4297.

Test code and the disassembled output:
--------------------
immutable int[] TABLE = [ 1, 2, 3 ];
void foo()
{
    const(int)[] tab = TABLE;
}
--------------------
_D4test3fooFZv:
        push    EBP
        mov    EBP,ESP
        push    EBX
        push    3
        push    2
        push    1
        push    3
        mov    EAX,offset FLAT:_D12TypeInfo_Axi6__initZ@SYM32
        push    EAX
        call      _d_arrayliteralT@PC32
        mov    ECX,EAX
        mov    EBX,3
        add    ESP,014h
        pop    EBX
        pop    EBP
        ret
--------------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298


Shin Fujishiro <rsinfu@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #1 from Shin Fujishiro <rsinfu@gmail.com> 2010-06-08 18:02:25 PDT ---
Patch against DMD r526:
====================
--- src/optimize.c
+++ src/optimize.c
@@ -58,10 +58,9 @@ Expression *expandVar(int result, VarDeclaration *v)
             return e;
         }

-        Type *tb = v->type->toBasetype();
         if (result & WANTinterpret ||
             v->storage_class & STCmanifest ||
-            (tb->ty != Tsarray && tb->ty != Tstruct)
+            v->type->isscalar()
            )
         {
             if (v->init)
====================

The if condition is changed so that only scalar constants are expanded. Affected types are Tarray, Taarray, Tclass and Tdelegate.  Arrays and associative arrays should not be expanded (the reported problem); classes and delegates are not necessary to be expanded.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298


Shin Fujishiro <rsinfu@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|patch                       |


--- Comment #2 from Shin Fujishiro <rsinfu@gmail.com> 2010-06-15 15:15:15 PDT ---
(In reply to comment #1)

With the patch in comment #1, this example does not link:
--------------------
void main()
{
    immutable s = "abc";
    auto t = s[0 .. $ - 1];
}
--------------------
% dmd -run test
test.o(.text._Dmain+0x1a): In function `_Dmain':
: undefined reference to `_D4test4mainFZv8__dollarxk'
--------------------

The link error does not occur if the dollar $ is replaced with s.length.

The real cause of the link error may be elsewhere (only $ causes a link error?), but the patch itself doesn't work anyway.  So I remove the 'patch' keyword.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 15, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298


Shin Fujishiro <rsinfu@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
         Depends on|                            |3779


--- Comment #3 from Shin Fujishiro <rsinfu@gmail.com> 2010-06-15 16:26:14 PDT ---
(In reply to comment #2)
> The link error does not occur if the dollar $ is replaced with s.length.
> 
> The real cause of the link error may be elsewhere (only $ causes a link error?), but the patch itself doesn't work anyway.  So I remove the 'patch' keyword.

The linker error about __dollar is caused by bug 3779.  I think the patch in comment #1 is valid if that bug is fixed.  Restored the patch keyword and added a dependency to bug 3779.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 25, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha@yahoo.com


--- Comment #4 from David Simcha <dsimcha@yahoo.com> 2010-07-25 10:09:58 PDT ---
*** Issue 3370 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4298


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.sagitario@gmx.de


--- Comment #5 from David Simcha <dsimcha@yahoo.com> 2010-09-08 20:11:40 PDT ---
*** Issue 4631 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 02, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=4298


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies@gmail.com
         Resolution|                            |DUPLICATE


--- Comment #6 from yebblies <yebblies@gmail.com> 2012-02-02 14:27:49 EST ---
*** This issue has been marked as a duplicate of issue 2356 ***

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