Optimize indexes() and values()

This commit is contained in:
aaron 2025-03-27 19:27:54 +00:00
parent 37e8e05206
commit 452ca383f7
11 changed files with 320 additions and 260 deletions

View file

@ -1,6 +1,6 @@
local graph = require "./graph"
local create_node = graph.create_node
local push_child_to_scope = graph.push_child_to_scope
local push_scope_as_child_of = graph.push_scope_as_child_of
local assert_stable_scope = graph.assert_stable_scope
local evaluate_node = graph.evaluate_node
@ -10,7 +10,7 @@ local function derive<T>(source: () -> T): () -> T
evaluate_node(node)
return function()
push_child_to_scope(node)
push_scope_as_child_of(node)
return node.cache
end
end

View file

@ -245,7 +245,7 @@ local function update_descendants<T>(root: SourceNode<T>)
update_queue.n = n0
end
local function push_child_to_scope<T>(node: SourceNode<T>)
local function push_scope_as_child_of<T>(node: SourceNode<T>)
local scope = get_scope()
if scope and scope.effect then -- do not track nodes with no effect
push_child(node, scope)
@ -302,7 +302,7 @@ return table.freeze {
push_cleanup = push_cleanup,
destroy = destroy,
flush_cleanups = flush_cleanups,
push_child_to_scope = push_child_to_scope,
push_scope_as_child_of = push_scope_as_child_of,
update_descendants = update_descendants,
push_child = push_child,
create_node = create_node,

114
src/indexes.luau Normal file
View file

@ -0,0 +1,114 @@
local flags = require "./flags"
local graph = require "./graph"
type Node<T> = graph.Node<T>
type SourceNode<T> = graph.SourceNode<T>
local create_node = graph.create_node
local create_source_node = graph.create_source_node
local push_scope_as_child_of = graph.push_scope_as_child_of
local update_descendants = graph.update_descendants
local assert_stable_scope = graph.assert_stable_scope
local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local evaluate_node = graph.evaluate_node
local destroy = graph.destroy
type Map<K, V> = { [K]: V }
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO, delay: number?): () -> { VO }
local owner = assert_stable_scope()
local subowner = create_node(owner, false, false)
local input_cache = {} :: Map<K, VI>
local output_cache = {} :: Map<K, VO>
local input_nodes = {} :: Map<K, SourceNode<VI>>
local scopes = {} :: Map<K, Node<unknown>>
local function update_children(data)
local children_need_update = false
-- remove old indexes
for i in input_cache do
if data[i] == nil then
destroy(scopes[i])
input_cache[i] = nil
output_cache[i] = nil
input_nodes[i] = nil
scopes[i] = nil
children_need_update = true
end
end
push_scope(subowner)
-- process new or changed values
for i, v in data do
local cv = input_cache[i]
if cv ~= v then
input_cache[i] = v
if cv == nil then -- create new scope and run transform
local scope = create_node(subowner, false, false)
scopes[i] = scope :: Node<any>
local node = create_source_node(v)
push_scope(scope)
local ok, result = xpcall(transform, debug.traceback, function()
push_scope_as_child_of(node)
return node.cache
end, i)
pop_scope()
if not ok then
pop_scope() -- subowner scope
error(result, 0)
end
input_nodes[i] = node
output_cache[i] = result
children_need_update = true
else -- update source
input_nodes[i].cache = v
update_descendants(input_nodes[i])
end
end
end
pop_scope()
if children_need_update then
local output_array_size = #output_cache
local output_array
-- check if the table contains a dictionary section
if output_array_size > 0 and next(output_cache, output_array_size) == nil then
output_array = table.clone(output_cache)
else
output_array = table.create(output_array_size)
for _, v in output_cache do
table.insert(output_array, v)
end
end
return output_array
else
return nil
end
end
local node = create_node(owner, function(pre_children)
return update_children(input()) or pre_children
end, {})
evaluate_node(node)
return function()
push_scope_as_child_of(node)
return node.cache
end
end
return indexes

View file

@ -14,10 +14,12 @@ local batch = require "./batch"
local context = require "./context"
local switch = require "./switch"
local show = require "./show"
local indexes, values = require "./maps"()
local indexes = require "./indexes"
local values = require "./values"
local spring, update_springs = require "./spring"()
local action = require "./action"()
local changed = require "./changed"
local timeout, update_timeouts = require "./timeout"()
local flags = require "./flags"
export type Source<T> = source.Source<T>
@ -26,17 +28,17 @@ export type Context<T> = context.Context<T>
export type context<T> = Context<T>
local function step(dt: number)
if game then
debug.profilebegin("VIDE STEP")
debug.profilebegin("VIDE SPRING")
end
if game then debug.profilebegin("VIDE STEP") end
if game then debug.profilebegin("VIDE SPRING") end
update_springs(dt)
if game then debug.profileend() end
if game then
debug.profileend()
debug.profileend()
end
if game then debug.profilebegin("VIDE SCHEDULER") end
update_timeouts(dt)
if game then debug.profileend() end
if game then debug.profileend() end
end
local stepped = game and game:GetService("RunService").Heartbeat:Connect(function(dt: number)

View file

@ -1,216 +0,0 @@
local flags = require "./flags"
local graph = require "./graph"
type Node<T> = graph.Node<T>
type SourceNode<T> = graph.SourceNode<T>
local create_node = graph.create_node
local create_source_node = graph.create_source_node
local push_child_to_scope = graph.push_child_to_scope
local update_descendants = graph.update_descendants
local assert_stable_scope = graph.assert_stable_scope
local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local evaluate_node = graph.evaluate_node
local destroy = graph.destroy
type Map<K, V> = { [K]: V }
local function check_primitives(t: {})
if not flags.strict then return end
for _, v in next, t do
if type(v) == "table" or type(v) == "userdata" or type(v) == "function" then continue end
error("table source map cannot return primitives", 0)
end
end
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
local owner = assert_stable_scope()
local subowner = create_node(owner, false, false)
local input_cache = {} :: Map<K, VI>
local output_cache = {} :: Map<K, VO>
local input_nodes = {} :: Map<K, SourceNode<VI>>
local remove_queue = {} :: { K }
local scopes = {} :: Map<K, Node<unknown>>
local function update_children(data)
-- queue removed values
for i in next, input_cache do
if data[i] == nil then
table.insert(remove_queue, i)
end
end
-- remove queued values
for _, i in next, remove_queue do
destroy(scopes[i])
input_cache[i] = nil
output_cache[i] = nil
input_nodes[i] = nil
scopes[i] = nil
end
table.clear(remove_queue)
push_scope(subowner)
-- process new or changed values
for i, v in next, data do
local cv = input_cache[i]
if cv ~= v then
input_cache[i] = v
if cv == nil then -- create new scope and run transform
local scope = create_node(subowner, false, false)
scopes[i] = scope :: Node<any>
local node = create_source_node(v)
push_scope(scope)
local ok, result = xpcall(transform, debug.traceback, function()
push_child_to_scope(node)
return node.cache
end, i)
pop_scope()
if not ok then
pop_scope() -- subowner scope
error(result, 0)
end
input_nodes[i] = node
output_cache[i] = result
else -- update source
input_nodes[i].cache = v
update_descendants(input_nodes[i])
end
end
end
pop_scope()
local output_array = table.create(#scopes)
for _, v in next, output_cache do
table.insert(output_array, v)
end
check_primitives(output_array)
return output_array
end
local node = create_node(owner, function()
return update_children(input())
end, false :: any)
evaluate_node(node)
return function()
push_child_to_scope(node)
return node.cache
end
end
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
local owner = assert_stable_scope()
local subowner = create_node(owner, false, false)
local cur_input_cache_up = {} :: Map<VI, K>
local new_input_cache_up = {} :: Map<VI, K>
local output_cache = {} :: Map<VI, VO>
local input_nodes = {} :: Map<VI, SourceNode<K>>
local scopes = {} :: Map<VI, Node<unknown>>
local function update_children(data: Map<K, VI>)
local cur_input_cache, new_input_cache = cur_input_cache_up, new_input_cache_up
if flags.strict then
local cache = {}
for _, v in next, data do
if cache[v] ~= nil then
error "duplicate table value detected"
end
cache[v] = true
end
end
push_scope(subowner)
-- process data
for i, v in next, data do
new_input_cache[v] = i
local cv = cur_input_cache[v]
if cv == nil then -- create new scope and run transform
local scope = create_node(subowner, false, false)
scopes[v] = scope :: Node<any>
local node = create_source_node(i)
push_scope(scope)
local ok, result = xpcall(transform, debug.traceback, v, function()
push_child_to_scope(node)
return node.cache
end)
pop_scope()
if not ok then
pop_scope() -- subowner scope
error(result, 0)
end
input_nodes[v] = node
output_cache[v] = result
else -- update source
if cv ~= i then
input_nodes[v].cache = i
update_descendants(input_nodes[v])
end
cur_input_cache[v] = nil
end
end
pop_scope()
-- remove old values
for v in next, cur_input_cache do
destroy(scopes[v])
output_cache[v] = nil
input_nodes[v] = nil
scopes[v] = nil
end
-- update buffer cache
table.clear(cur_input_cache)
cur_input_cache_up, new_input_cache_up = new_input_cache, cur_input_cache
local output_array = table.create(#scopes)
for _, v in next, output_cache do
table.insert(output_array, v)
end
check_primitives(output_array)
return output_array
end
local node = create_node(owner, function()
return update_children(input())
end, false :: any)
evaluate_node(node)
return function()
push_child_to_scope(node)
return node.cache
end
end
return function() return indexes, values end

View file

@ -1,7 +1,7 @@
local graph = require "./graph"
type Node<T> = graph.Node<T>
local create_source_node = graph.create_source_node
local push_child_to_scope = graph.push_child_to_scope
local push_scope_as_child_of = graph.push_scope_as_child_of
local update_descendants = graph.update_descendants
export type Source<T> = (() -> T) & ((value: T) -> T)
@ -11,7 +11,7 @@ local function source<T>(initial_value: T): Source<T>
local function update_source(...): T
if select("#", ...) == 0 then -- no args were given
push_child_to_scope(node)
push_scope_as_child_of(node)
return node.cache
end

View file

@ -6,7 +6,7 @@ local create_source_node = graph.create_source_node
local assert_stable_scope = graph.assert_stable_scope
local evaluate_node = graph.evaluate_node
local update_descendants = graph.update_descendants
local push_child_to_scope = graph.push_child_to_scope
local push_scope_as_child_of = graph.push_scope_as_child_of
local UPDATE_RATE = 120
local TOLERANCE = 0.001
@ -202,7 +202,7 @@ local function spring<T>(source: () -> T, period: number?, damping_ratio: number
return function(...)
if select("#", ...) == 0 then -- no args were given
push_child_to_scope(output)
push_scope_as_child_of(output)
return output.cache
end

View file

@ -3,7 +3,7 @@ type Node<T> = graph.Node<T>
type SourceNode<T> = graph.SourceNode<T>
local create_node = graph.create_node
local evaluate_node = graph.evaluate_node
local push_child_to_scope = graph.push_child_to_scope
local push_scope_as_child_of = graph.push_scope_as_child_of
local destroy = graph.destroy
local assert_stable_scope = graph.assert_stable_scope
local push_scope = graph.push_scope
@ -53,7 +53,7 @@ local function switch<T, U>(source: () -> T): (map: Map<T, ((() -> U)?)>) -> ()
evaluate_node(node)
return function()
push_child_to_scope(node)
push_scope_as_child_of(node)
return node.cache
end
end

27
src/timeout.luau Normal file
View file

@ -0,0 +1,27 @@
local queue = {} :: {
{ t: number, fn: () -> (), cancel: boolean }
}
local function timeout(t: number, fn: () -> ())
local handle = { t = t, fn = fn, cancel = false }
table.insert(queue, handle)
return handle
end
local function update_timeouts(dt: number)
for i = #queue, 1, -1 do
local handle = queue[i]
handle.t -= dt
if handle.cancel or handle.t <= 0 then
queue[i] = queue[#queue]
queue[#queue] = nil
if not handle.cancel then
handle.fn()
end
end
end
end
return function() return timeout, update_timeouts end

143
src/values.luau Normal file
View file

@ -0,0 +1,143 @@
local flags = require "./flags"
local graph = require "./graph"
type Node<T> = graph.Node<T>
type SourceNode<T> = graph.SourceNode<T>
local create_node = graph.create_node
local create_source_node = graph.create_source_node
local push_scope_as_child_of = graph.push_scope_as_child_of
local update_descendants = graph.update_descendants
local assert_stable_scope = graph.assert_stable_scope
local push_scope = graph.push_scope
local pop_scope = graph.pop_scope
local evaluate_node = graph.evaluate_node
local destroy = graph.destroy
type Map<K, V> = { [K]: V }
local function check_primitives(t: {})
if not flags.strict then return end
for _, v in t do
if type(v) == "table" or type(v) == "userdata" or type(v) == "function" then continue end
error("table source map cannot return primitives", 0)
end
end
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO, delay: number?): () -> { VO }
local owner = assert_stable_scope()
local subowner = create_node(owner, false, false)
local update_count = 0
local caches = {} :: Map<VI, {
count: number,
index: K,
scope: Node<unknown>,
index_source: SourceNode<K>,
alive_source: SourceNode<boolean>,
result: VO,
}>
local function update_children(data: Map<K, VI>)
local count = update_count
update_count += 1
local children_need_update = false
if flags.strict then
local cache = {}
for _, v in data do
if cache[v] ~= nil then
error "duplicate table value detected"
end
cache[v] = true
end
end
push_scope(subowner)
-- process data
for i, v in data do
local cache = caches[v]
if cache == nil then -- create new scope and run transform
local scope = create_node(subowner, false, false)
local index_source = create_source_node(i)
local new_cache = {
count = count,
index = i,
scope = scope :: Node<unknown>,
index_source = index_source :: SourceNode<K>,
alive_source = nil :: any,
result = false :: any,
}
-- must be set before transform is run so that the scope can
-- be destroyed if the transform itself updates the input
-- which lets the strict active scope destruction check work
caches[v] = new_cache
push_scope(scope)
local ok, result = xpcall(transform, debug.traceback, v, function()
push_scope_as_child_of(index_source)
return index_source.cache
end)
pop_scope()
if not ok then
pop_scope() -- subowner scope
error(result, 0)
end
new_cache.result = result
children_need_update = true
else -- update source
cache.count = count
if cache.index ~= i then
cache.index = i
cache.index_source.cache = i
update_descendants(cache.index_source)
end
end
end
pop_scope()
-- remove old values
for v, cache in caches do
if cache.count < count then
destroy(cache.scope)
caches[v] = nil
children_need_update = true
end
end
if children_need_update then
local output_array = table.create(#data)
for _, cache in caches do
table.insert(output_array, cache.result)
end
check_primitives(output_array)
return output_array
else
return nil
end
end
local node = create_node(owner, function(pre_children) -- todo: add test
return update_children(input()) or pre_children
end, {})
evaluate_node(node)
return function()
push_scope_as_child_of(node)
return node.cache
end
end
return values