Skip to content

perf(nif): cache reply and dtype atoms at load - #136

Merged
ausimian merged 1 commit into
mainfrom
perf/issue-135-cache-atoms
May 31, 2026
Merged

perf(nif): cache reply and dtype atoms at load#136
ausimian merged 1 commit into
mainfrom
perf/issue-135-cache-atoms

Conversation

@ausimian

Copy link
Copy Markdown
Owner

Closes #135. Defines the reply atoms (:ok/:error/:stopped/:argument/:runtime/:unknown) and dtype-kind atoms (:f/:bf/:s/:u/:c/:pred) as static fine::Atoms in a new c_src/emily/atoms.hpp. FINE registers namespace-scope atoms and builds their terms once in the load callback (init_atoms), and Encoder<Atom> returns the cached term — so the reply builders (async.hpp, emily_nif.cpp) and from_mlx_dtype no longer do a per-call atom-table lookup. Covers all atom creation in Emily's own NIF code; fine::Ok<> etc. already use FINE's internal cached atoms.

Correctness-neutral — the dtype assertions (Native.dtype/1{:f, 32} …) and async :ok/:error/:stopped reply tests pass unchanged; the NIF loading cleanly confirms the static-atom registration has no init-order problem. mix precommit green (543 tests). Micro-optimization (the lookup is ~tens of ns, noise next to a GPU op), done for the idiomatic cached-atom pattern.

Atoms on the async-reply path (`:ok` on every op, plus
`:error`/`:stopped`/`:argument`/`:runtime`/`:unknown`) and the Nx dtype
"kind" atoms (`:f`/`:bf`/`:s`/`:u`/`:c`/`:pred`) were built at point of
use with enif_make_atom / fine::Atom("..."), redoing the atom-table
lookup on each call.

Define them once as static fine::Atom objects (emily/atoms.hpp). fine
registers namespace-scope atoms and creates their terms in the NIF load
callback, and Encoder<Atom> then returns the cached term directly. The
reply builders and from_mlx_dtype now reference these cached atoms.

Correctness-neutral (verified by the dtype and async tests); removes the
per-reply atom-table lookup from the hot path.

Closes #135
@ausimian
ausimian merged commit 781cb2b into main May 31, 2026
3 checks passed
@ausimian
ausimian deleted the perf/issue-135-cache-atoms branch May 31, 2026 11:22
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.

Cache NIF reply atoms at load instead of per-call enif_make_atom

1 participant