Debian Codegraph @

test/hashmap_gc_test.d

raw
1 import containers : HashMap;
2 import std.stdio : writefln;
3 import core.memory : GC;
4
5 /**
6 * Generate a random alphanumeric string.
7 */
8 @trusted string randomString(uint len)
9 {
10 import std.ascii : letters, digits;
11 import std.conv : to;
12 import std.random : randomSample;
13 import std.range : chain;
14
15 auto asciiLetters = to!(dchar[])(letters);
16 auto asciiDigits = to!(dchar[])(digits);
17
18 if (len == 0)
19 len = 1;
20
21 auto res = to!string(randomSample(chain(asciiLetters, asciiDigits), len));
22 return res;
23 }
24
25 void main()
26 {
27 immutable iterationCount = 4;
28 HashMap!(string, string) hmap;
29
30 for (uint n = 1; n <= iterationCount; n++)
31 {
32 foreach (i; 0 .. 1_000_000)
33 hmap[randomString(4)] = randomString(16);
34 GC.collect();
35 hmap = HashMap!(string, string)(16);
36 GC.collect();
37
38 foreach (i; 0 .. 1_000_000)
39 hmap[randomString(4)] = randomString(16);
40 GC.collect();
41 hmap.clear();
42 GC.collect();
43
44 writefln("iteration %s/%s finished", n, iterationCount);
45 }
46 }