-
Notifications
You must be signed in to change notification settings - Fork 22k
Perf: micro optimised Result column hash_row creation #12065
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
index = 0 | ||
length = columns.length | ||
|
||
while index < length |
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.
Is this significantly more performant than using columns.each_with_index
?
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.
@egilburg I tried pretty much everything, nothing is as fast as the naive implementation see: |
@tenderlove one thing we can think of is returning a proxy object from [] etc. That way you hold a single hash that maps column names to position in array and reuse it for all rows. It would be a pretty strong optimisation for the massive amount of over-querying rails apps tend to do. Quite often I see 50 columns selected even though only a handful are used. |
Sam, that sounds like a good idea. Can you try it? I like the idea, and I can't think why it would work. If it doesn't work, we can just pull in this optimization anyway. Aaron Patterson On Aug 29, 2013, at 5:18 PM, Sam notifications@github.com wrote:
|
@tenderlove I would like to get to it, but it may take a bit, recommend we pull this fix in as-is into Rails 4, since it is super safe. I am concerned changing the classes Result returns is going to need much more regression testing. |
@SamSaffron sounds good. I'll merge this and backport! |
Perf: micro optimised Result column hash_row creation
Perf: micro optimised Result column hash_row creation
This does replace rather elegant code with more verbose code, but for good reason.
With this change applied array allocation in AR is heavily reduced:
For a Discourse sample query I am seeing
Before:
After:
So T_ARRAY allocations are reduced from by almost half. A micro bench shows this is also faster cpu wise.
Results: