Skip to content

Commit ede2986

Browse files
Pack the hash beside the value in map entries
String, Int64 and object-keyed map entries were laid out value, key, hash, next. With an Int value that left 4 bytes of padding after the value and 4 after the hash: a StringMap<Int> entry took 40 bytes on 64-bit, 48 with its header and alignment. Laying them out key, hash, value, next makes it 32. Entries with 8 or 16-byte values keep their size. Measured on Windows x64: a 200,000-entry StringMap<Int> holds 9.6 MB instead of 11.2 MB (stock hxcpp 4.3.53: 9.4 MB, with half the bucket array); map_object peak memory 44.1 MB -> 41.6 MB; map_string 148.9 MB -> 147.9 MB, against 114.8 MB for stock, at the same speed. That is only part of map_string's gap. Of about 33 MB of peak memory over stock, about 23 MB comes from HXCPP_ALIGN_ALLOC, which libhxcpp defines on every platform since HaxeFoundation/hxcpp#1246 and stock 4.3.53 only for Emscripten: each small allocation starts 8-byte aligned, and the benchmark keeps 400,000 key strings each built among temporaries. About 6 MB comes from the memoized hash slot of strings of 8 or more characters, and 1 MB from the lower bucket load factor, both kept for lookup speed. String data holds no atomics or doubles and could skip the alignment, but that needs a change to the allocator and the nursery's hole walker. Tests: TestMapValues in test/haxe fills String and object-keyed maps with Int, Float, String, Dynamic and Bool values, collects, compacts, removes half and checks every lookup and the iteration, and changes a map's value type as it goes; it passes before this change too, as the layout must not change behavior. TestStringHash and TestObjectHash also use Int values. Bench: map_string, map_object. Refs #370
1 parent 077e7d0 commit ede2986

4 files changed

Lines changed: 86 additions & 3 deletions

File tree

‎Changes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ libhxcpp 6.0.0 (unreleased)
88
* Sped up val_callN and val_ocallN with up to five arguments, which built an argument array for every call. They now call the fixed-arity entry points, as val_call0 to val_call3 do: a three-argument callback through val_callN went from 31M to 62M calls a second (libhxcpp #324)
99
* Sped up Std.string on deeply nested enums, which copied each parameter's text into its parent's, quadratic in the depth, and recursed once per level, so a chain a million deep overflowed the native stack. Enums are now formatted in one pass: a 2,048-deep enum formats 33x faster (libhxcpp #328)
1010
* Fixed formatting a map that holds itself, directly or through other maps, ending the program with a stack overflow. Past 5 nested maps a map now formats as "...", as arrays and anonymous objects already did (libhxcpp #343)
11+
* Map entries with Int values in StringMap, ObjectMap and Int64 maps take 8 bytes less on 64-bit: the hash sat after the key, leaving padding beside the value. A 200,000-entry StringMap<Int> holds 9.6 MB instead of 11.2 MB (libhxcpp #370)
1112
* Fixed a copy of an hx::Ref not keeping its object alive: copying one of the same type shared the original's GC root, so once the original went the collector could free the object the copy still held (libhxcpp #355)
1213
* Fixed hx::Ref::setPtr leaving its GC root at the old offset when it moved to another interface of the same object, so the collector could free the object while the reference still held it (libhxcpp #361)
1314
* Fixed an exception thrown by a finalizer leaving the collector paused: it unwound out of the collection with every other thread still stopped. The collection now finishes, running the other finalizers, and the first exception is thrown to the code that started it (libhxcpp #358)

‎src/hx/Hash.h‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,10 @@ struct TInt64Element
103103
}
104104
inline unsigned int getHash() { return hash; }
105105

106-
Value value;
106+
// hash beside a 4-byte value fills what would be padding (see TStringElement)
107107
Key key;
108108
unsigned int hash;
109+
Value value;
109110
TInt64Element<VALUE> *next;
110111
};
111112

@@ -135,9 +136,11 @@ struct TStringElement
135136
}
136137
inline unsigned int getHash() { return hash; }
137138

138-
Value value;
139+
// With value first, an Int value and the hash each left 4 bytes of
140+
// padding: 40 bytes on 64-bit where 32 hold the same fields
139141
Key key;
140142
unsigned int hash;
143+
Value value;
141144
TStringElement<VALUE> *next;
142145
};
143146

@@ -231,9 +234,10 @@ struct TDynamicElement
231234
}
232235
inline unsigned int getHash() { return hash; }
233236

234-
Value value;
237+
// hash beside a 4-byte value fills what would be padding (see TStringElement)
235238
Key key;
236239
unsigned int hash;
240+
Value value;
237241
TDynamicElement<VALUE,WEAK> *next;
238242
};
239243

‎test/haxe/TestMain.hx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class TestMain #if nme extends nme.display.Sprite #end {
2727
runner.addCase(new TestStringHash());
2828
runner.addCase(new TestObjectHash());
2929
runner.addCase(new TestWeakHash());
30+
runner.addCase(new TestMapValues());
3031
runner.addCase(new TestDeque());
3132
#if !nme
3233
runner.addCase(new file.TestFile());
@@ -84,6 +85,7 @@ class TestMain #if nme extends nme.display.Sprite #end {
8485
new TestStringHash(),
8586
new TestObjectHash(),
8687
new TestWeakHash(),
88+
new TestMapValues(),
8789
new TestDeque(),
8890
new file.TestFile(),
8991
new native.TestFinalizer(),

‎test/haxe/TestMapValues.hx‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import utest.Test;
2+
import utest.Assert;
3+
import haxe.ds.ObjectMap;
4+
import haxe.ds.StringMap;
5+
6+
class MapValueKey
7+
{
8+
public var id:Int;
9+
public function new(inId:Int) id = inId;
10+
}
11+
12+
// String and object-keyed map entries keep the hash beside the value
13+
// (libhxcpp #370). Each value type, through growth, removal, iteration and
14+
// collections, and a map that changes value type as it goes.
15+
class TestMapValues extends Test
16+
{
17+
static inline var N = 5000;
18+
19+
static function key(i:Int) return "key_" + StringTools.hex(i * 2579);
20+
21+
// Inlined, so each value type uses its own typed map store
22+
inline function check<T>(make:Int->T)
23+
{
24+
var values = [for(i in 0...N) make(i)];
25+
var smap = new StringMap<T>();
26+
var keys = [for(i in 0...N) new MapValueKey(i)];
27+
var omap = new ObjectMap<MapValueKey,T>();
28+
for(i in 0...N)
29+
{
30+
smap.set(key(i), values[i]);
31+
omap.set(keys[i], values[i]);
32+
}
33+
cpp.vm.Gc.run(true);
34+
cpp.vm.Gc.compact();
35+
for(i in 0...(N >> 1))
36+
{
37+
smap.remove(key(i * 2));
38+
omap.remove(keys[i * 2]);
39+
}
40+
var ok = true;
41+
for(i in 0...N)
42+
{
43+
var expected:Dynamic = (i & 1)==0 ? null : values[i];
44+
if (smap.get(key(i))!=expected || omap.get(keys[i])!=expected)
45+
ok = false;
46+
}
47+
var count = 0;
48+
for(v in smap)
49+
count++;
50+
for(k in omap.keys())
51+
if (omap.get(k)!=values[k.id])
52+
ok = false;
53+
Assert.isTrue(ok);
54+
Assert.equals(N >> 1, count);
55+
}
56+
57+
function testIntValues() check(i -> i * 3);
58+
function testFloatValues() check(i -> i + 0.25);
59+
function testStringValues() check(i -> "v" + i);
60+
function testDynamicValues() check((i:Int) -> (i==7 ? null : ({ v: i } : Dynamic)));
61+
function testBoolValues() check(i -> (i % 3)==0);
62+
63+
// A Dynamic map starts with Int values, then turns into one of strings
64+
function testValueTypeChange()
65+
{
66+
var m = new StringMap<Dynamic>();
67+
for(i in 0...N)
68+
m.set(key(i), i);
69+
m.set(key(0), "now a string");
70+
cpp.vm.Gc.run(true);
71+
Assert.equals("now a string", m.get(key(0)));
72+
Assert.equals(N - 1, m.get(key(N - 1)));
73+
m.set(key(1), 1.5);
74+
Assert.equals(1.5, m.get(key(1)));
75+
}
76+
}

0 commit comments

Comments
 (0)