mirror of
https://github.com/littensy/charm.git
synced 2026-10-20 17:51:37 +00:00
feat: use alien-signals
This commit is contained in:
parent
d7a1f62c3c
commit
861ee5acd7
76 changed files with 3112 additions and 1862 deletions
|
|
@ -1,15 +1,25 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
|
||||
local Charm = require(ReplicatedStorage.DevPackages.Charm)
|
||||
|
||||
local computed = Charm.computed
|
||||
local effect = Charm.effect
|
||||
local atom = Charm.atom
|
||||
|
||||
local cleanup: () -> ()
|
||||
|
||||
return {
|
||||
ParameterGenerator = function() end,
|
||||
AfterEach = function()
|
||||
cleanup()
|
||||
end,
|
||||
Functions = {
|
||||
atom = function()
|
||||
local state = atom(0)
|
||||
|
||||
cleanup = effect(function()
|
||||
state()
|
||||
end)
|
||||
|
||||
for _ = 1, 500 do
|
||||
state(math.random(1, 2))
|
||||
end
|
||||
|
|
@ -18,10 +28,14 @@ return {
|
|||
computed = function()
|
||||
local state = atom(0)
|
||||
|
||||
local _double = computed(function()
|
||||
local double = computed(function()
|
||||
return 2 * state()
|
||||
end)
|
||||
|
||||
cleanup = effect(function()
|
||||
double()
|
||||
end)
|
||||
|
||||
for _ = 1, 500 do
|
||||
state(math.random(1, 2))
|
||||
end
|
||||
38
tests/benchmarks/propagate.bench.luau
Normal file
38
tests/benchmarks/propagate.bench.luau
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
local ReplicatedStorage = game:GetService("ReplicatedStorage")
|
||||
|
||||
local Charm = require(ReplicatedStorage.DevPackages.Charm)
|
||||
local signals = require(ReplicatedStorage.DevPackages.Charm.signals)
|
||||
|
||||
local function propagate(
|
||||
signal: <T>(T) -> (T?) -> T,
|
||||
computed: <T>(() -> T) -> () -> T,
|
||||
effect: (() -> ()) -> (),
|
||||
w: number,
|
||||
h: number
|
||||
)
|
||||
local src = signal(1)
|
||||
for _ = 1, w do
|
||||
local last = src
|
||||
for _ = 1, h do
|
||||
local prev = last
|
||||
last = computed(function()
|
||||
return prev() + 1
|
||||
end)
|
||||
end
|
||||
effect(function()
|
||||
last()
|
||||
end)
|
||||
end
|
||||
src(src() + 1)
|
||||
end
|
||||
|
||||
return {
|
||||
Functions = {
|
||||
charm = function()
|
||||
propagate(Charm.atom, Charm.computed, Charm.effect, 10, 10)
|
||||
end,
|
||||
alien_signals = function()
|
||||
propagate(signals.signal, signals.computed, signals.effect, 10, 10)
|
||||
end,
|
||||
},
|
||||
}
|
||||
61
tests/benchmarks/propagate.luau
Normal file
61
tests/benchmarks/propagate.luau
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
local Charm = require("../../packages/charm/src/lib")
|
||||
|
||||
local atom = Charm.atom
|
||||
local computed = Charm.computed
|
||||
local effect = Charm.effect
|
||||
|
||||
local signals = require("../../packages/charm/src/signals")
|
||||
|
||||
local signal = signals.signal
|
||||
local computedSignal = signals.computed
|
||||
local effectSignal = signals.effect
|
||||
|
||||
local w = 10
|
||||
local h = 10
|
||||
|
||||
local function bench(name: string, test: () -> ())
|
||||
local start = os.clock()
|
||||
local iter = 0
|
||||
for _ = 1, 15000 do
|
||||
iter += 1
|
||||
test()
|
||||
end
|
||||
local endTime = os.clock()
|
||||
local elapsed = endTime - start
|
||||
print(`{name}: {elapsed / iter * 1000 * 1000} us/iter`)
|
||||
end
|
||||
|
||||
bench(`charm {w} * {h}`, function()
|
||||
local src = atom(1)
|
||||
for _ = 1, w do
|
||||
local last = src
|
||||
for _ = 1, h do
|
||||
local prev = last
|
||||
last = computed(function()
|
||||
return prev() + 1
|
||||
end)
|
||||
end
|
||||
effect(function()
|
||||
last()
|
||||
end)
|
||||
end
|
||||
src(src() + 1)
|
||||
end)
|
||||
|
||||
bench(`alien-signals {w} * {h}`, function()
|
||||
local src = signal(1)
|
||||
for _ = 1, w do
|
||||
local last = src
|
||||
for _ = 1, h do
|
||||
local prev = last
|
||||
last = computedSignal(function()
|
||||
return prev() + 1
|
||||
end)
|
||||
end
|
||||
effectSignal(function()
|
||||
last()
|
||||
return nil
|
||||
end)
|
||||
end
|
||||
src(src() + 1)
|
||||
end)
|
||||
Loading…
Add table
Add a link
Reference in a new issue