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

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

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

⚠️ No Changeset found

Latest commit: 26fccc1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a 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?

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.

1 participant