www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6898] New: Some built-in optimizations for tuples


           Summary: Some built-in optimizations for tuples
           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

One of the disadvantages of using library-defined types is the relative lack of
higher-level optimizations done on them. Tuples are a fundamental data type,
about as much fundamental as arrays, and they are meant to be used everywhere
in programs; and I don't think it's wise to rely too much on D back-ends to
optimize tuple operations a lot. So I think it's a job for the D front-end.

As example of such basic tuple optimizations to be implemented in a good D
front-end, I expect the fib2 program below to produce an assembly code similar
to the assembly of fib1 (in DMD 2.057head this optimization is not done.
Similar basic optimization is needed to use tuples freely in D code, otherwise
D tuples are usable only in non performance critical code, and their usefulness
is reduced significantly):

import std.stdio, std.bigint, std.algorithm, std.range, std.typecons;

T fib1(T)(T n) {
    auto a = cast(T)1;
    auto b = a;
    foreach (i; cast(T)1 .. n) {
        auto aux = b;
        b = a + b;
        a = aux;
    return a;

T fib2(T)(T n) {
    auto ab = tuple(cast(T)1, cast(T)1);
    foreach (i; cast(T)1 .. n)
        ab = tuple(ab[1], ab[0] + ab[1]);
    return ab[0];

void main() {
    foreach (i; BigInt(1) .. BigInt(20))
        write(fib1(i), " ");

    foreach (i; BigInt(1) .. BigInt(20))
        write(fib2(i), " ");

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