Thread overview
[Issue 6528] New: Private module functions optimizations
Jan 05, 2012
Trass3r
Jan 17, 2013
yebblies
August 18, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6528

           Summary: Private module functions optimizations
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2011-08-18 14:20:15 PDT ---
The "private" attribute for module-level functions offers some optimization opportunities that I think DMD/D-front-end is not using.

Example 1, space optimization: I like the fact that many functions are able to run both at run-time and compile-time, like some string functions. But I think that often user-define compile-time functions are never called at run-time. If such functions are also private, then they can't be called from other modules:

module Foo;
private int sqr(in int x) pure { return x; }
enum y = sqr(10);
void main() {}


In this case I think the compiler is free to not put the implementation of sqr() into the final binary, saving binary space.

---------------------

Example 2, performance optimization: if a global function is private, the compiler is free to change and optimize its signature. An example function:


private void foo(int[] a1, int[] a2) {}
void main() {
    int n = 100; // run-time value
    auto a3 = new int[n];
    auto a4 = new int[n];
    foo(a3, a4);
}


I think the compiler is free to optimize it into something like this (this is faster because now the function receives only 3 words instead of 4. If foo() gets called really many times this is able to make a certain performance difference):

private void foo(int* a1, int* a2, size_t a1a2len) {}
void main() {
    int n = 100;
    auto a3 = new int[n];
    auto a4 = new int[n];
    foo(a3.ptr, a4.ptr, n);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6528



--- Comment #1 from bearophile_hugs@eml.cc 2011-08-20 01:14:33 PDT ---
Another optimization example:

private void foo(int[] a) {}
void main() {
    int[100] array;
    foo(array);
}


Converted to:

private void foo(ref int[100] a) {}
void main() {
    int[100] array;
    foo(array);
}

------------------

The optimization is possible if foo has more than one call from the module, if the body of foo is short:

private void foo(int[] a) {
    // this is a short function
}
void main() {
    int[100] array1;
    foo(array1);
    int[200] array2;
    foo(array2);
}


Converted to:

private void foo(size_t N)(ref int[N] a) {
    // this is a short function
}
void main() {
    int[100] array1;
    foo(array1);
    int[200] array2;
    foo(array2);
}

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


Trass3r <mrmocool@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |diagnostic, performance
                 CC|                            |mrmocool@gmx.de


--- Comment #2 from Trass3r <mrmocool@gmx.de> 2012-01-05 14:33:18 PST ---
Also there should be a warning if you define a private function and not use it (including CTFE). Just like in C.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 17, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=6528


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies@gmail.com


--- Comment #3 from yebblies <yebblies@gmail.com> 2013-01-17 13:36:39 EST ---
The first case here is already done by the linker.

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