What happened

I’ve been tinkering with Plush, a toy Lisp-like language I designed for exploring interpreter and virtual machine design ideas. A key feature is actor-based parallelism with no global VM lock – allowing for truly concurrent operation—and a custom garbage collector (GC) implemented using a copying algorithm.

The initial goal was to build a performant GC capable of collecting one million live objects in under 20 milliseconds, suitable for prototyping a 3D game engine without noticeable pauses. I built a gc_many_objs.psh microbenchmark to test this performance. While the benchmark ultimately failed – taking approximately 117ms on my M5 MacBook Air—the real revelation came through a conversation with colleague Laurent Huberdeau. He pointed out a fundamental detail about Rust's default HashMap implementation, designed with built-in protection against HashDoS attacks. This protection, while secure, utilizes a computationally intensive hashing function that dramatically impacts performance when used as a lookup table for managing object copies during garbage collection.

Why it matters

The story highlights a common pitfall in optimization: pursuing convenience can mask underlying performance issues.

The seemingly logical choice to reuse the same copying algorithm for messages and the GC, driven by a desire for efficiency, inadvertently created a critical bottleneck. This isn’t just about Plush; any system relying on hashing for object tracking—particularly in memory-constrained environments—faces similar risks.

But, but, but…

The reliance on Rust's secure HashMap—and the subsequent switch to Rustc_hash—demonstrates that performance considerations aren’t always intuitive. Even with a more optimized hash table implementation, the overhead of hash collisions and the unpredictable access patterns created by the copying algorithm still resulted in significantly slower collection times (down to 43ms on my M5 laptop).

This underscores the importance of profiling and understanding how optimizations affect specific workloads—and that clever shortcuts can often lead to unforeseen consequences. The issue isn't a bad GC implementation, but rather an unexpected interaction between the copying algorithm and the hash table’s security features.

“it makes sense if you think about it. we’re copying a linked list. the list nodes are pretty small, with only a next pointer and a value field in each.”
— Dr Non

The counter-take

It's frequently assumed that faster data structures always equate to higher performance, which is simply not true. The choice of hashing algorithm—even for seemingly benign tasks—can dramatically affect overall system performance.

The AI angle

This incident highlights the critical role of understanding computational complexity and potential bottlenecks when designing systems – a key area where AI can assist by identifying patterns and predicting performance issues before they arise.