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

Faster get function for views #90

Closed
wants to merge 1 commit into from
Closed

Faster get function for views #90

wants to merge 1 commit into from

Conversation

airhorns
Copy link
Contributor

@airhorns airhorns commented May 30, 2024

This implements a better performing getter function for readonly instances. Before this, we used the same definition of the getter function for all views, which ended up megamorphic, because it accessed a very wide variety of properties for every instance! Instead, this evals a monomorphic getter for each one. I also changed the object that stores the memos to have a fixed shape from birth by evaling it out as well.

I also changed us to use one object to store all the memoized values, instead of two. We were previously using two in order to implement memoization of undefined and null correctly, but with the new strategy to initialize an object with slots for every memo from the start, we can populate it with a $notYetMemoized symbol that indicates if we have memoized or not.

@airhorns airhorns changed the base branch from main to tinybench May 30, 2024 23:50
@airhorns airhorns changed the title faster get 3 Faster get function for views May 30, 2024
@airhorns airhorns force-pushed the faster-get-3 branch 3 times, most recently from 01dda85 to e1d74e8 Compare May 31, 2024 00:24
Copy link

codspeed-hq bot commented May 31, 2024

CodSpeed Performance Report

Merging #90 will not alter performance

Comparing faster-get-3 (16c821f) with main (f183291)

Summary

✅ 8 untouched benchmarks

@airhorns airhorns requested a review from thegedge May 31, 2024 01:09
Copy link
Contributor

@thegedge thegedge left a comment

Choose a reason for hiding this comment

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

eval to the (mono)rescue!

Comment on lines +41 to +51
let value = this[$memos].${property};
if (value != $notYetMemoized) {
return value;
}

value = getValue.call(this);
this[$memos].${property} = value;
return value;
Copy link
Contributor

@thegedge thegedge May 31, 2024

Choose a reason for hiding this comment

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

Curious if you tried overriding the descriptor with the memoized value, and if so, was that an improvement?

Object.defineProperty(this, "${property}", { value, writable: false })
return value;

Would eliminate the need for this[$memos]

Base automatically changed from tinybench to main May 31, 2024 13:11
@airhorns airhorns force-pushed the faster-get-3 branch 3 times, most recently from f94b020 to b928fdb Compare May 31, 2024 13:44
…ched views

This implements a better performing getter function for readonly instances. Before this, we used the same definition of the getter function for all views, which ended up megamorphic, because it accessed a very wide variety of properties for every instance! Instead, this evals a monomorphic getter for each one. I also changed the object that stores the memos to have a fixed shape from birth by evaling it out as well.

I also changed us to use one object to store all the memoized values, instead of two. We were previously using two in order to implement memoization of undefined and null correctly, but with the new strategy to initialize an object with slots for every memo from the start, we can populate it with a `$notYetMemoized` symbol that indicates if we have memoized or not.
@airhorns
Copy link
Contributor Author

Closing in favor of #94

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