Logos.Keyword.Registry (Logos v0.2.0)

Copy Markdown

Interns %Logos.Keyword{} structs so that reading the same keyword text twice (:a ... :a) always yields the exact same struct value.

Structs already compare structurally equal (==/Kernel.=== on two separately-built %Logos.Keyword{name: "a", ns: nil} are already true since Elixir struct equality is field-by-field) -- so interning isn't needed for ==/=== to work at all. It matters for two things Logos actually needs later:

  • Cheap identity checks in hot paths -- cond/case-style dispatch on keyword tags (e.g. Logos.Eval's try/catch matches a thrown value's tag against each catch clause's keyword tag) can compare the interned reference instead of doing a full structural walk every time, once callers choose to rely on that (this module makes it possible; it doesn't force callers to use identity instead of ==).
  • A single canonical representative per keyword text, so anything that wants to use a keyword as an ETS/Map key by identity, or wants to list "every keyword interned so far" (a debugging/introspection aid), has one place to do it -- an ordinary Kernel.==-keyed cache would work too, but a real interning table is what a Lisp reader is expected to provide, and it costs little.

Backed by a persistent_term (not an Agent): the registry is read-dominated (interned once at read time, looked up/compared constantly afterward) and global for the life of the VM, which is exactly persistent_term's sweet spot -- lookups are as cheap as reading a literal, at the cost of a slower write (interning a new keyword triggers a global GC pass over the persistent term). That write cost is paid once per distinct keyword text ever seen, not once per occurrence, so it's negligible for a reader workload.

Summary

Functions

Interns the keyword named name in namespace ns (nil for unqualified), returning the canonical struct.

Functions

intern(name, ns \\ nil)

@spec intern(String.t(), String.t() | nil) :: Logos.Keyword.t()

Interns the keyword named name in namespace ns (nil for unqualified), returning the canonical struct.