README.md

janet-num #

Small C kernels for Janet's numeric hot paths, called via core ffi - a plain shared library, not a jpm native module. It runs on any Janet build with ffi, builds from pinned source at install time, and puts no Janet symbols in the runtime.

Part of Jenny Stats, a home for Janet data and statistics packages. Jenny is just Janet.

The in-REPL reference: (doc jennystats/num) lists the families, and every public function documents its contract via (doc num/<name>). The docstrings need the typed module with docstring hoisting: jpm install https://github.com/jennystats/janet-kit (0.1.4 or newer - 0.1.4 added the full docstrings this document quotes). Without it the library computes correctly but (doc) comes back empty - and typed is also vendored in this package's jennystats/ directory, so importing from the repo tree (or a checkout on $JANET_PATH) works standalone.

Quick start #

All jenny modules install under a jennystats/ namespace directory: (import jennystats/num) - collision-proof against bare-name shadowing of core symbols or other packages, by construction.

(import jennystats/num :as num)

(def xs (seq [i :range [0 1000000]] i))
(def packed (num/pack-f64 xs))   # packed handle

The :as alias is the form used in this repo's tests; the plain import gives the same call sites with the num/ prefix. The :prefix "" form (exposing every kernel bare) is covered under Gotchas below - it shadows core bitops.

What and why, measured #

Janet's numeric loops pay interpreter dispatch per element; the kernels pay it once per call. Measured at 1M elements, kernel vs pure Janet, min-of-5, one harness:

family speedup vs pure
vmap2 (add, mul) 43-59x
vdot 38-40x
vreduce (sum, min) 114-180x
specfun (erf and friends) 40-1224x
ptukey (studentized range, pairwise-CI shapes) 617-828x
matmul 89-126x
i64 lanes 49-486x
bitops (band, bor, bxor) 22x
popcount 562x

Kernel costs are 0.4-24 ns per element; pure Janet loops run 50-65 ns per element and maps 200-350. At n=100 the floor is 2.5-8x (an ffi call costs about 30 ns). Packing is one pass - pack-f64 5.1 ns/e, unpack 7.7, pack-i32 7.6 - a round trip costs about 1.5 kernel ops, and the packed path wins at every n measured: 2.2x at n=100 with one op, 7.2-10.9x at 1k-1M, 31-93x at sixteen ops. There is no crossing point to wait for. The i64/u64 packers add a wrapper-side string-element guard and bench 51-60 ns/e.

Allocation-inclusive cells drift between runs (7-15 ns/e on the same day; allocation-free reductions move less than 5%), so ratios compare within one run. Reproduce the pack cells (min of 5, 1M elements; a cold process lands at the top of the drift band):

janet -e '(import jennystats/num)
(def xs (seq [i :range [0 1000000]] i))
(each [nm f] [["pack-f64" num/pack-f64] ["pack-i32" num/pack-i32] ["pack-u64" num/pack-u64] ["pack-i64" num/pack-i64]]
  (var best math/inf)
  (for _ 0 5 (f xs) (def t0 (os/clock)) (f xs) (def d (- (os/clock) t0)) (when (< d best) (set best d)))
  (printf "%s 1e6: %.1f ns/e\n" nm (* 1000 best)))'

Gotchas #

  • band/bor/bxor shadow the core bitops under (import jennystats/num :prefix "") - capture the core fns first if you need both.
  • Packer element errors are the primitives' native messages: ffi/write reports bad slot #1, expected number, got "x" (element index and offending value), the 64-bit conversions report can not convert number 1.5 to 64 bit signed integer. Empty arrays and string elements keep janet-num:-prefixed errors - the 64-bit primitives would parse bare strings silently (including hex), so the i64/u64 packers reject them wrapper-side. Plain doubles cap at 2^53 in the i64/u64 packers - pack larger values as (int/s64 "...") boxes, and negative u64 words as s64 boxes (bit-exact two's complement).
  • unpack-u64 returns int/u64 boxes: identity-compared (= is false for equal values), int/to-number caps at the double-safe range, and the box constructors reinterpret bits. Exact comparison route: (string (int/s64 box)) on both sides.
  • The packed layout is native-endian. Marshal buffers explicitly if they leave the machine; there is no cross-endian exchange story.
  • The kernels never allocate; buffers are Janet-owned and GC-managed. The handle's :free is a no-op kept for with compatibility - :close is what with calls.

Alternatives #

  • tarray - Janet's typed arrays: storage (u8..f64 views, stride and offset, slice and copy-bytes) as a native jpm module built per Janet version. janet-num is the compute half - kernels, reductions, dot products, bitops, specfun, matrix ops - via core ffi, source-built at install. Different halves of the same problem. No direct interop today: tarray buffers are abstracts, not Janet buffers, so a bridge would copy or take a raw-pointer lane.

Install #

Requirements: a C compiler and libc headers. Debian/Ubuntu: sudo apt install build-essential. Fedora/Red Hat: sudo dnf install gcc. macOS: xcode-select --install (clang is used automatically if gcc is absent). The build is one shared library, seconds long. Windows: run the stack under WSL2.

The kernels are a C library; build once with the pinned installer (verified sha256, idempotent):

libs/install-janet-num.sh   # builds ~/.local/lib/janet-num/libjn.so

Then either import by path, symlink the module tree's jennystats/num.janet into your module directory (under jennystats/), or:

jpm install https://github.com/jennystats/janet-num

Lib resolution on import: $JANET_NUM_LIB, then ~/.local/lib/janet-num/libjn.so, then dlopen search. The import fails with an actionable message if none loads.

Tests #

./run_tests.sh

The smoke uses pure Janet loops as the oracle (bitwise for f64 paths) and skips cleanly when libjn is absent.

License #

AGPLv3 - see LICENSE.

AI assistance #

The code in this repository was written with the assistance of a large language model and reviewed by its maintainer.