Bug 6898 – Some built-in optimizations for tuples

Status
RESOLVED
Resolution
INVALID
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-06T01:23:41Z
Last change time
2021-01-24T06:30:09Z
Assigned to
No Owner
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2011-11-06T01:23:41Z
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), " "); writeln(); foreach (i; BigInt(1) .. BigInt(20)) write(fib2(i), " "); }
Comment #1 by maxhaton — 2021-01-24T06:30:09Z