Faster Maps: Chasing Swiss Speed
After looking at Go's collector, we turned to its maps. Swiss tables have an appealing premise: keep entries compact, use a small amount of metadata to narrow the search, and avoid chasing a separate object for every mapping. ParparVM already had compact arrays and separate metadata. We were starting closer than I expected. What is Codename One? Codename One is an open-source framework for…
ParparVM map implementation gained performance by borrowing ideas from Go's maps. Swiss tables aim to keep entries compact, use small metadata to narrow searches, and avoid separate objects for each mapping. ParparVM already had compact arrays and separate metadata, putting it closer to the Swiss approach than expected.
Codename One is an open-source framework for building native apps from a single Java or Kotlin codebase. After benchmarking Go's maps, ParparVM turned to its own map implementation. A missing key in a three-million entry map took 32.7 seconds, while a benchmark key search remained healthy.
The probe sequence, string comparisons, and boxed numbers inside the table were investigated. The map's changes and wider tagged values affected costs in the same Java collection. One probe for a hit and thousands for a miss were the issues. ParparVM's HashMap stores entries in arrays and resolves collisions by probing other slots, with integer hashes preserving values and small dense integer keys close to their original positions.
If a missing key's probe enters a run of occupied keys, linear probing walks until it finds an empty slot. Deletions leave tombstones that cannot terminate the search. The benchmark expanded to include misses, tombstones, growth, string keys, and identity keys, controlling key distribution for each run.
Mean probes per miss dropped significantly after fixes. Miss-heavy workloads fell from 32.7 seconds to 44.9 ms. Random hits in large tables became slower, but the geometric mean of the broad suite barely moved. Hashtable now avoids an Entry allocation per mapping and uses a compact layout. IdentityHashMap had a different warning, with its identity hash already scrambled and collisions worsened when copied from the JDK's indexing expression.
ParparVM extended tagged immediates to Short, Character, Float, Long, and Double, reducing allocations for eligible values. The spare bits in the 64-bit representation were used as type tags, with the remaining 61 bits carrying the payload. The implementation is part of the native equality path, with string equality improvements like cached unequal hashes and native memcmp for UTF-16 arrays.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.