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

Shorthand idea: false instead of () => false for equals #218

Open
NullVoxPopuli opened this issue May 7, 2024 · 5 comments
Open

Shorthand idea: false instead of () => false for equals #218

NullVoxPopuli opened this issue May 7, 2024 · 5 comments

Comments

@NullVoxPopuli
Copy link
Collaborator

in options for both Signal.State and Signal.Computed, an equals function is allowed to be passed to change how "dirtiness" is calculated -- it could be an optimization to allow false instead of requiring a function to then call every time -- we could skip part of the dirtying algo.

thoughts?

@ritschwumm
Copy link

might be premature optimization - i'd guess a modern JIT would inline a constant function returning false and eliminate what's left of it.

@NullVoxPopuli
Copy link
Collaborator Author

that is an interesting point about JIT -- I made a little test to see what happens (perf.link)

I only have these two browsers atm:

  • FireFox (Dev, 127.0a1)
    • just false is faster (which is what I would expect)
  • Chrome: (124.0.6367.118)
    • calling a function is faster than just having false, which is very surprising

Node and Bun are basically the same:
image

@zaygraveyard
Copy link

Modifying @NullVoxPopuli's test (perf.link) to include a loop results in the false version being faster in FireFox (Dev, 126.0b1), Google Chrome (124.0.6367.202), and Safari (17.4.1) on an M1 MacBook Air running macOS 14.4.1

PS: Not sure if my version is benchmarking the right thing though, benchmarking is hard 😣

@littledan
Copy link
Member

This is about more than the speed of calling the function: the signals system can have a more clear understanding of what's going on if it knows that it will always be false.

@dead-claudia
Copy link
Contributor

And to add on to that, if it's faster to always call, an engine will set its internal equals field to a private pre-optimized global function that always returns false.

Not that I even slightly find this likely, though. Not when it's only 3-4 instructions and 1 load total compared to well over a dozen plus a multiply indirect call.

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

No branches or pull requests

5 participants