Skip to content

Performance: Type Hints - #45

Merged
codeboost merged 1 commit into
masterfrom
perf-type-hints
Nov 3, 2026
Merged

codeboost merged 1 commit into
masterfrom
perf-type-hints

Conversation

@codeboost

Copy link
Copy Markdown
Owner

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.update and String.getBytes by reflection, so db-key-hash took about 2 µs against about 70 ns for the SHA-1 digest itself.

This adds type hints to remove the reflection from:

  • key hashing in db-key-hash
  • hash map and set lookups (map-contains-key?, set-contains?, smap-contains-key?)
  • map and set iteration (map-seq, set-seq)
  • keypath write cursors (keypath-cursor, write-cursor-for-key, map-write-cursor-storing-key!, smap-write-cursor)
  • the XITDBDatabase fields used by deref and swap!, plus open-database, replica-state and snapshot-memory-db

deref-at and xitdb-swap-with-lock! move below the XITDBDatabase deftype so their argument can be hinted. Both stay public and behave the same.

In a :memory database (ns/op, before → after):

Operation Before After
db-key-hash, keyword 2,084 151
get-in with a keyword key 5,603 1,585
contains? on a set 6,182 1,313
seq over a 1,000-entry set 1.50 ms 0.39 ms
swap! with assoc-in 18,164 7,263

Only freeze! still reflects: the wrapped Read* types share no interface that exposes their cursor, and freeze! isn't on a hot path.

The map and set iterators on the write side return key/value pairs whose class redeclares keyCursor and valueCursor. 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

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
codeboost merged commit 5ed7fa4 into master Nov 3, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant