Field_collection currently is using many DB queries in the uncached case.
By using field_collection_field_formatter_prepare_view and recursing into the entities the performance can be increased by 50%.
With three field collection fields and one of them containing another field_collection I get:
Before: 304 calls to entity_load
After: 4 calls to entity_load
Before for field_collection_item_load_multiple: 8,308,574
After for field_collection_item_load_multiple: 2,525,895
Patch and xhprof shots attached.
Enjoy!
Best Wishes,
Fabian
Comments
Comment #2
Fabianx CreditAttribution: Fabianx commentedBefore:
After:
field_collection_item_load_multiple before:
field_collection_item_load_multiple after:
The above is after a fresh cache clear with just homepage loaded afterwards and then this page with XHPROF.
And some live data for the record on DevCloud with the record already cached with entity_cache, field_cache, etc.:
Without Patch:
Avg | Max
1,376.0 | 1,374.0
With Patch:
Avg | Max
964.0 | 949.5
So a good 400ms saved on average.
Comment #3
Fabianx CreditAttribution: Fabianx commentedAttached new patch against 7.x-1.x-dev
Comment #4
dealancer CreditAttribution: dealancer commentedAn awesome job!
Comment #5
dealancer CreditAttribution: dealancer commentedThere is a tab here.
Comment #6
tim.plunkettOut of curiosity, why the use of field_read_fields and not field_info_fields?
I need to give this a real review soon.
Comment #7
Fabianx CreditAttribution: Fabianx commentedHi,
field_read_fields was used, because below in the code it said that field_info_fields had been replaced with field_read_fields.
So I used the same function for consistency reasons.
However probably there is no problem to change it to field_info_fields.
Best Regards,
Fabian
Comment #8
Fabianx CreditAttribution: Fabianx commentedBumpy bump!
Hey, tim.plunkett, any chance you can review the patch / approach?
Comment #9
grasmash CreditAttribution: grasmash commentedI'd love to see this patch committed. According to my performance tracking tools, field collection is responsible for nearly 90% of my db hits for a single node view. That's with just one field collection per node, and no field collection nesting.
Comment #10
fagooh, yes we should definitely do a multiple load in pre-view here.
The patch needs work though:
* $item['entity'] is already the place for the field-collection
* No static cache needed, the entity controller has that already.
* Comments (hook implementations, ..) are missing and need work.
* Variable names should be descriptive and match with what is used in the code else.
Comment #11
mrfelton CreditAttribution: mrfelton commentedThis brings massive improvements to our setup, which has 4 multiple value field collections, each with 6 fields attached to a custom entity type. When loading the edit form for an entity with about 100 rows of field collection data, this slashes the number of queries from 2431 down to 1117, cuts about 250ms off the page execution time, and reduces the memory footprint from 217MB down to 180MB. A massive improvement, although it's still a little on the slow side.
Comment #12
mrfelton CreditAttribution: mrfelton commentedOh, and the patch didn't apply to the latest dev, as the change to field_collection_field_get_entity seems to already be in the latest codebase. Updated patch attached.
Comment #13
k.skarlatos CreditAttribution: k.skarlatos commentedThis looks very interesting. Does the updated patch address the issues of #10?
Comment #14
klokie CreditAttribution: klokie commentedComment #15
jerdavis12: 1427158_Use-field_collection_field_formatter_prepare_view-to-increase-performance_12.patch queued for re-testing.
Comment #16
jenlamptonI'm seeing the same kinds of improvements on my site as well. Thanks for the patch @Fabianx!
Comment #17
joseph.olstadgreat patch, rerolling just for testbot.
credit to FabianX and mrfelton
Comment #18
wildfeed CreditAttribution: wildfeed as a volunteer commentedWondering about status of patch in #17.
Completed testing?
Ready to be merged into dev branch?
Thoughts?
Comment #20
ram4nd CreditAttribution: ram4nd as a volunteer commentedComment #21
joseph.olstadThanks @ram4nd !
Comment #23
Liam MorlandThe change in this issue has changed what views_get_view_result() does and broken our site; please see #3109657: field_collection_field_formatter_prepare_view() changes return value of views_get_view_result(). I would appreciate any insight into the problem.
Comment #24
Liam MorlandThis problem appears to have been fixed in #3091188: Field Collection stop working. It would be great to get a new release with this fix.