justinfagnani opened issue #15133:
Note: the bug report was written by an AI agent. I could not find an AI contribution policy, so I hope that's ok. I'll handle all communication and try the patches out locally.
Test Case
subtype-cache.zip, which holds
subtype-cache.wasm, its text formsubtype-cache.wat, andgen.py, which writes the text.The module defines, in one rec group, 38 non-final struct types and 425 final subtypes of them, weighted toward a few supertypes the way a compiler's casts go to a few AST and type classes. Its
runexport appliesref.testto an instance of each subtype against that subtype's supertype, 100,000 times: 42.5 million tests. Every test misses the exact-type fast path, so each one calls theis_subtypelibcall, and after the first round each one is answered from the store'ssubtype_check_cache.Steps to Reproduce
wasmtime run -W gc=y --invoke run subtype-cache.wasm- Time it. It prints
42500000.Expected Results
A test whose answer is in the cache should cost the libcall plus one hash-map lookup of a
u64: a few nanoseconds.Actual Results
The run takes 3.0 s on wasmtime 50.0.0, about 70 ns per test.
With the change to
NopHasher::finishbelow, the same module takes 0.28 s. I measured that on 50.0.1 embedded in our runtime, where the unchanged 50.0.1 takes 3.1 s.Versions and Environment
Wasmtime version or commit: 50.0.0 (the CLI); 50.0.1 (the
wasmtimecrate, for the comparison with the change). 48.0.5 has the same code, and its CLI takes 4.4 s.Operating system: NixOS 26.11, Linux 6.18
Architecture: x86_64 (AMD Ryzen 9 9955HX)
Extra Info
Cause.
StoreOpaque::is_subtype_cachedkeepssubtype_check_cache: HashMap<u64, bool, NopHasher>, keyed by(sub << 32) | sup, andNopHasher::finishreturns the key as the hash. hashbrown takes a key's starting bucket from the hash's low bits and its 7-bit tag from the top 7 bits. For this key:
- The tag is bits 25–31 of the subtype's
VMSharedTypeIndex, which are 0 unless the engine has more than 2^25 types. Every full slot in a probed group matches the tag, so a lookup compares keys slot by slot.- The starting bucket comes from the supertype's index alone, so every pair with the same supertype starts probing at the same group, as do supertypes whose indices are equal modulo the table size.
A hit then walks the cluster around its supertype.
TraceInfos::map(HashMap<VMSharedTypeIndex, TraceInfoLoc, NopHasher>) has the same constant tag.Change. Mixing the key in
NopHasher::finish, here with Murmur3's 64-bit finalizer:fn finish(&self) -> u64 { let mut h = self.0; h ^= h >> 33; h = h.wrapping_mul(0xff51_afd7_ed55_8ccd); h ^= h >> 33; h = h.wrapping_mul(0xc4ce_b9fe_1a85_ec53); h ^= h >> 33; h }A multiplication alone would not be enough for this key: the low bits of a product depend only on the low bits of its input, which are the supertype index, so pairs would still start at their supertype's bucket. Using hashbrown's default hasher for the two maps would also work.
Effect on a real program. We run a compiler written in a WasmGC language (Zena) on wasmtime. A compile makes about 67 million subtype checks over about 425 distinct pairs. Because the probe lengths depend on the exact type indices, its compile time also moved with changes that only shifted them. On 50.0.1, two builds of the compiler took 17.0 s and 24.4 s on the same input, though the second does less work. With the change above they take 15.6 s and 14.9 s, with byte-identical output. On 48.0.5, the first build became as slow as the second, 23.8 s against 14.4 s, when we loaded the second build's driver module into the same engine before it, which only shifts the engine's type indices.
This is separate from #13484, which would make the cache unnecessary.
justinfagnani added the bug label to Issue #15133.
justinfagnani commented on issue #15133:
I have a patch for
/crates/wasmtime/src/runtime/vm/gc/enabled.rsthat seems to fix this:<details>
<summary>Patch</summary>```patch
--- a/crates/wasmtime/src/runtime/vm/gc/enabled.rs
+++ b/crates/wasmtime/src/runtime/vm/gc/enabled.rs
@@ -41,8 +41,15 @@
.all(|chunk| chunk == &POISONED[..chunk.len()])
}-/// A hasher that doesn't hash, for use in the trace-info hash map, where we are
-/// just using scalar keys and aren't overly concerned with collision-based DoS.
+/// A cheap hasher for maps with scalar keys, such as the trace-info map and the
+/// subtype-check cache, where we aren't overly concerned with collision-based
+/// DoS.
+///
+/// It still has to mix the key. hashbrown takes an entry's bucket from the
+/// hash's low bits and its 7-bit tag from the top 7 bits, and a type index has
+/// no top bits, so an unmixed key would give every entry the same tag, and
+/// every(sub << 32) | supkey in the subtype-check cache would start probing
+/// at its supertype's bucket.
#[derive(Default)]
pub struct NopHasher(u64);@@ -58,7 +65,15 @@
impl core::hash::Hasher for NopHasher {
#[inline]
fn finish(&self) -> u64 {
- self.0
- // Murmur3's 64-bit finalizer. A multiplication alone is not enough:
- // the low bits of a product depend only on the low bits of the key.
- let mut h = self.0;
- h ^= h >> 33;
- h = h.wrapping_mul(0xff51_afd7_ed55_8ccd);
- h ^= h >> 33;
- h = h.wrapping_mul(0xc4ce_b9fe_1a85_ec53);
- h ^= h >> 33;
- h
}#[inline]
```
</details>On my Mac M1 Pro:
Unpatched:
$ time ./target/debug/wasmtime --invoke run ./subtype-cache/subtype-cache.wasm warning: using `--invoke` with a function that returns values is experimental and may break in the future 42500000 ./target/debug/wasmtime --invoke run ./subtype-cache/subtype-cache.wasm 54.33s user 0.26s system 99% cpu 54.969 totalPatched:
$ time ./target/debug/wasmtime --invoke run ./subtype-cache/subtype-cache.wasm warning: using `--invoke` with a function that returns values is experimental and may break in the future 42500000 ./target/debug/wasmtime --invoke run ./subtype-cache/subtype-cache.wasm 9.62s user 0.15s system 51% cpu 18.886 totalUnpatched,
--release:$ time ./target/release/wasmtime --invoke run subtype-cache/subtype-cache.wasm warning: using `--invoke` with a function that returns values is experimental and may break in the future 42500000 ./target/release/wasmtime --invoke run subtype-cache/subtype-cache.wasm 3.67s user 0.02s system 99% cpu 3.700 totalPatched,
--release:$ time ./target/release/wasmtime --invoke run subtype-cache/subtype-cache.wasm warning: using `--invoke` with a function that returns values is experimental and may break in the future 42500000 ./target/release/wasmtime --invoke run subtype-cache/subtype-cache.wasm 0.30s user 0.01s system 99% cpu 0.311 total
justinfagnani edited issue #15133:
Note: the bug report was written by an AI agent. I could not find an AI contribution policy, so I hope that's ok. I'll handle all communication and try the patches out locally.
Test Case
subtype-cache.zip, which holds
subtype-cache.wasm, its text formsubtype-cache.wat, andgen.py, which writes the text.The module defines, in one rec group, 38 non-final struct types and 425 final subtypes of them, weighted toward a few supertypes the way a compiler's casts go to a few AST and type classes. Its
runexport appliesref.testto an instance of each subtype against that subtype's supertype, 100,000 times: 42.5 million tests. Every test misses the exact-type fast path, so each one calls theis_subtypelibcall, and after the first round each one is answered from the store'ssubtype_check_cache.Steps to Reproduce
wasmtime --invoke run subtype-cache.wasm- Time it. It prints
42500000.Expected Results
A test whose answer is in the cache should cost the libcall plus one hash-map lookup of a
u64: a few nanoseconds.Actual Results
The run takes 3.0 s on wasmtime 50.0.0, about 70 ns per test.
With the change to
NopHasher::finishbelow, the same module takes 0.28 s. I measured that on 50.0.1 embedded in our runtime, where the unchanged 50.0.1 takes 3.1 s.Versions and Environment
Wasmtime version or commit: 50.0.0 (the CLI); 50.0.1 (the
wasmtimecrate, for the comparison with the change). 48.0.5 has the same code, and its CLI takes 4.4 s.Operating system: NixOS 26.11, Linux 6.18
Architecture: x86_64 (AMD Ryzen 9 9955HX)
Extra Info
Cause.
StoreOpaque::is_subtype_cachedkeepssubtype_check_cache: HashMap<u64, bool, NopHasher>, keyed by(sub << 32) | sup, andNopHasher::finishreturns the key as the hash. hashbrown takes a key's starting bucket from the hash's low bits and its 7-bit tag from the top 7 bits. For this key:
- The tag is bits 25–31 of the subtype's
VMSharedTypeIndex, which are 0 unless the engine has more than 2^25 types. Every full slot in a probed group matches the tag, so a lookup compares keys slot by slot.- The starting bucket comes from the supertype's index alone, so every pair with the same supertype starts probing at the same group, as do supertypes whose indices are equal modulo the table size.
A hit then walks the cluster around its supertype.
TraceInfos::map(HashMap<VMSharedTypeIndex, TraceInfoLoc, NopHasher>) has the same constant tag.Change. Mixing the key in
NopHasher::finish, here with Murmur3's 64-bit finalizer:fn finish(&self) -> u64 { let mut h = self.0; h ^= h >> 33; h = h.wrapping_mul(0xff51_afd7_ed55_8ccd); h ^= h >> 33; h = h.wrapping_mul(0xc4ce_b9fe_1a85_ec53); h ^= h >> 33; h }A multiplication alone would not be enough for this key: the low bits of a product depend only on the low bits of its input, which are the supertype index, so pairs would still start at their supertype's bucket. Using hashbrown's default hasher for the two maps would also work.
Effect on a real program. We run a compiler written in a WasmGC language (Zena) on wasmtime. A compile makes about 67 million subtype checks over about 425 distinct pairs. Because the probe lengths depend on the exact type indices, its compile time also moved with changes that only shifted them. On 50.0.1, two builds of the compiler took 17.0 s and 24.4 s on the same input, though the second does less work. With the change above they take 15.6 s and 14.9 s, with byte-identical output. On 48.0.5, the first build became as slow as the second, 23.8 s against 14.4 s, when we loaded the second build's driver module into the same engine before it, which only shifts the engine's type indices.
This is separate from #13484, which would make the cache unnecessary.
alexcrichton removed the bug label from Issue #15133.
alexcrichton added the performance label to Issue #15133.
alexcrichton added the wasm-proposal:gc label to Issue #15133.
justinfagnani commented on issue #15133:
Would it be ok if I put up a PR for this? It would be AI generated (the patch above), but I'll be sure to test it myself as well.
pchickey commented on issue #15133:
Here is our AI policy https://github.com/bytecodealliance/governance/blob/main/AI_TOOL_POLICY.md
Rather than defining our own Hasher inline, which we aren't 100% certain on the properties of, does std, hashbrown, or another highly vetted and used rust crate provide something that provides a comparable performance gain here?
MythicByte commented on issue #15133:
I have tryed some hashing algorithmen and foldhash the default of hashbrown is the same.### Wall time (s)
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 3.708 0.315 0.321 0.345 0.344 0.657 1000 3.690 0.300 0.295 0.319 0.320 0.659 4093 3.676 0.298 0.300 0.326 0.325 0.662 Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24
MythicByte edited a comment on issue #15133:
I have tryed some hashing algorithmen and foldhash the default of hashbrown is the same.
Wall time (s)
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 3.708 0.315 0.321 0.345 0.344 0.657 1000 3.690 0.300 0.295 0.319 0.320 0.659 4093 3.676 0.298 0.300 0.326 0.325 0.662 Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24
MythicByte edited a comment on issue #15133:
@pchickey @justinfagnani i tried some hashing algorithmen and foldhash the default of hashbrown is the same as your custom patch.
Wall time (s)
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 3.708 0.315 0.321 0.345 0.344 0.657 1000 3.690 0.300 0.295 0.319 0.320 0.659 4093 3.676 0.298 0.300 0.326 0.325 0.662 Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24
MythicByte edited a comment on issue #15133:
@pchickey @justinfagnani i tried some hashing algorithmen and foldhash the default of hashbrown is the same as your custom patch. The consequenz of this timing woud be to add foldhash as a dependecie. Which is already pulled in with hashbrown and replace the NopHasher.
Wall time (s)
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 3.708 0.315 0.321 0.345 0.344 0.657 1000 3.690 0.300 0.295 0.319 0.320 0.659 4093 3.676 0.298 0.300 0.326 0.325 0.662 Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24
MythicByte edited a comment on issue #15133:
@pchickey @justinfagnani i tried some hashing algorithmen and foldhash the default of hashbrown is the same as your custom patch. The consequenz of this timing woud be to add foldhash as a dependecie. Which is already pulled in with hashbrown and replace the NopHasher.
Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24
MythicByte edited a comment on issue #15133:
@pchickey @justinfagnani i tried some hashing algorithmen and foldhash the default of hashbrown is the same as your custom patch. The consequenz of this timing woud be to add foldhash as a dependecie. Which is already pulled in with hashbrown and replace the NopHasher.
Time (ns), 1-round run subtracted
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24 Some context with hasbrown is replacing the maps tested and not only the hasher
MythicByte edited a comment on issue #15133:
@pchickey @justinfagnani i tried some hashing algorithmen and foldhash the default of hashbrown is the same as your custom patch. The consequenz of this timing woud be to add foldhash as a dependecie. Which is already pulled in with hashbrown and replace the NopHasher.
Time (ns)
pad nop justinfagnani patch foldhash hashbrown FxHasher siphash 0 87.09 7.24 7.37 7.94 7.93 15.28 1000 86.61 6.84 6.72 7.29 7.31 15.27 4093 86.14 6.66 6.72 7.33 7.29 15.24 Some context with hasbrown is replacing the maps tested and not only the hasher
Last updated: Nov 19 2026 at 20:05 UTC