mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
This commit is contained in:
parent
fa2709f182
commit
6f9e86c5c7
3 changed files with 73 additions and 20 deletions
|
|
@ -87,6 +87,7 @@ local function bind_parent(instance: Instance, fn: () -> Instance?)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- todo: could optimize, see: maps.luau values()
|
||||||
local function bind_children(parent: Instance, fn: () -> { Instance })
|
local function bind_children(parent: Instance, fn: () -> { Instance })
|
||||||
local current_child_set: { [Instance]: true } = {} -- cache of all children parented before update
|
local current_child_set: { [Instance]: true } = {} -- cache of all children parented before update
|
||||||
local new_child_set: { [Instance]: true } = {} -- cache of all children parented after update
|
local new_child_set: { [Instance]: true } = {} -- cache of all children parented after update
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ local link = graph.link
|
||||||
|
|
||||||
type Map<K, V> = { [K]: V }
|
type Map<K, V> = { [K]: V }
|
||||||
|
|
||||||
-- todo: this could be optimized
|
-- todo: optimize output array
|
||||||
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI, K) -> VO): () -> { VO }
|
||||||
local input_cache = {} :: Map<K, VI>
|
local input_cache = {} :: Map<K, VI>
|
||||||
local output_cache = {} :: Map<K, VO>
|
local output_cache = {} :: Map<K, VO>
|
||||||
|
|
@ -32,9 +32,12 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
||||||
input_nodes[k] = nil
|
input_nodes[k] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
table.clear(remove_queue)
|
||||||
|
|
||||||
-- process new or changed values
|
-- process new or changed values
|
||||||
for k, v in next, data do
|
for k, v in next, data do
|
||||||
local cv = input_cache[k]
|
local cv = input_cache[k]
|
||||||
|
|
||||||
if cv == nil then
|
if cv == nil then
|
||||||
local node, get_value = create(v)
|
local node, get_value = create(v)
|
||||||
input_nodes[k] = node
|
input_nodes[k] = node
|
||||||
|
|
@ -72,45 +75,45 @@ local function indexes<K, VI, VO>(input: () -> Map<K, VI>, transform: (() -> VI,
|
||||||
return output_get
|
return output_get
|
||||||
end
|
end
|
||||||
|
|
||||||
-- todo: this should be optimized
|
-- todo: optimize output array
|
||||||
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
local function values<K, VI, VO>(input: () -> Map<K, VI>, transform: (VI, () -> K) -> VO): () -> { VO }
|
||||||
local input_cache_up = {} :: Map<VI, K>
|
local cur_input_cache_up = {} :: Map<VI, K>
|
||||||
local input_cache_buffer_up = {} :: Map<VI, K>
|
local new_input_cache_up = {} :: Map<VI, K>
|
||||||
|
|
||||||
local output_cache = {} :: Map<VI, VO>
|
local output_cache = {} :: Map<VI, VO>
|
||||||
local input_nodes = {} :: Map<VI, Node<K>>
|
local input_nodes = {} :: Map<VI, Node<K>>
|
||||||
local output_array = {} :: { VO }
|
local output_array = {} :: { VO }
|
||||||
|
|
||||||
local function recompute(data: Map<K, VI>)
|
local function recompute(data: Map<K, VI>)
|
||||||
local input_cache, input_cache_buffer =
|
local cur_input_cache, new_input_cache = cur_input_cache_up, new_input_cache_up
|
||||||
input_cache_up, input_cache_buffer_up
|
|
||||||
|
|
||||||
-- process new or changed values
|
-- process data
|
||||||
for i, v in next, data do
|
for i, v in next, data do
|
||||||
local cv = input_cache[v]
|
new_input_cache[v] = i
|
||||||
|
|
||||||
|
local cv = cur_input_cache[v]
|
||||||
|
|
||||||
if cv == nil then
|
if cv == nil then
|
||||||
local node, get_value = create(i)
|
local node, get_value = create(i)
|
||||||
input_nodes[v] = node
|
input_nodes[v] = node
|
||||||
output_cache[v] = transform(v, get_value)
|
output_cache[v] = transform(v, get_value)
|
||||||
elseif cv ~= i then
|
else
|
||||||
set(input_nodes[v], i)
|
if cv ~= i then
|
||||||
|
set(input_nodes[v], i)
|
||||||
|
end
|
||||||
|
cur_input_cache[v] = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
input_cache_buffer[v] = i
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- remove old values
|
-- remove old values
|
||||||
for v, k in next, input_cache do
|
for v in next, cur_input_cache do
|
||||||
if input_cache_buffer[v] == nil then
|
output_cache[v] = nil
|
||||||
output_cache[v] = nil
|
input_nodes[v] = nil
|
||||||
input_nodes[v] = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- update buffer cache
|
-- update buffer cache
|
||||||
table.clear(input_cache)
|
table.clear(cur_input_cache)
|
||||||
input_cache_up, input_cache_buffer_up = input_cache_buffer, input_cache
|
cur_input_cache_up, new_input_cache_up = new_input_cache, cur_input_cache
|
||||||
|
|
||||||
-- output elements
|
-- output elements
|
||||||
table.clear(output_array)
|
table.clear(output_array)
|
||||||
|
|
|
||||||
|
|
@ -135,11 +135,14 @@ BENCH("indexes() no change", function()
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
state(state()) -- fill double buffer
|
||||||
|
|
||||||
START(N)
|
START(N)
|
||||||
|
|
||||||
state(data)
|
state(data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
-- todo: find out what is causing allocation
|
||||||
BENCH("indexes() all change", function()
|
BENCH("indexes() all change", function()
|
||||||
local data = {}
|
local data = {}
|
||||||
|
|
||||||
|
|
@ -153,6 +156,8 @@ BENCH("indexes() all change", function()
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
state(state()) -- fill double buffer
|
||||||
|
|
||||||
for i, v in data do
|
for i, v in data do
|
||||||
data[i] = v + 1
|
data[i] = v + 1
|
||||||
end
|
end
|
||||||
|
|
@ -162,6 +167,26 @@ BENCH("indexes() all change", function()
|
||||||
state(data)
|
state(data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
BENCH("indexes() all remove", function()
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
for i = 1, N do
|
||||||
|
data[i] = i
|
||||||
|
end
|
||||||
|
|
||||||
|
local state = vide.source(data)
|
||||||
|
|
||||||
|
local _list = vide.values(state, function(v, i)
|
||||||
|
return {}
|
||||||
|
end)
|
||||||
|
|
||||||
|
table.clear(data)
|
||||||
|
|
||||||
|
START(N)
|
||||||
|
|
||||||
|
state(data)
|
||||||
|
end)
|
||||||
|
|
||||||
BENCH("values() no change", function()
|
BENCH("values() no change", function()
|
||||||
local data = {}
|
local data = {}
|
||||||
|
|
||||||
|
|
@ -175,6 +200,8 @@ BENCH("values() no change", function()
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
state(state()) -- fill double buffer
|
||||||
|
|
||||||
START(N)
|
START(N)
|
||||||
|
|
||||||
state(data)
|
state(data)
|
||||||
|
|
@ -193,6 +220,8 @@ BENCH("values() all change", function()
|
||||||
return {}
|
return {}
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
state(state()) -- fill double buffer
|
||||||
|
|
||||||
for i = 1, N do
|
for i = 1, N do
|
||||||
local r = math.random(1, #data)
|
local r = math.random(1, #data)
|
||||||
data[i], data[r] = data[r], data[i]
|
data[i], data[r] = data[r], data[i]
|
||||||
|
|
@ -203,4 +232,24 @@ BENCH("values() all change", function()
|
||||||
state(data)
|
state(data)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
BENCH("values() all remove", function()
|
||||||
|
local data = {}
|
||||||
|
|
||||||
|
for i = 1, N do
|
||||||
|
data[i] = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
local state = vide.source(data)
|
||||||
|
|
||||||
|
local _list = vide.values(state, function(v, i)
|
||||||
|
return {}
|
||||||
|
end)
|
||||||
|
|
||||||
|
table.clear(data)
|
||||||
|
|
||||||
|
START(N)
|
||||||
|
|
||||||
|
state(data)
|
||||||
|
end)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue