Reserve on dynamic arrays
cym13 via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Jul 14 00:20:46 PDT 2016
Does changing the capacity of dynamic arrays actually do anything
with respect to memory allocation? I have the following data with
DMD 2.071.1
// With dmd -profile=gc
void main() {
import std.stdio;
int[] arr = [1, 1, 1];
arr.reserve(512);
foreach (i ; 0..10)
arr ~= 42;
}
/*
* bytes allocated, allocations, type, function, file:line
* 40 10 int[] D main test.d:8
* 12 1 int[] D main test.d:4
*/
void main() {
import std.stdio;
int[] arr = [1, 1, 1];
arr.reserve(512);
foreach (i ; 0..20) // Just changing the number of elements
arr ~= 42;
}
/*
* bytes allocated, allocations, type, function, file:line
* 80 20 int[] D main test.d:8
* 12 1 int[] D main test.d:4
*/
void main() {
import std.stdio;
int[] arr = [1, 1, 1];
// Same as above, without reserve
foreach (i ; 0..20)
arr ~= 42;
}
/*
* bytes allocated, allocations, type, function, file:line
* 80 20 int[] D main test.d:8
* 12 1 int[] D main test.d:4
*/
As one can see there seem to be absolutely no difference in
allocations wether we reserve or not. I've ran more experiences,
reserving way less or more than I appened to no avail:
allocations seem to just ignore it.
Isn't the point of .reserve to perform preallocation?
More information about the Digitalmars-d-learn
mailing list