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

refactor(core): Use iterative approach in recompute dependents #2821

Merged
merged 1 commit into from
Nov 17, 2024

Conversation

dmaskasky
Copy link
Collaborator

@dmaskasky dmaskasky commented Nov 15, 2024

Summary

Replaces the recursive topo search in recomputeDependents with an iterative one. An iterative approach theoretically is faster and does not suffer from stack overflow issues. This makes it possible to process very large atom graphs.

Benchmark

  • Recursive: ~200ms
  • Iterative: ~ 250ms
    TODO: Currently the new iterative approach is slower than the recursive one.

Check List

  • pnpm run prettier for formatting code and docs

@dmaskasky dmaskasky marked this pull request as draft November 15, 2024 01:01
@dmaskasky dmaskasky changed the title use iterative approach in recompute dependents [WIP] use iterative approach in recompute dependents Nov 15, 2024
Copy link

vercel bot commented Nov 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
jotai ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 17, 2024 11:00am

Copy link

codesandbox-ci bot commented Nov 15, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

github-actions bot commented Nov 15, 2024

LiveCodes Preview in LiveCodes

Latest commit: d570004
Last updated: Nov 17, 2024 10:59am (UTC)

Playground Link
React demo https://livecodes.io?x=id/8ZPZWPCBW

See documentations for usage instructions.

Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a great improvement.

src/vanilla/store.ts Outdated Show resolved Hide resolved
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from baa1bc5 to 349c1fc Compare November 15, 2024 01:45
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 349c1fc to 4878f64 Compare November 15, 2024 23:21
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 4878f64 to 1a899fe Compare November 17, 2024 03:02
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 1a899fe to 862d7cc Compare November 17, 2024 08:13
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 862d7cc to 423e5b7 Compare November 17, 2024 08:19
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 423e5b7 to 409949a Compare November 17, 2024 08:35
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from 409949a to d71f64d Compare November 17, 2024 08:36
@dmaskasky dmaskasky force-pushed the recompute-dependents-iterative branch from d71f64d to ca597ed Compare November 17, 2024 08:37
@dmaskasky dmaskasky changed the title [WIP] use iterative approach in recompute dependents Use iterative approach in recompute dependents Nov 17, 2024
src/vanilla/store.ts Outdated Show resolved Hide resolved
Copy link
Member

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

tests/vanilla/store.test.tsx Show resolved Hide resolved
@dai-shi dai-shi changed the title Use iterative approach in recompute dependents refactor(core): Use iterative approach in recompute dependents Nov 17, 2024
@dai-shi dai-shi added this to the v2.10.3 milestone Nov 17, 2024
@dai-shi dai-shi merged commit 474e369 into pmndrs:main Nov 17, 2024
40 checks passed
@cltnschlosser
Copy link

Can you share the benchmark that you used to get the times in the PR description? This slowdown isn’t going to work for us, but I think I see how to optimize the iterative case, I want to prove it with the same benchmark though.

@dmaskasky
Copy link
Collaborator Author

dmaskasky commented Nov 21, 2024

Oh, would you be able to provide these benchmarks? I only used vitest test duration using stack limit size nodes (~12k) in a chained dependency configuration.

I'd love to hear your ideas on how to make this faster. The original recursive solution suffers from stack overflow issues, so I'm keen on keeping an iterative approach. I tried both DFS and BFS and found they both have the same number of iterations but the DFS was more memory efficient.

@dmaskasky
Copy link
Collaborator Author

Oh, I found a typo in the PR description. The iterative DFS solution in around 250 ms in my testing, not 350 ms.

@cltnschlosser
Copy link

We should be able to shuffle some things around to remove one of the visiting or visited sets.

@dmaskasky
Copy link
Collaborator Author

I'm not sure how to improve on the post-order DFS topological sort solution we have currently. I tried implementing Kahn's, but there's too much administrative work required to beat the current solution.

#2833

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

Successfully merging this pull request may close these issues.

3 participants