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

Allow Computeds to eagerly recalculate to prevent dirty propagation #253

Open
DavidANeil opened this issue Jan 22, 2025 · 0 comments
Open

Comments

@DavidANeil
Copy link

In our application we frequently have reactive graphs that contain 10,000s of nodes connected among 100,000s of edges. This means that even propagating the dirty flag eagerly can noticeably hang the CPU thread. Our implementation of Signals has the ability to mark a node as preventDirtyPropagationViaRecalculation = true.

The implementation is fairly simple:

// in `notify` step
node.dirty = true;
if (node.preventDirtyPropagationViaRecalculation) {
    preventDirtyPropagationViaRecalculationQueue.push(node);
} else {
    markAllConsumersOfNodeAsDirty(node);
}

// After whatever began the `notify`
const nodes = preventDirtyPropagationViaRecalculationQueue.slice();
preventDirtyPropagationViaRecalculationQueue.length = 0;
for (const node of nodes) {
    const prevValueVersion = node.version; // This is a bit over-simplified, we actually need to grab this version when we store it in the queue, I think.
    producerUpdateValueVersion(node);
    if (node.version !== prevValueVersion) {
        markAllConsumersOfNodeAsDirty(node);
    }
}

When applied judiciously, this utility can dramatically improve the performance of a graph where their are certain nodes that are frequently invalidated and read, but only infrequently actually change. When applied inappropriately it is capable of turning full reactive graphs into a push-based system, which is usually very harmful to performance.

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

1 participant