Clojure on Fennel Part One: Persistent Data Structures
2 days ago
- #Fennel
- #Persistent Data Structures
- #ClojureFnl
- The author started a project in 2019 called fennel-cljlib to bring Clojure features to Lua via Fennel, which grew to include lazy sequences, immutability, testing, and core.async.
- A new project, ClojureFnl, is a Clojure-to-Fennel compiler using fennel-cljlib, capable of compiling many .cljc files but with incomplete standard library support.
- The initial immutable data structures (itable and immutable red-black tree) were slow due to copy-on-write, prompting the development of a new library, immutable.fnl, with persistent data structures.
- The new library includes Persistent HAMT hash maps, hash sets, vectors, persistent red-black trees, and lazy linked lists, with branching factor 16 for maps and 32 for vectors for performance trade-offs.
- Benchmarks show persistent data structures are slower than Lua tables but usable; e.g., hash map insertion is 80.3x slower on PUC Lua, while vectors show similar ratios, with better performance on LuaJIT.
- Key operations for maps include assoc, dissoc, get, and transient variants; vectors support conj, assoc, get, pop; sorted maps use red-black trees without transients.
- Persistent lists were reimplemented with three metatables for better integration, and persistent queues use a list front and vector rear for O(1) append and removal.
- The author plans to focus on the ClojureFnl compiler in future work, having improved the underlying data structures.