mirror of
https://github.com/littensy/charm.git
synced 2026-10-22 12:41:37 +00:00
refactor: improve logic
This commit is contained in:
parent
210e948225
commit
72f936591e
48 changed files with 434 additions and 349 deletions
48
benches/bench.luau
Normal file
48
benches/bench.luau
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
local profiles: { [string]: number } = {}
|
||||
local times: { [string]: number } = {}
|
||||
local stack: { string } = {}
|
||||
|
||||
local function run(name: string, test: () -> ())
|
||||
local start = os.clock()
|
||||
local iter = 0
|
||||
|
||||
table.clear(profiles)
|
||||
table.clear(times)
|
||||
table.clear(stack)
|
||||
|
||||
while os.clock() - start < 2 do
|
||||
iter += 1
|
||||
test()
|
||||
end
|
||||
|
||||
local elapsed = os.clock() - start
|
||||
local results = {}
|
||||
|
||||
table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
|
||||
|
||||
for name, elapsed in times do
|
||||
table.insert(results, `{name}: \t{string.format("%.2f", elapsed / iter * 1000 * 1000)} us/iter`)
|
||||
end
|
||||
|
||||
print(table.concat(results, " | "))
|
||||
end
|
||||
|
||||
local function profilebegin(name: string)
|
||||
profiles[name] = os.clock()
|
||||
table.insert(stack, name)
|
||||
end
|
||||
|
||||
local function profileend()
|
||||
local name = table.remove(stack)
|
||||
|
||||
if name then
|
||||
times[name] = (times[name] or 0) + os.clock() - profiles[name]
|
||||
profiles[name] = nil
|
||||
end
|
||||
end
|
||||
|
||||
return {
|
||||
run = run,
|
||||
profilebegin = profilebegin,
|
||||
profileend = profileend,
|
||||
}
|
||||
79
benches/mapped.luau
Normal file
79
benches/mapped.luau
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
local Charm = require("@pkgs/charm/src/lib")
|
||||
local bench = require("./bench")
|
||||
|
||||
local atom = Charm.atom
|
||||
local computed = Charm.computed
|
||||
local mapped = Charm.mapped
|
||||
local setCurrentSub = Charm.signals.setCurrentSub
|
||||
|
||||
type Entry = { number }
|
||||
|
||||
local count = 512
|
||||
local initialState: { Entry } = table.create(count)
|
||||
|
||||
for i = 1, count do
|
||||
initialState[i] = table.create(64, i)
|
||||
end
|
||||
|
||||
local function mapper(value: Entry, key: number): (Entry, number)
|
||||
return table.clone(value), key + 1
|
||||
end
|
||||
|
||||
local function simpleMapped(source: () -> { Entry }, mapper: (Entry, number) -> (Entry, number)): () -> { Entry }
|
||||
return computed(function()
|
||||
local state = source()
|
||||
local nextState = {}
|
||||
|
||||
for index, value in state do
|
||||
local currentSub = setCurrentSub(nil)
|
||||
local success, valueOut, keyOut = pcall(mapper, value, index)
|
||||
setCurrentSub(currentSub)
|
||||
|
||||
if success then
|
||||
nextState[keyOut] = valueOut
|
||||
end
|
||||
end
|
||||
|
||||
return state
|
||||
end)
|
||||
end
|
||||
|
||||
local function updatePercentage(percent: number): ({ Entry }) -> { Entry }
|
||||
return function(state)
|
||||
local nextState = table.clone(state)
|
||||
for index, value in state do
|
||||
if math.random() < percent then
|
||||
nextState[index] = table.clone(value)
|
||||
end
|
||||
end
|
||||
return nextState
|
||||
end
|
||||
end
|
||||
|
||||
for _, percent in { 0.1, 0.5, 1 } do
|
||||
bench.run(`mapped {percent}`, function()
|
||||
local src = atom(initialState)
|
||||
local out = mapped(src, mapper)
|
||||
|
||||
bench.profilebegin("init")
|
||||
out()
|
||||
bench.profileend()
|
||||
src(updatePercentage(percent))
|
||||
bench.profilebegin("update")
|
||||
out()
|
||||
bench.profileend()
|
||||
end)
|
||||
|
||||
bench.run(`control {percent}`, function()
|
||||
local src = atom(initialState)
|
||||
local out = simpleMapped(src, mapper)
|
||||
|
||||
bench.profilebegin("init")
|
||||
out()
|
||||
bench.profileend()
|
||||
src(updatePercentage(percent))
|
||||
bench.profilebegin("update")
|
||||
out()
|
||||
bench.profileend()
|
||||
end)
|
||||
end
|
||||
54
benches/patch.bench.luau
Normal file
54
benches/patch.bench.luau
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
|
||||
local CharmSync = require(ReplicatedStorage.Packages.CharmSync)
|
||||
|
||||
local patch = CharmSync.patch
|
||||
|
||||
local USERS = 1000
|
||||
|
||||
local function generate()
|
||||
local users = {}
|
||||
|
||||
for user = 1, USERS do
|
||||
users[user] = {
|
||||
coins = math.random(1, 2),
|
||||
gems = math.random(1, 2),
|
||||
}
|
||||
end
|
||||
|
||||
return { users = users }
|
||||
end
|
||||
|
||||
local function update(state)
|
||||
state = table.clone(state)
|
||||
state.users = table.clone(state.users)
|
||||
|
||||
for user = 1, USERS do
|
||||
local rng = math.random()
|
||||
|
||||
if rng < 1 / 3 then
|
||||
continue
|
||||
elseif rng < 2 / 3 then
|
||||
state.users[user] = nil
|
||||
else
|
||||
state.users[user] = {
|
||||
coins = math.random(1, 2),
|
||||
gems = math.random(1, 2),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return state
|
||||
end
|
||||
|
||||
return {
|
||||
ParameterGenerator = function()
|
||||
local state = generate()
|
||||
return state, update(state)
|
||||
end,
|
||||
Functions = {
|
||||
diff = function(_, a, b)
|
||||
patch.diff(a, b)
|
||||
end,
|
||||
},
|
||||
}
|
||||
42
benches/propagate.luau
Normal file
42
benches/propagate.luau
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
local Charm = require("@pkgs/charm/src/lib")
|
||||
local bench = require("./bench")
|
||||
|
||||
local signals = Charm.signals
|
||||
|
||||
for _, w in { 10, 100 } do
|
||||
for _, h in { 10, 100 } do
|
||||
bench.run(`charm {w}*{h}`, function()
|
||||
local src = Charm.atom(1)
|
||||
for _ = 1, w do
|
||||
local last = src
|
||||
for _ = 1, h do
|
||||
local prev = last
|
||||
last = Charm.computed(function()
|
||||
return prev() + 1
|
||||
end)
|
||||
end
|
||||
Charm.effect(function()
|
||||
last()
|
||||
end)
|
||||
end
|
||||
src(src() + 1)
|
||||
end)
|
||||
|
||||
bench.run(`alien-signals {w}*{h}`, function()
|
||||
local src, setSrc = signals.signal(1)
|
||||
for _ = 1, w do
|
||||
local last = src
|
||||
for _ = 1, h do
|
||||
local prev = last
|
||||
last = signals.computed(function()
|
||||
return prev() + 1
|
||||
end)
|
||||
end
|
||||
signals.effect(function()
|
||||
last()
|
||||
end)
|
||||
end
|
||||
setSrc(src() + 1)
|
||||
end)
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue