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

Improve cache hits and JIT friendliness #486

Merged
merged 3 commits into from Dec 15, 2022
Merged

Improve cache hits and JIT friendliness #486

merged 3 commits into from Dec 15, 2022

Conversation

tenderlove
Copy link
Contributor

Hi,

This PR adds a few commits to improve inline cache hits for instance variables as well as improve JIT friendliness. Removing and adding instance variables is very bad for JIT compilers, so this PR removes the "remove" calls. When we want to remove any caches or precomputed values, we simply set the instance variable to nil, or to the sentinel object. We don't have to check whether or not the IV is defined before setting the value, we can just set it.

This PR also removes some calls to defined? that didn't do anything. defined? will always return true for local variables, so there was no need to check it.

Finally, since we can stop checking defined? on IVs, I was able to convert some of the reader methods to just use attr_reader.

Thanks!

lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Outdated Show resolved Hide resolved
lib/addressable/uri.rb Show resolved Hide resolved
@tenderlove
Copy link
Contributor Author

I'm happy to fix the style issues, but I believe they were in the pre-existing code. Please let me know, and I'll fix them. 😃

@dentarg
Copy link
Collaborator

dentarg commented Dec 15, 2022

That's fine, it was obvious to me that you aimed for the minimal amount of changes. That made it easy to read the diff. (It is a bit unfortunate that there's a bot with rules that aren't consistent with the existing code; should be addressed in a separate PR IMHO (not saying it is anything you need to do))

I'm thinking about the change from guards/early returns, many lines changed just because they needed indenting. Could that be avoided or is this how it need to be in order to be performant?

Just trying to keep future git blame:ing in mind when merging changes.

@tenderlove
Copy link
Contributor Author

@dentarg nope, we don't need to do any indentation changes. I just did them because I guess that's the way it's supposed to be (but I was basically doing a mechanical replacement, and I have no opinion). I can change them to return blah unless blah == NONE. It's really up to you and I'm happy to make that change! 😃

Btw, if you add ?w=1 to the query string, GitHub will show you the diff but eliminating whitespace. Here's a link.

@tenderlove
Copy link
Contributor Author

In retrospect, I probably should have done the return unless pattern to get rid if the whitespace. I will do it and push again. Thanks for your patience!

This commit makes Addressable::URI more cache friendly.  It changes the
internals to always define all instance variables, and uses a sentinel
object in the cases where we need to cache a computation that could
possibly result in nil.
`defined?` always returns true for local variables, so there's no need
to check these
@tenderlove
Copy link
Contributor Author

Ok, the diff should be much smaller now. Thank you!

@dentarg
Copy link
Collaborator

dentarg commented Dec 15, 2022

I know about ?w=1 (it is in the UI too now) and I did use it :-)

Thanks for these changes, they look good to me!

Copy link
Collaborator

@dentarg dentarg left a comment

Choose a reason for hiding this comment

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

I wont squash merge this as the individual commits look to be valuable. Thanks again.

@dentarg dentarg merged commit d6f6288 into sporkmonger:main Dec 15, 2022
@dentarg dentarg mentioned this pull request Dec 15, 2022
@tenderlove tenderlove deleted the better-iv branch December 15, 2022 16:04
@tenderlove
Copy link
Contributor Author

Thank you!

casperisfine pushed a commit to casperisfine/addressable that referenced this pull request Apr 27, 2023
Ref: sporkmonger#486

Following the introduction of `NONE` flags, `Addressable::URI`
instance can no longer be correctly serialized with YAML.

It appear to work, but `NONE` is serialized as `!ruby/object {}`,
so when the YAML is parsed back, the attributes are assigned a
distinct anonymous object.
casperisfine pushed a commit to casperisfine/addressable that referenced this pull request May 2, 2023
Ref: sporkmonger#486

Following the introduction of `NONE` flags, `Addressable::URI`
instance can no longer be correctly serialized with YAML.

It appear to work, but `NONE` is serialized as `!ruby/object {}`,
so when the YAML is parsed back, the attributes are assigned a
distinct anonymous object.
dentarg pushed a commit that referenced this pull request May 19, 2023
Ref: #486

Following the introduction of `NONE` flags, `Addressable::URI`
instance can no longer be correctly serialized with YAML.

It appear to work, but `NONE` is serialized as `!ruby/object {}`,
so when the YAML is parsed back, the attributes are assigned a
distinct anonymous object.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
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.

None yet

3 participants