Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .lute/styling.luau
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local process = require("@lute/process")

local cmd = { "stylua", ".lute", "src", "tests" }
local cmd = { "stylua", ".lute", "src", "tests", "bench" }

-- kinda cursed but, easiest way to do this.
table.insert(cmd, "--check")
Expand Down
2 changes: 0 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

"luau-lsp.completion.imports.stringRequires.enabled": true,

"luau-lsp.types.definitionFiles": ["types/globals.d.luau"],

"luau-lsp.sourcemap.autogenerate": false,
"luau-lsp.sourcemap.enabled": false,
"luau-lsp.sourcemap.includeNonScripts": true,
Expand Down
4 changes: 4 additions & 0 deletions ERRORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,7 @@ Self explanatory.
## stable_parent_assertion_failed

## invalid_parent_scope

## too_many_edges

A node read more than 65535 different nodes in one run, or more than 65535 nodes are reading it at once. Split the reads across derives, so no single node has to read or be read by that many.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This section details the interesting bits where Fluid has an advantage.
- Push-pull reactive graph with deferred mode
- `interval` utility to poll values. `fluid.interval` will run at the specified hz
- Exported `UsedAs` type
- `_G.__DEV__` for development mode, which will enhance error messages & print useful information at the cost of performance.
- `fluid.dev()` for development mode, which adds extra checks (e.g. erroring when a reactive function yields) at the cost of performance.

## Prior Art

Expand Down
177 changes: 177 additions & 0 deletions bench/bench.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
-- reactive graph benchmark, run with `lute run bench/bench.luau`
-- pass dev (`lute run bench/bench.luau dev`) to benchmark with dev mode enabled

local BENCH, START = require("@vendor/testkit").benchmark()
local fluid = require("@src/")
local process = require("@lute/process")

-- the first arg is the script's path
local DEV = process.args[2] == "dev"
if DEV then
fluid.dev()
end

local step = fluid.provide_scheduler()

-- roots are destroyed after each benchmark, so teardown isn't measured
local destroy: () -> () = function() end

print(`dev = {DEV}`)

BENCH("create effect", function()
destroy = fluid.root(function()
local src = fluid.source(0)

for _ = 1, START(10000) do
fluid.effect(function()
src()
end)
end
end)
end)
destroy()

BENCH("set source with 1000 eager dependents", function()
local src = fluid.source(0)

destroy = fluid.root(function()
for _ = 1, 1000 do
fluid.effect(function()
src()
end)
end
end)

for i = 1, START(200) do
src(i)
end
end)
destroy()

BENCH("set source of effect with 10 dependencies", function()
local sources = {}
for i = 1, 10 do
sources[i] = fluid.source(0)
end

destroy = fluid.root(function()
fluid.effect(function()
for i = 1, 10 do
sources[i]()
end
end)
end)

for i = 1, START(5000) do
sources[i % 10 + 1](i)
end
end)
destroy()

BENCH("set + flush deferred effect behind 5 derives", function()
local src = fluid.source(0)

destroy = fluid.root(function()
local previous: () -> number = src
for _ = 1, 5 do
local read = previous
previous = fluid.derive(function()
return read() + 1
end)
end

local last = previous
fluid.deferred(function()
last()
end)
end)

for i = 1, START(2000) do
src(i)
step(1 / 60)
end
end)
destroy()

BENCH("set + flush 50 derives that don't change", function()
local src = fluid.source(0)
local unrelated = fluid.source(0)

destroy = fluid.root(function()
local gate = fluid.derive(function()
return unrelated() > 1e9
end)

local consumers = {}
for i = 1, 50 do
consumers[i] = fluid.derive(function()
src()
gate()
return i
end)
end

fluid.deferred(function()
for i = 1, 50 do
consumers[i]()
end
end)
end)

for i = 1, START(2000) do
-- alternates between rerunning everything, and only rerunning the gate
if i % 2 == 0 then
src(i)
else
unrelated(i)
end
step(1 / 60)
end
end)
destroy()

BENCH("step 1000 springs", function()
local targets = {}

destroy = fluid.root(function()
for i = 1, 1000 do
local target = fluid.source(0)
targets[i] = target

local spring = fluid.spring(target, 10, 0.5)
fluid.effect(function()
spring()
end)
end
end)

for i = 1, 1000 do
targets[i](100)
end

for _ = 1, START(300) do
step(1 / 60)
end
end)
destroy()

BENCH("step 100 intervals", function()
destroy = fluid.root(function()
for _ = 1, 100 do
local elapsed = 0
local value = fluid.interval(function(dt)
elapsed += dt
return elapsed
end)

fluid.effect(function()
value()
end)
end
end)

for _ = 1, START(600) do
step(1 / 60)
end
end)
destroy()
1 change: 1 addition & 0 deletions roblox-tests.project.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"emitLegacyScripts": false,

"tree": {
"$ignoreUnknownInstances": true,
"$className": "DataModel",

"ReplicatedStorage": {
Expand Down
11 changes: 4 additions & 7 deletions src/anim/lerp.luau
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,21 @@ local oklab = require("../utils/oklab")
local read = require("../utils/read")
local types = require("../reactive/types")

local DEFERRED = graph.evaluation.DEFERRED
local active_animations = {}

local function vector_lerp(a: vector, b: vector, t: number): vector
return a + (b - a) * t
end

local function general_lerp(a: any, b: any, t: number): any
return a + (b - a) * t
end

local lerp_functions: { [string]: (a: any, b: any, t: number) -> any } = {
number = math.lerp,
Vector3 = vector_lerp,
Vector3 = vector.lerp,
Vector2 = general_lerp,
Color3 = function(a: Color3, b: Color3, t)
local initial_vector = oklab.from_srgb(vector.create(a.R, a.G, a.B))
local goal_vector = oklab.from_srgb(vector.create(b.R, b.G, b.B))
local interpolated_vector = vector_lerp(initial_vector, goal_vector, t)
local interpolated_vector = vector.lerp(initial_vector, goal_vector, t)
local result_vector = vector.clamp(oklab.to_srgb(interpolated_vector), vector.zero, vector.one)

return Color3.new(result_vector.x, result_vector.y, result_vector.z)
Expand Down Expand Up @@ -52,7 +49,7 @@ function lerp.create<T>(goal: types.UsedAs<T>, takes: types.UsedAs<number>)
return nil
end

graph.create_reactive_node(graph.assert_stable_parent(), effect, "deferred")
graph.create_reactive_node(graph.assert_stable_parent(), effect, DEFERRED)

return function()
graph.push_dependency(output)
Expand Down
3 changes: 2 additions & 1 deletion src/anim/spring/spring_file.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local oklab = require("../../utils/oklab")
local read = require("../../utils/read")
local types = require("../../reactive/types")

local DEFERRED = graph.evaluation.DEFERRED
local SLEEP_VELOCITY = 1e-3
local SLEEP_DISTANCE = 1e-5

Expand Down Expand Up @@ -177,7 +178,7 @@ function spring_file.create<T>(
end

-- set up the dependency
local input_node = graph.create_reactive_node(graph.assert_stable_parent(), on_update, "deferred", read(input))
local input_node = graph.create_reactive_node(graph.assert_stable_parent(), on_update, DEFERRED, read(input))

graph.evaluate_node(input_node)

Expand Down
7 changes: 7 additions & 0 deletions src/dev.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- dev mode adds extra checks (e.g. erroring when a reactive function yields) at the cost of performance.
-- enabled through fluid.dev()
local dev = {
enabled = false,
}

return dev
25 changes: 23 additions & 2 deletions src/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ local action = require("@self/instances/action")
local async = require("@self/reactive/async")
local bind = require("@self/instances/bind")
local changed = require("@self/instances/changed")
local cleanup = require("@self/reactive/cleanup")
local create = require("@self/instances/create")
local deferred = require("@self/reactive/deferred")
local derive = require("@self/reactive/derive")
local dev = require("@self/dev")
local effect = require("@self/reactive/effect")
local for_keys = require("@self/instances/for_keys")
local for_values = require("@self/instances/for_values")
Expand All @@ -31,8 +31,22 @@ export type Source<T> = types.Source<T>
export type Action = action.Identity
export type PropertiesFor<T> = create.PropertiesFor<T>

type Destroyable = { destroy: (any) -> () } | { Destroy: (any) -> () }
type Disconnectable = { disconnect: (any) -> () } | { Disconnect: (any) -> () }

local scheduler_thread: thread? = nil

-- values are stored as is, the graph works out how to clean them up when the scope reruns or dies
local function cleanup(value: graph.Cleanup)
local scope = graph.get_scope()

if not scope then
error("cannot cleanup outside a stable or reactive scope")
end

graph.push_cleanup(scope, value)
end

local function step(delta_time: number)
-- This will call intervals
scheduler.step(delta_time)
Expand Down Expand Up @@ -70,7 +84,8 @@ fluid.action = action.create
fluid.is_action = action.is_action

-- Reactive graph
fluid.cleanup = cleanup
fluid.cleanup =
cleanup :: ((callback: () -> ()) -> ()) & ((thread: thread) -> ()) & ((instance: Destroyable) -> ()) & ((connection: Disconnectable) -> ()) & ((instance: Instance) -> ()) & ((connection: RBXScriptConnection) -> ())
fluid.derive = derive
fluid.effect = effect
fluid.deferred = deferred
Expand Down Expand Up @@ -102,6 +117,12 @@ fluid.provide_scheduler = function(): (delta_time: number) -> ()
return step
end

-- toggles dev mode, which adds extra checks (e.g. erroring when a reactive function yields)
-- at the cost of performance. call this before mounting anything
fluid.dev = function()
dev.enabled = true
end

fluid.__SCHEDULER_INTERFACE = scheduler

return fluid
Loading
Loading