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

Use Module#include rather than prepend for faster method lookup #51640

Merged
merged 1 commit into from Apr 22, 2024

Conversation

nirvdrum
Copy link
Contributor

@nirvdrum nirvdrum commented Apr 22, 2024

Motivation / Background

While looking at some performance work with @eregon, we noticed that Rails is using Module#prepend on core classes. Usually that's done when introducing a new implementation of an existing method and needing to ensure the correct one is used. Here, the methods being introduced are all brand new, so I think Module#include can be used instead. Modules prepended go through a more involved method lookup than included methods and can hamper VM optimization.

Detail

This PR changes some mixed in modules that don't need to be prepended to be included instead.

Additional information

The use of prepend on core classes like Integer undoes some interpreter optimizations performed in TruffleRuby. Arguably, the interpreter should detect that core methods aren't being redefined in this case and avoid a cache invalidation. We're planning on fixing that for the next TruffleRuby release. However, I still think using include when possible is preferable for simpler method lookup semantics in all Ruby implementations.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@nirvdrum
Copy link
Contributor Author

Sorry. I read the bit about not creating draft PRs, but accidentally clicked the wrong button :-/

…nd` semantics here and `include` leads to faster method lookups.
@nirvdrum nirvdrum marked this pull request as ready for review April 22, 2024 20:04
@@ -46,7 +46,7 @@ def to_json(options = nil)
end

[Enumerable, Object, Array, FalseClass, Float, Hash, Integer, NilClass, String, TrueClass].reverse_each do |klass|
klass.prepend(ActiveSupport::ToJsonWithActiveSupportEncoder)
klass.include(ActiveSupport::ToJsonWithActiveSupportEncoder)
Copy link
Member

Choose a reason for hiding this comment

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

Are you sure we can call include here? We need to call super in the to_json implementation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're guaranteed to load after JSON because of the require "json" on line 4. While there could be something interfering with the typical load process, the test suite passes and I couldn't find a failure case locally. I tried with a few different data types, including a custom Struct so I'd hit the Object case, and the results were the same with either prepend or include.

If the reverse_each is changed to each then there's a behavioral difference between prepend (tests pass in both directions) and include (tests only pass with reverse_each), but the reverse_each is already there.

@rafaelfranca rafaelfranca merged commit fee72c3 into rails:main Apr 22, 2024
3 checks passed
@rafaelfranca rafaelfranca deleted the prepend-to-include branch April 22, 2024 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants