cgen: content-address and cache the compiled .so (jolt-ihdp) (#147)

compile-fn now keys the .so on a hash of the generated C + the Janet ABI + the
platform, in a persistent cache dir (default jolt-cgen under TMPDIR, override
with JOLT_CGEN_CACHE_DIR; JOLT_CGEN_NO_CACHE=1 forces a rebuild). cc runs only on
the first build of a given fn; later runs with the same source reuse the cached
.so, so the per-startup compile cost is paid once.

mandelbrot 100 whole-process wall: cold ~0.71s -> warm ~0.21s (the ~0.5s cc
cost). These cache knobs don't shape output, so they stay out of
ctx-shaping-env-vars (same as the image-cache knobs). Test asserts the .so is
content-addressed and a second compile hits the cache without the source .c.

Co-authored-by: Yogthos <yogthos@gmail.com>
This commit is contained in:
Dmitri Sotnikov 2026-06-16 17:37:35 +00:00 committed by GitHub
parent 393656d8d9
commit ce3c7df24b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 17 deletions

View file

@ -41,6 +41,9 @@
# --- behavioral equivalence (only where the toolchain is present) ---
(if (cgen/toolchain-available?)
(do
# start from a clean cache dir so the content-addressed-file count is exact
(when (os/stat "build/cgen-test")
(each f (os/dir "build/cgen-test") (os/rm (string "build/cgen-test/" f))))
(api/eval-string ctx count-point-src)
(def bc-cp (api/eval-string ctx "count-point")) # the compiled/interpreted fn
(def c-cp (cgen/compile-fn (ir-of count-point-src)
@ -63,7 +66,19 @@
(set acc (+ acc a)) (++ y))
acc)
(check "C and bytecode agree on the full n=80 grid total"
(= (grid-total c-cp 80) (grid-total bc-cp 80)))))
(= (grid-total c-cp 80) (grid-total bc-cp 80)))
# caching: the .so is content-addressed, so a second compile of the same
# fn reuses it. Drop the generated .c (keep the .so) then recompile: a
# cache hit loads the existing .so without needing cc/source again.
(def cache-files (filter |(string/has-suffix? ".so" $) (os/dir "build/cgen-test")))
(check "produced a content-addressed .so" (= 1 (length cache-files)))
(each f (os/dir "build/cgen-test")
(when (string/has-suffix? ".c" f) (os/rm (string "build/cgen-test/" f))))
(def c-cp2 (cgen/compile-fn (ir-of count-point-src)
{:dir "build/cgen-test" :name "count_point_test"}))
(check "cache hit recompiles without the source .c"
(and (callable? c-cp2) (= (c-cp2 -0.5 0.0 200) 200)))))
(print " (toolchain absent — skipping behavioral equivalence)"))
(if (= 0 failures)