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

[IMP] reactivity: replace sets with small arrays for performance #1581

Merged
merged 1 commit into from
Jan 12, 2024

Conversation

sdegueldre
Copy link
Contributor

While Sets have better lookup complexity than arrays, because of the large constant factors, small arrays can perform better than small sets when checking for inclusion.

In practice, replacing both of the raw types sets with arrays can improve performance of reactive-heavy workloads by as much as 30%.

Considering the reactivity code is very hot when rendering data-heavy components, and the low impact on readability of the fix, the cost-benefit analysis is clearly in favour of making the fix.

While Sets have better lookup complexity than arrays, because of the
large constant factors, small arrays can perform better than small sets
when checking for inclusion.

In practice, replacing both of the raw types sets with arrays can
improve performance of reactive-heavy workloads by as much as 30%.

Considering the reactivity code is very hot when rendering data-heavy
components, and the low impact on readability of the fix, the
cost-benefit analysis is clearly in favour of making the fix.
@sdegueldre sdegueldre force-pushed the master-reactivity-perf-imp-sad branch from 7852758 to 489f1a3 Compare January 12, 2024 14:31
@aab-odoo aab-odoo merged commit 7b3e39b into master Jan 12, 2024
3 checks passed
@aab-odoo aab-odoo deleted the master-reactivity-perf-imp-sad branch January 12, 2024 14:40
@@ -45,7 +46,7 @@ function canBeMadeReactive(value: any): boolean {
if (typeof value !== "object") {
return false;
}
return SUPPORTED_RAW_TYPES.has(rawType(value));
return SUPPORTED_RAW_TYPES.includes(rawType(value));
Copy link
Contributor

Choose a reason for hiding this comment

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

at this point, maybe it's even faster to simply check for equality:

const type = rawType(value);
return type === "Object" || type === "Array" || type === "Set" || type === "Map" || type === "WeakMap";

Same for collections. what do you think? @sdegueldre

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried with an inline switch previously when experimenting but perf was about the same, I'm guessing this is already very easy to JIT efficiently but may require more exploration.

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