Skip to content

Optimizations - #77

Open
gaymeowing wants to merge 19 commits into
ffrostfall:mainfrom
gaymeowing:optimizations
Open

gaymeowing wants to merge 19 commits into
ffrostfall:mainfrom
gaymeowing:optimizations

Conversation

@gaymeowing

@gaymeowing gaymeowing commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

graph

  • node kind and state are stored in bitflags instead of the evaluation and state string enums; usages of the evaluation string enum are now replaced with references to graph.evaluation.
  • select graph functions are annotated with @native
  • edges store their index in the other node's array so they can be removed in O(1); slots are stored as u16s in a buffer, based on Solid's sourceSlots/observerSlots
  • edges are reused between runs by matching reads against the last pass's dependencies by position, like Solid 2.0's link()
  • reading the same node more than once in a pass only adds one edge, using a per-pass epoch like Solid 2.0's per-pass _depGen
  • children are removed from their parent in O(1) through child_index
  • children and cleanups share a frozen empty table until the first one is added
  • added EffectNode for nodes nothing reads (effects, deferred, bindings, reactive children, tags), which skips the dependents fields
  • cleanups are stored as values directly, instead of a bunch of closures
  • only lazy nodes compare results and bump versions, since eager and deferred nodes are never read
  • is_similar checks == first
  • no yield checks only run in dev mode
  • pending_eval has its length tracked via an upvalue
  • processing_nodes is weak so that dead threads don't get kept around

threads

  • scheduler render callbacks and async callbacks now reuse threads
  • interval now only runs its callback on a separate thread in dev mode

instances

  • bind sets .Parent last
  • bind connects events after properties are set, so handlers don't fire for the initial values (taken from vide)
  • reactive children store everything in one record and track children with an update id instead of swapping 2 sets
  • create caches one constructor per class and no longer wraps Instance.new() in a pcall()

misc

  • Removed per-call closures from root, source and switch_delay
  • switch_delay builds its result array when it updates instead of on every read
  • for_values no longer invalidates its own node from destroy
  • lerp uses vector.lerp() for vectors
  • dev mode is enabled with fluid.dev() instead of _G.__DEV__, and yields in effects and intervals are only caught in dev mode
  • reactive function errors are raised on a detached thread with logging.report_error instead of being warned

bug fixes

  • for_keys destroyed and recreated keys whose value was false on every update:

    local done = fluid.source({ homework = false })
    local rows = fluid.for_keys(done, function(is_done, task)
    	print("created", task)
    	return task
    end)
    
    rows() -- OUTPUT: created homework
    done({ homework = false, dishes = true })
    rows()
    -- OUTPUT (before):
    -- created dishes
    -- created homework
    -- OUTPUT (now):
    -- created dishes
  • readers lost their dependency on a derive that was dirty but didn't change, so they stopped updating:

    local count = fluid.source(1)
    local label = fluid.source("a")
    local refresh = fluid.source(0)
    
    local positive = fluid.derive(function()
    	return count() > 0
    end)
    local text = fluid.derive(function()
    	return if positive() then label() else "none"
    end)
    
    fluid.deferred(function()
    	refresh()
    	print(text())
    end) -- OUTPUT: a
    
    count(2) -- positive reruns but stays true
    refresh(1) -- OUTPUT: a
    label("b")
    -- OUTPUT (before): nothing
    -- OUTPUT (now): b
  • many effects writing sources in one update causes a stack overflow:

    local source = fluid.source(0)
    
    for _ = 1, 300 do
    	local mirrored = fluid.source(0)
    	fluid.effect(function()
    		mirrored(source())
    	end)
    	fluid.effect(function()
    		mirrored()
    	end)
    end
    
    source(1)
    -- OUTPUT (before), repeated 300 times:
    -- [fluid]: Reactive function errored ./src/reactive/graph.luau:284: Reactive function errored C stack overflow [effect_fn_errored]
    -- OUTPUT (now): nothing
  • destroyed nodes kept rerunning with stale values, and more of them piled up every time their owner reran:

    local source = fluid.source(0)
    
    fluid.effect(function()
    	local value = source()
    	fluid.untrack(function()
    		fluid.effect(function()
    			print(value, source())
    		end)
    		return nil
    	end)
    end) -- OUTPUT: 0 0
    
    source(1)
    -- OUTPUT (before):
    -- 1 1
    -- 0 1
    -- OUTPUT (now):
    -- 1 1
    
    source(2)
    -- OUTPUT (before):
    -- 2 2
    -- 1 2
    -- 0 2
    -- OUTPUT (now):
    -- 2 2
  • nodes that errored inside untrack silently never ran again:

    local source = fluid.source(0)
    
    fluid.effect(function()
    	local value = source()
    	print("ran with", value)
    
    	fluid.untrack(function()
    		if value == 1 then
    			error("oops")
    		end
    		return nil
    	end)
    end) -- OUTPUT: ran with 0
    
    source(1)
    -- OUTPUT:
    -- ran with 1
    -- [fluid]: Reactive function errored ./src/reactive/graph.luau:284: Reactive function errored example.luau:9: oops [effect_fn_errored]
    
    source(2)
    -- OUTPUT (before): nothing
    -- OUTPUT (now): ran with 2
  • effects that read a derive mid evaluation kept the derive's stale value:

    local source = fluid.source(0)
    local mirrored = fluid.source(0)
    
    local doubled = fluid.derive(function()
    	local value = source()
    	mirrored(value) -- reruns the effect while doubled is still running
    	return value * 2
    end)
    
    fluid.effect(function()
    	mirrored()
    	print(doubled())
    end) -- OUTPUT: 0
    
    source(1)
    -- OUTPUT (before): 0
    -- OUTPUT (now): 2

behavior changes

  • dev mode is enabled with fluid.dev() instead of _G.__DEV__, with yields in effects and intervals only caught in dev mode
  • reactive function errors are raised on a separate thread with logging.report_error instead of using warn

tooling

  • added the benchmark that was used for doing graph optimizations under the bench dir
  • fixed testkit's convert_units looping forever on negative values
  • unit tests run in dev mode, with new tests for the fixes above

* add fluid.dev() to toggle dev mode, replacing the graph's hardcoded dev_mode flag and _G.__DEV__
* remove types/globals.d.luau and its definition file setting
* unit tests run with dev mode enabled
* render callbacks run on reused runner threads instead of task.spawn creating a thread for each one, a callback that yields keeps its thread and a new one gets created for the rest
* type scheduler's cancel, since coroutine.close has returns and task.cancel doesn't
* add a test for a yielding callback not stalling the others
* for_keys keeps keys whose value is false, instead of destroying and recreating them every update
* store node kind and state as bitflags (EAGER/DEFERRED/LAZY/ASYNC in the low bits, RUNNING/DIRTY/DEAD above them), instead of the evaluation & state strings
* replace the evaluation string enum with graph.evaluation
* add EffectNode for nodes that no other node depends on (effects, deferred, tags)
* add graph.refresh_node, which brings a node up to date without tracking it as a dependency
* dependency edges store their position in the other node's array as u16 slots in buffers, so they're removed in O(1)
* reads are matched against the last run's dependencies by position, so edges read in the same order get kept instead of being removed and re-added every run
* reading the same node multiple times in one run only adds one edge
* children are removed from their parent in O(1) through child_index
* children and cleanups start as a shared frozen empty table until the first one is added
* cleanups store values as is (functions, threads, connections, instances, objects) instead of wrapping each one in a closure, cleanup.luau only cleans up a value now and fluid.cleanup is defined in init.luau, since the graph requires cleanup
* threads passed to fluid.cleanup get cancelled through scheduler.cancel
* only lazy nodes compare their result & bump last_evaluation, eager and deferred nodes are never read
* track verified_at separately from last_evaluation, fixes nodes dropping a dependency on a dirty derive that reran without changing
* nested invalidations only evaluate what they queued, fixes a stack overflow when many effects set sources in one invalidation
* dirty dependencies of a pending node get evaluated on their own first, so an erroring dependency doesn't stop the node from running
* erroring nodes are reported with logging.report_error instead of logging.warn
* nodes whose dependency errored are marked clean, so they keep updating
* destroyed nodes are marked dead so queued ones don't rerun, including nodes destroyed by their owner rerunning mid update
* restore a node's effect after it errors inside untrack, fixes the node never running again
* nodes destroyed by their own effect drop the dependencies they read afterwards
* changes made while a node is running still count as newer than its run
* reading a node mid evaluation returns its cached value instead of erroring
* is_similar checks == first
* yield checks and the self dependency assert only run in dev mode, outside of it functions run on the caller's thread through xpcall
* run_as_unsafe hardcodes 2 returns instead of passing a varadic through
* rename processing_node to processing_nodes as to better reflect the fact its a hash map that holds multiple nodes
* processing_nodes is weak, so threads that errored or got cancelled can be collected
* flush_deferred_nodes is passed to scheduler.defer directly instead of a closure
* assert_scope returns Node<T>
* effect & deferred create effect nodes, derive caches evaluate_node, tags pushes its cleanup straight to the graph
* bind now sets the Parent property last
* bind connects events after the properties are set, so handlers don't fire for the initial values
* bind & changed push connections as cleanups directly instead of wrapping them in a closure
* bind skips reading properties for values that aren't functions, since only functions can be event handlers
* property bindings & reactive children create effect nodes
* reactive children store their instance, function & children in one record so their effect isn't a closure, with children tracked using an update id instead of swapping 2 sets
* an instance that's in a reactive child's result more than once only gets parented once
* root runs fn through a packing function instead of creating a closure every call, and passes false as the parent when there's no scope
* source checks how many args it got inline instead of through get_argument_from, and caches the graph functions it calls
* switch_delay's destroy functions are at the file scope instead of being closures created for every switch
* switch_delay builds its result array when it updates instead of on every read, reads return a copy so consumers mutating it don't change the actual values
* interval only checks for yields in dev mode, otherwise it calls its function with xpcall on the caller's thread instead of creating a thread every tick
* hoist interval's silent yield handler instead of creating a closure every failing tick, and cache 1 / hz
* remove reactive_fn_calls.call, it was just an unused xpcall wrapper
* add a test for interval running on the caller's thread outside dev mode

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant