feat(compile): Phase 2 — native ops + direct calls (fib30 50s -> 0.076s, ~660x)

Two changes unlock native Janet speed in compile mode:

- Hot numeric primitives (+ - * < > <= >=) emit as native Janet SYMBOLS rather
  than the variadic core fns, so Janet's compiler uses its arithmetic/compare
  opcodes. = / not= / quot / rem / mod / division stay as core fns (their
  semantics differ from Janet's). Trade-off: the strict non-number checks are
  relaxed under compilation (documented perf-mode divergence).
- emit-invoke emits a DIRECT call (f arg...) when the callee is a function
  reference (core/local/symbol/fn), instead of wrapping every call in jolt-call.
  jolt-call is kept only for keyword/collection literals in call position
  ((:k m), ({:a 1} :a)) so IFn dispatch still works.

compiled fib(30): 3.4s -> 0.076s (native ceiling), faster than jank's 0.8s.
Updated compiler-test string assertions (core-+ -> +); compile-mode-test gains
native-op + IFn-dispatch cases; README documents compile mode. jpm test green.
This commit is contained in:
Yogthos 2026-06-05 18:00:46 -04:00
parent f74af5dbc5
commit 3cf303578e
4 changed files with 59 additions and 16 deletions

View file

@ -88,7 +88,17 @@
(eval-string ctx "(defn sum-sq [a b] (+ (sq a) (sq b)))")
(assert (= 25 (ct-eval ctx "(sum-sq 3 4)")) "defn calling earlier defn")
(eval-string ctx "(def base 100)")
(assert (= 142 (ct-eval ctx "(+ base 42)")) "compiled def referenced later"))
(assert (= 142 (ct-eval ctx "(+ base 42)")) "compiled def referenced later")
# Phase 2: native ops are emitted directly (fast), but IFn values in call
# position (keyword/map/set) still dispatch via the runtime.
(print " native ops + IFn dispatch...")
(assert (= 10 (ct-eval ctx "(+ 1 2 3 4)")) "n-ary +")
(assert (= true (ct-eval ctx "(< 1 2 3)")) "n-ary <")
(assert (= 1 (ct-eval ctx "(:a {:a 1})")) "keyword as fn")
(assert (= 1 (ct-eval ctx "({:a 1} :a)")) "map as fn")
(assert (= 2 (ct-eval ctx "(#{1 2 3} 2)")) "set as fn")
(assert (= true (ct-eval ctx "(= [1 2] [1 2])")) "= is value equality, not core-= bypass"))
# Context isolation: a def in one compiled context is invisible in another.
(let [a (init {:compile? true}) b (init {:compile? true})]