Use Module#include
rather than prepend
for faster method lookup
#51640
+4
−4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 thinkModule#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 likeInteger
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 usinginclude
when possible is preferable for simpler method lookup semantics in all Ruby implementations.Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]