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

Fix and optimize ReactiveMap and ReactiveWeakMap #704

Merged
merged 12 commits into from
Jan 2, 2025

Conversation

Exelord
Copy link
Contributor

@Exelord Exelord commented Oct 4, 2024

This PR is improving and fixing ReactiveMap and ReactiveWeakMap, following up on #593

All noticeable changes have been commented. @thetarnav please let me know in case you have more questions :)

Copy link

changeset-bot bot commented Oct 4, 2024

🦋 Changeset detected

Latest commit: bf5a737

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@solid-primitives/map Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@@ -1,7 +1,7 @@
import { Accessor, batch } from "solid-js";
import { TriggerCache } from "@solid-primitives/trigger";

const $KEYS = Symbol("track-keys");
const $OBJECT = Symbol("track-object");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing to object as now we need to track object keys and values in both caches

#keyTriggers = new TriggerCache<K | typeof $KEYS>();
#valueTriggers = new TriggerCache<K>();
#keyTriggers = new TriggerCache<K | typeof $OBJECT>();
#valueTriggers = new TriggerCache<K | typeof $OBJECT>();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Values will have an object trigger as well to support and values, entires and forEach

return super.size;
}

*keys(): MapIterator<K> {
this.#keyTriggers.track($OBJECT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now, keys invalidate whenever a key has been added, changed or removed. No matter if a key was used or not.

This is Removing the same keys optimization as in Set, as we cannot assume a break will happen consistently on each computation.

for (const [key, v] of super.entries()) {
this.#valueTriggers.track(key);
yield v;
this.#valueTriggers.track($OBJECT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Values are no longer tracking changes in keys changes. Instead they track only values changes.

This is also Removing the same keys optimization as in Set, as we cannot assume a break will happen consistently on each computation.

*entries(): MapIterator<[K, V]> {
this.#keyTriggers.track($OBJECT);
this.#valueTriggers.track($OBJECT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Entries are now tracking global changes to keys and values, ensuring they will be dirtied whenever anything has changed.

This is also Removing the same keys optimization as in Set, as we cannot assume a break will happen consistently on each computation.

this.#keyTriggers.dirty(key);
}
if (hasChanged) {
this.#valueTriggers.dirty($OBJECT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Global values are dirtied for values, entries and forEach support

this.#keyTriggers.dirty($KEYS);
this.#valueTriggers.dirty(key);

if (isDefined) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Value trigger is only dirtied if the value actually has changed.

batch(() => {
this.#keyTriggers.dirty($OBJECT);
this.#valueTriggers.dirty($OBJECT);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is important: we do dirty global values trigger always, as values() may depend on order. Even though the actual value of the key may not change, removing it, will have impact on amount of iterations in *values()

}
super.clear();
this.#keyTriggers.dirty($KEYS);
this.#keyTriggers.dirtyAll();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clear is now optimized using new dirtyAll()

clear(): void {
if (super.size) {
super.clear();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

clear is now fixed, calling .clear() at before dirtying

@Exelord
Copy link
Contributor Author

Exelord commented Nov 5, 2024

@thetarnav any inputs?

@thetarnav thetarnav merged commit 37f0616 into solidjs-community:main Jan 2, 2025
3 checks passed
@github-actions github-actions bot mentioned this pull request Jan 2, 2025
@Exelord Exelord deleted the fix-map branch January 3, 2025 16:38
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.

2 participants