Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make derived cells lazy #3

Open
fewkz opened this issue Apr 29, 2023 · 3 comments
Open

Make derived cells lazy #3

fewkz opened this issue Apr 29, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@fewkz
Copy link
Owner

fewkz commented Apr 29, 2023

right now, deriving a formula from a cell will result in that calculation being made whenever the cell changes, even if nothing is subscribed to that formula. We should make it so formulas are lightweight and lazy, which is the behavior in the signals library cells is based off of: https://github.com/preactjs/signals

@fewkz fewkz added the enhancement New feature or request label Sep 1, 2023
@fewkz
Copy link
Owner Author

fewkz commented Nov 19, 2024

So for example, for the follow code:

local c = cell(0)

local f1 = formula(function()
    print(`f1 updated`)
    return `f1: {c.value}`
end)

local f2 = formula(function()
    print(`f2 updated`)
    return `f2: {c.value}`
end)

local isone = formula(function()
    return true
end)

subscribe(function()
    if isone.value then
        print(f1.value)
    else
        print(f2.value)
    end
end)

We want the following behavior:

-- prints f1 updated
-- prints f1: 0
c.value = 10
-- prints f1 updated
-- prints f1: 10
c.value = 20
-- prints f1 updated
-- prints f1: 20
isone.value = false
-- prints f2 updated
-- prints f2: 20

However, currently the behavior is as follows

-- prints f1 updated
-- prints f2 updated
-- prints f1: 0
c.value = 10
-- prints f1 updated
-- prints f2 updated
-- prints f1: 10
c.value = 20
-- prints f1 updated
-- prints f2 updated
-- prints f1: 20
isone.value = false
-- prints f1 updated
-- prints f2 updated
-- prints f2: 20

Clearly, formulas are calculated despite not being needed. As well, this means that merely CREATING a formula has the overhead of performing its calculation. Having it be deferred to its initial subscription would be better.

@fewkz fewkz changed the title make cells lazy Make cells lazy Nov 19, 2024
@fewkz fewkz changed the title Make cells lazy Make derived cells lazy Nov 19, 2024
@fewkz
Copy link
Owner Author

fewkz commented Nov 19, 2024

This isn't necessarily a critical issue, as cells still works perfectly fine without it, and I don't particularly feel like working on adding this right now, but implementing this should be a simple addition that will just optimize any code using cells without requiring changes.

@fewkz
Copy link
Owner Author

fewkz commented Nov 19, 2024

You could also accomplish this behavior by just not using formula() and just creating a function that operates on the cell's value directly. This could technically be faster for some calculations since there's no overhead from formula. You could consider it like inlining the formula. However, I'm sure there's still cases where you would want to have the result of a calculation cached in the dependency graph.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant