Repository navigation
perf(typecheck): compare = guards over engine terms in the arena (b-typed ~4.3x less CPU) - #96
Merged
Merged
Conversation
The native Prolog translator lifted every embedded Shen expression into a KL defun called with its term arguments materialized, so the by-hypothesis check (= (shen.deref X B) (shen.deref Y B)) copied the goal and each hypothesis out to fresh Shen lists once per hypothesis per goal. On capcov's b-typed workload that copying (plus the GC it causes) was ~85% of samples. lift_guard now compiles (= T1 T2), where each side is (shen.deref X B) or a bare term variable, to E.eqterm: a lazyderef walk over the arena that returns the same answer as (= (materialize a) (materialize b)) without allocating. Distinct atoms/vars still defer to the kernel's =, so tuples and vectors keep structural equality; car recursion is counted on the #93 stack segments. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The native Prolog translator lifts every Shen expression embedded in a rule, such as a
whentest or anisright-hand side, into a KL defun. It calls that defun with its engine-term arguments materialized, meaning deep-copied out to fresh Shen lists. One of those guards isshen.by-hypothesis's check,(when (= (shen.deref X B) (shen.deref Y B))), and it runs once per hypothesis per goal. On capcov's typecheck-heavyb-typedworkload,materialize+R.cons+ the GC they cause took ~85% of samples (LuaJITjit.p), with GC alone at 34%.lift_guardnow compiles(= T1 T2), where each side is(shen.deref X B)or a bare term variable, toE.eqterm. That is alazyderefwalk over the arena that gives the same answer as(= (materialize a) (materialize b))and allocates nothing.=on their O(1) materializations. Tuples, vectors and pvars therefore keep their exact structural=.The bare-variable form matters for a 43.1 kernel with the typecheck deref hoist (shen-sources
perf/typecheck-deref-hoist). There,by-hypothesis-hcompares the already-dereferenced X as(= X (shen.deref Y B)). This PR alone does not make shen-lua run that kernel: its newshen.by-hypothesis-hdriver is missing fromDRIVER_FNS. That is fixed in the stacked PR (kernel robustness).Results
b-typed CPU (user+sys) from
/usr/bin/time -lon the binary, 2 interleaved rounds, LuaJIT 2.1.1785763465, payload byte-identical to go-1f587de in every run:That is -79% instructions retired (661 G → 140 G mean, 4.7x) and about 4.3x less CPU (48.0 s → 11.3 s mean).
Gates (this branch, vendored 43.1)
make test: TOTAL 1774 pass, 0 fail across 28 specs (origin/main: 1762 / 27). This addstest/eqterm_spec.lua, which checks:=on all 841 pairs of a mixed term set;EQTERMfor both guard spellings and keeping the lifted path for other guards;run-kernel-tests.lua: 154 passed, 0 failed,kerneltests result: ok.🤖 Generated with Claude Code