PERF: avoid allocating column names where possible #33051
Merged
Conversation
(@rails-bot has picked a reviewer for you, use r? to override) |
activerecord/lib/active_record/result.rb
Outdated
@@ -125,7 +125,7 @@ def hash_rows | |||
begin | |||
# We freeze the strings to prevent them getting duped when | |||
# used as keys in ActiveRecord::Base's @attributes hash | |||
columns = @columns.map { |c| c.dup.freeze } | |||
columns = @columns.map { |c| -c } |
bdewater
Jun 5, 2018
Contributor
Might want to change to columns = @columns.map(&:-@)
for a small speed improvement.
Might want to change to columns = @columns.map(&:-@)
for a small speed improvement.
SamSaffron
Jun 5, 2018
Author
Contributor
@bdewater yes I can confirm this is worth it to change, I will amend this PR tomorrow
https://gist.github.com/SamSaffron/805bf43016cc897c0f0c19078e7ad171
@bdewater yes I can confirm this is worth it to change, I will amend this PR tomorrow
https://gist.github.com/SamSaffron/805bf43016cc897c0f0c19078e7ad171
It's the reason Rubocop has Style/SymbolProc. There's a lot of context in #16833
require 'benchmark/ips'
COLUMNS = %w(id foo bar baz qux test yolo)
def block
COLUMNS.map { |c| -c }
end
def to_proc
COLUMNS.map(&:-@)
end
Benchmark.ips do |x|
x.report("block") { block }
x.report("to_proc") { to_proc }
x.compare!
end
|
LGTM! Let me know when you update the PR to include the |
When requesting columns names from database adapters AR:Result would dup/freeze column names, this prefers using fstrings which cuts down on repeat allocations Attributes that are retained keep these fstrings around for the long term Note, this has the highest impact on "short" result sets, eg: Topic.first where you can void allocating the number of columns * String.
@rafaelfranca done! |
|
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.
When requesting columns names from database adapters AR:Result
would dup/freeze column names, this prefers using fstrings which
cuts down on repeat allocations
Attributes that are retained keep these fstrings around for the long
term
Note, this has the highest impact on "short" result sets, eg: Topic.first where you can void allocating the number of columns * String.
See also: https://samsaffron.com/archive/2018/02/16/reducing-string-duplication-in-ruby for detailed explanation of uminus
per @rafaelfranca we are fine including uminus in master now. cc @tenderlove @sgrif