Problem: Node has an Entityreference field, the selectbox widget loads all nodes from referenced content type. The number of nodes of this content type is over 2000, so the node/[nid]/edit page loads 36 sec. When I select autocomplete widget for entityreference field the same page loads 3 sec.
While handling this issue I`ve found:
1. getReferencableEntities function in EntityReference_SelectionHandler_Generic.class.php builds options array for the selectbox widget. In https://www.drupal.org/node/2151631 - the 2151631-getReferencableEntities-node-users-20.patch I`ve tested - it improves the performance so the page loads 3 sec. instead of 36 sec. - it fixes the problem by creating the options array using db_query.
2. Why is it so slow: The getReferencableEntities function uses function getLabel() to retrieve the label for each option. getLabel() goes to entity_metadata_no_hook_node_access function in Entity module. I`ve found that the preformance can be boosted if I change $default_revision = node_load($node->nid) to db_select, while not loading the whole node - we need just the "vid" field in that function. Such changes improve the performance form 36 sec to 5 sec for the node/[nid]/edit page.
It appears that Entity module performance can be improved by changing node_load to db_select, that in fact boosts the performance of Entityreference module.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | 2636332-entity_api_performance-12.patch | 771 bytes | ethant |
| #2 | 2636332-entity_api_performance-1.patch | 652 bytes | hego |
Comments
Comment #2
hego commentedComment #4
hego commentedComment #5
temkin commentedSelected the right version. Marked for re-test.
Comment #7
temkin commentedComment #8
azinck commentedThis seems smart. Will test.
Comment #9
azinck commentedThis does help; though not all that significantly if you're already running the entitycache module.
That said, RTBC #2
Comment #10
fagopassing on this custom select result as a regular $node is an API violation which will lead to bugs once modules expect a regular entity. So this is something we cannot do.
Comment #11
azinck commented@fago Can you elaborate on what you mean by your comment? Maybe I'm missing your point, but $default_revision is only used within our own function. Where is the API violation here?
Comment #12
ethantRerolling to be compatible with entity 7.x-1.9