perf(memo): remove hot-path closures via new Fiber.Var combinators#15220
Merged
Conversation
Add argument-threading combinators to Fiber.Var ([get_apply], [get_apply_map], [set_apply], [update_apply]). They behave like [get]/[set]/[update] but take a separate argument that is threaded into the continuation, so callers on hot paths can pass a hoisted, closure-free function instead of allocating a fresh [unit -> _ fiber] thunk on every call. They mirror the existing [of_thunk_apply]. Use them to remove per-call closures from the parts of Memo that run on every dependency edge and every node: - Dependency collection: [Deps_collector.run] becomes [run_apply] (threads the computation's input through [set_apply]); [add_dep_from_caller] reads the active collector via [get_apply_map] with a hoisted function; and the parallel combinators thread each element instead of wrapping it in a [fun () -> ...] thunk. - Call stack: [Call_stack.push_frame] updates the call-stack variable and runs the body in a single [update_apply] effect, dropping both the separate read and the per-push thunk. Behaviour is unchanged; this only reduces allocations on the hot path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
ba37e73 to
396f48b
Compare
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.
Remove per-call closure allocations from the parts of Memo that run on every
dependency edge and every node.
Fiber
Adds argument-threading combinators to
Fiber.Var:get_apply,get_apply_map,set_apply, andupdate_apply. They behave likeget/set/updatebut take a separate argument that is threaded into thecontinuation, so callers on hot paths can pass a hoisted, closure-free function
instead of allocating a fresh
unit -> _ fiberthunk on every call. They mirrorthe existing
of_thunk_applyand come with tests insrc/fiber/test/var_tests.ml.Memo
Uses the new combinators to drop per-call closures:
deps_collector.ml):runbecomesrun_apply(threads the computation's input via
set_apply);add_dep_from_callerreadsthe active collector via
get_apply_mapwith a hoisted function; and theparallel combinators (
parallel.ml) thread each element instead of wrapping itin a
fun () -> ...thunk.node.ml):Call_stack.push_frameupdates the call-stackvariable and runs the body in a single
update_applyeffect, dropping both theseparate read and the per-push thunk.
Behaviour is unchanged — this only reduces allocations on the hot path.