The 185-Microsecond Type Hint
3 hours ago
- #Roughtime
- #Performance Optimization
- #Clojure
- A 'trivial' change in code yielded a 13× throughput increase in a Clojure Roughtime implementation.
- The Roughtime protocol involves cryptographic proof for secure time synchronization, where servers respond with signed certificates containing nonces and timestamps.
- The server's heavy lifting includes queueing, protocol compatibility across 16 versions, recursive Merkle tree hashing, and Ed25519 signatures.
- Initial benchmarks showed 200µs response time, with 90% runtime attributed to a simple line calculating byte array lengths.
- Adding a type hint to the `alength` function reduced encoding time from 31µs to 4µs by avoiding dynamic dispatch and reflection.
- End-to-end benchmarks showed throughput increased from 19,959 to 264,316 responses per second, a 13× improvement.
- The unexpected speedup may be due to reduced contention in reflective call paths under load.
- The lesson: even 'trivial' code can be a bottleneck, and type hints can significantly optimize performance in Clojure.