-
Notifications
You must be signed in to change notification settings - Fork 22k
PERF: avoid allocating column names where possible #33051
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
Conversation
(@rails-bot has picked a reviewer for you, use r? to override) |
@@ -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 } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might want to change to columns = @columns.map(&:-@)
for a small speed improvement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@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.
88a2f1f
to
a46dcb7
Compare
@rafaelfranca done! 🎊 |
❤️ |
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