Bug 16120 – dmd does not inline simple range primitives

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2016-06-04T16:27:00Z
Last change time
2016-06-06T14:47:23Z
Assigned to
nobody
Creator
greensunny12

Comments

Comment #0 by greensunny12 — 2016-06-04T16:27:21Z
observation 1) dmd doesn't inline range primitives (it's 8x slower!) observation 2) foreach with direct random access is lower in dmd (30%) simple benchmark yields ``` > dmd -release -O foo.d && ./foo 0 881 ms, 351 μs, and 9 hnsecs 1 685 ms, 888 μs, and 4 hnsecs 2 685 ms, 654 μs, and 4 hnsecs 3 7 secs, 211 ms, 530 μs, and 3 hnsecs ``` for: auto f_random_access(R)(R r) { auto sum = 0; foreach (const i; 0 .. r.length) { sum += r[i]; } return sum; } auto f_foreach(R)(R r) { auto sum = 0; foreach (const el; r) { sum += el; } return sum; } auto f_foreach_ref(R)(R r) { auto sum = 0; foreach (const ref el; r) { sum += el; } return sum; } auto f_for(R)(R r) { import std.range; auto sum = 0; for (r.popFront(); !r.empty; r.popFront()) { sum += r.front; } return sum; } void main() { import std.datetime: benchmark, Duration; import std.stdio: writeln; import std.array: array; import std.conv: to; import std.random: randomShuffle; import std.range:iota; auto arr = iota(100_000).array; arr.randomShuffle; auto i = 0; void f0(){ i += arr.f_random_access; } void f1(){ i += arr.f_foreach; } void f2(){ i += arr.f_foreach_ref; } void f3(){ i += arr.f_for; } auto rs = benchmark!(f0, f1, f2, f3)(10_000); foreach(j,r;rs) writeln(j, " ", r.to!Duration); // prevent any optimization writeln(i); }
Comment #1 by jack — 2016-06-06T14:47:23Z
*** This issue has been marked as a duplicate of issue 14943 ***