Perf: micro optimised Result column hash_row creation#12065
Perf: micro optimised Result column hash_row creation#12065tenderlove merged 1 commit intorails:masterfrom
Conversation
There was a problem hiding this comment.
Is this significantly more performant than using columns.each_with_index?
There was a problem hiding this comment.
|
@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 [email protected] 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: