Repository navigation
Performance: Type Hints - #45
Merged
Merged
Conversation
Clojure key hashing called MessageDigest.update and String.getBytes by reflection on every keyword, string or Date key, which made db-key-hash many times slower than the SHA-1 digest itself. Hint those calls and the other reflective ones on read and write paths: hash map and set lookups, map and set iteration, keypath write cursors, and the XITDBDatabase fields used by deref and swap!. deref-at and xitdb-swap-with-lock! move below the XITDBDatabase deftype so they can take a hinted argument. In a :memory database (ns/op, before -> after): db-key-hash, keyword 2084 -> 151 get-in keyword key 5603 -> 1585 contains? on a set 6182 -> 1313 seq over a 1000-entry set 1.50 ms -> 0.39 ms swap! assoc-in 18164 -> 7263 Only freeze! still reflects: the wrapped Read* types share no interface that exposes their cursor, and freeze! isn't on a hot path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
codeboost
force-pushed
the
perf-type-hints
branch
from
November 3, 2026 11:14
787df6d to
f9225bb
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Several hot paths called Java methods by reflection, because the values involved had no type hints. The worst was Clojure key hashing: every keyword, string or Date key called
MessageDigest.updateandString.getBytesby reflection, sodb-key-hashtook about 2 µs against about 70 ns for the SHA-1 digest itself.This adds type hints to remove the reflection from:
db-key-hashmap-contains-key?,set-contains?,smap-contains-key?)map-seq,set-seq)keypath-cursor,write-cursor-for-key,map-write-cursor-storing-key!,smap-write-cursor)XITDBDatabasefields used byderefandswap!, plusopen-database,replica-stateandsnapshot-memory-dbderef-atandxitdb-swap-with-lock!move below theXITDBDatabasedeftype so their argument can be hinted. Both stay public and behave the same.In a
:memorydatabase (ns/op, before → after):db-key-hash, keywordget-inwith a keyword keycontains?on a setseqover a 1,000-entry setswap!withassoc-inOnly
freeze!still reflects: the wrapped Read* types share no interface that exposes their cursor, andfreeze!isn't on a hot path.The map and set iterators on the write side return key/value pairs whose class redeclares
keyCursorandvalueCursor. Reading them through the hinted parent type is safe: xitdb-java's constructor sets the parent's fields to the same cursor objects.All tests pass.
🤖 Generated with Claude Code