Avoid calling to_s at end of ERB#45756
Merged
jhawthorn merged 1 commit intorails:mainfrom Aug 4, 2022
Merged
Conversation
With the recent changes to OutputBuffer, calling `to_s` is extremely expensive if the buffer later is concatenated to (if the buffer never changes it should be relatively inexpensive, as Ruby will share memory).
6f72df1 to
ee68644
Compare
Contributor
|
🤦 I definitely didn't think of shared string while writing this. I'm definitely good with returning the buffer itself, rendering partials directly into the parent buffer is one of the things I'm trying to achieve with these refactors, so 👍 . Until we get there though, we might need to make sure |
Contributor
|
But I'm good with merging this as is 👍 |
byroot
approved these changes
Aug 4, 2022
Member
Author
I think that should be fine as |
kaspth
added a commit
to bullet-train-co/jbuilder-schema
that referenced
this pull request
Oct 9, 2023
Rails 7.1 has sped up its internal OutputBuffer implementation (what handles << concatenation in templates), which we rely on tricking a bit. rails/rails#45614 Then a follow up PR the `to_s` call was moved to after calling `view._run` rails/rails#45756 However, we rely on the return value being a Hash (which I'm not sure how to fix yet). So since `target!` is the return value of our templates, we can wrap that and ensure `to_s` doesn't actually `to_s`, which Rails 7.1 will call for us. On Rails < 7.1 we must call the compatibility `unwrap_target!` method ourselves to get our inner Hash return value. This also adds a Rails version matrix so we can detect these things before release in the future.
kaspth
added a commit
to bullet-train-co/jbuilder-schema
that referenced
this pull request
Oct 9, 2023
* Fix Rails 7.1 compatibility. Rails 7.1 has sped up its internal OutputBuffer implementation (what handles << concatenation in templates), which we rely on tricking a bit. rails/rails#45614 Then a follow up PR the `to_s` call was moved to after calling `view._run` rails/rails#45756 However, we rely on the return value being a Hash (which I'm not sure how to fix yet). So since `target!` is the return value of our templates, we can wrap that and ensure `to_s` doesn't actually `to_s`, which Rails 7.1 will call for us. On Rails < 7.1 we must call the compatibility `unwrap_target!` method ourselves to get our inner Hash return value. This also adds a Rails version matrix so we can detect these things before release in the future. * Allow us to run tests in multiple Rails versions * Yep, I'm calling it, don't think it's worth it anymore * Fix enum invocation for Rails 6.1 tests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
With the recent changes to OutputBuffer, calling
to_sis extremely expensive if the buffer later is concatenated to (if the buffer never changes it should be relatively inexpensive, as Ruby will share memory).This tries to avoid calling
to_swhere possible, and to explicitly return nil when a buffer is passed totemplate.renderBefore
After