Bug 5198 – Appender much slower when appending ranges of elements than individual elements
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2010-11-10T11:23:00Z
Last change time
2010-12-27T11:20:47Z
Assigned to
schveiguy
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2010-11-10T11:23:42Z
Given an appender Appender!string a, the following code should be roughly equivalent performance-wise:
a.put(' ');
a.put(" ");
But as shown by this code, the version that puts an individual character is an order of magnitude faster.
import std.stdio;
import std.date;
import std.array;
void f0()
{
Appender!string a;
a.reserve(100_000_000);
foreach(i; 0 .. 100_000_000)
{
a.put(' ');
}
}
void f1()
{
Appender!string a;
a.reserve(100_000_000);
foreach(i; 0 .. 100_000_000)
{
a.put(" ");
}
}
void main()
{
auto r = benchmark!(f0)(1);
writeln(r, "ms");
r = benchmark!(f1)(1);
writeln(r, "ms");
}
Results:
[2433]ms
[13276]ms
Swapping the order of testing does not change the situation.
Comment #1 by andrei — 2010-11-10T11:55:35Z
Great catch! I'm marking this as "assigned" as I see you've already assign it to yourself.
Long term I wonder whether we should include performance tests as part of build acceptance tests.
Comment #2 by schveiguy — 2010-12-27T11:20:47Z
turns out the appender was not reserving any extra space when appending an array, it was simply reallocating -- just enough to hold the existing data + the new data (in this example, 1 more element). I fixed it so the same algorithm used to expand the array for one element is now used to append an array of elements.
changeset http://www.dsource.org/projects/phobos/changeset/2237