Although I honestly enjoy micro-optimizing, this change will probably have no effect outside of a microbenchmark. Have you tested it in a larger context?
Also, String#freeze would be more idiomatic. EDIT: It was late, and I wasn't thinking straight. String#freeze isn't really applicable when using interpolated strings.
@jonathanhefner
Thanks. I agree with your opinion. There might be no effect on a typical Rails app.
However, ActiveModel is used in various contexts independent of Rails.
This change benefits on batch processing like data format conversions using ActiveModel.
Thank for the pull request but could you just put # frozen_string_literal: true to the top of the file? Or the performance improvement come from avoiding to do the interpolation twice?
@rafaelfranca
Thanks, I just put # frozen_string_literal: true.
The problem was unnecessary repeated string interpolations as you mentioned.
@kaspth
Thanks. I noticed that quoted symbols are better as a whole.
Following reflection methods(respond_to? and public_send) handles symbols faster than strings.
after 40.172k (± 2.6%) i/s - 203.149k in 5.060347s
before 32.679k (± 4.0%) i/s - 163.929k in 5.025078s
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.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.
Summary
Improve the performance of ActiveModel initializer with removing duplicated string object generation.
Other Information
simple benchmarks for
ActiveModel::new
1.2x faster than the current implementation.