www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6528] New: Private module functions optimizations

reply d-bugmail puremagic.com writes:
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



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: -------
Aug 18 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6528




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: -------
Aug 20 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jan 05 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6528


yebblies <yebblies gmail.com> changed:

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



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: -------
Jan 16 2013