#2116363: Unified repository of field definitions (cache + API) adds more STARTS_WITH id conditions for field config instances. We already optimize the case where we have a direct match, we could easily extend that to use strpos() for STARTS_WITH and other conditions.

Comments

berdir’s picture

Issue tags: +Needs tests
StatusFileSize
new2.48 KB

Something like this I think...

yched’s picture

Code looks good - minor nitpick :

+++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
@@ -151,7 +155,43 @@ protected function loadRecords() {
     // If no restrictions on IDs were found, we need to parse all records.
     else {
-      $names = $this->configStorage->listAll($prefix);
+      $all_names = $this->configStorage->listAll($prefix);
+      // In case we have an ID condition, try to filter down the list of config
+      // objects to load.

This addition makes the existing comment a bit moot :-)
This does a second pass looking at all existing names, so we still have a chance to avoid parsing all existing contents.

berdir’s picture

Status: Active » Needs review

jhedstrom’s picture

+++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
@@ -151,7 +155,43 @@ protected function loadRecords() {
+      if ($id_condition) {
+      $names = array();
+      foreach ($all_names as $name) {

Indentation error here.

Perhaps this new logic could be moved to another method, something like filterByIdCondition() (it would simplify the loadRecords() method).

This seems tricky to explicitly test since this only alters internal behavior, and the end result from execute() should be the same as it is now?

chx’s picture

Since entity query is required for mongodb I am considering this work and will chime in: array_filter was made for this. Use the switch to create the filter closure and then run array_filter on it. Don't forget to invert the conditions from the current code since current adds but array_filter removes. For eg the <>:

function ($id) use ($id_condtion) {
   return $id == $id_condition['value']);
}
yched’s picture

@chx : we can do that. Not sure it's a real legibility win in the end, but why not :-)

chx’s picture

Status: Needs review » Needs work

Now this shows where we need more tests: as I said in #7 we need to invert the conditions and yet it passed.

yched’s picture

Status: Needs work » Needs review

Er - we shouldn't invert the conditions ?

array_filter keeps the array values where the callback returns TRUE.
Thus if the condition is '<>', the callback should return true if the id is !== that the condition value,
etc.

The callbacks as written in the patch are correct AFAICT.

chx’s picture

Oh, of course, I have no idea what I am talking about :/ Sorry!

case '<>':closing brace has an indent error and still needs tests.

yched’s picture

Issue tags: -Needs tests
StatusFileSize
new5.08 KB
new3.08 KB

With tests.

The fact that you can write EntityQueries with STARTS_WITH, CONTAINS... conditions is not added by this patch, so arguably it shouldn't be the job of this patch to add tests for them :-)

What we should be testing is the fact that those conditions are evaluated without loading all the entities, but I'm not sure we can do that.

yched’s picture

Reroll + bump ?

chx’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, then.

alexpott’s picture

yched’s picture

@alexpott: looks related but different - this case here is about conditions on IDs, where optimizing requires no denormalization in storage, and PHP can reduce the set of results before even hitting the db ?

IIRC we do have a couple queries like "select all field config starting with node.article.*", I don't think any denormalization will optimize those ?

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new3.89 KB

What happens if we have two ID conditions in a query?

+++ b/core/lib/Drupal/Core/Config/Entity/Query/Query.php
@@ -141,6 +145,41 @@ protected function loadRecords() {
     // If no restrictions on IDs were found, we need to parse all records.

This comment now seems out of place.

I proposed we do something like the interdiff attached - which we need tests for.

yched’s picture

@alexpott: do you think it's really a case we need to care optimizing ?
If there are several conditions on IDs, we use the first one to restrict the set of records to load, and then the other cnditions are checked on the records. Not sure we really need to do more ?

alexpott’s picture

@yched in the case of several conditions we're using the last which seems non-obvious to me

berdir’s picture

This needs to be rerolled after #2430219: Implement a key value store to optimise config entity lookups got in. Would be great if someone in LA can give it a try to make this work again.

Not sure if the discussion above is still relevant, let's see how the reroll looks like.

alexpott’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll +Configurables
StatusFileSize
new5.57 KB

@Berdir your wish is my command :)

Rerolled.

berdir’s picture

Priority: Normal » Major

Thanks!

So, using the same scenario as #2482295: Rebuilding field map with many bundles/fields is very slow, this is already a huge improvement on the execution time:

$ time drush cr
Cache rebuild complete.

real	0m12.820s
user	0m10.060s
sys	0m0.348s

So, #2482295: Rebuilding field map with many bundles/fields is very slow is only about twice as fast as this, unlike compared to HEAD, where it is ~25x as fast ;)

For memory usage, I got some really interesting results. I'd expect this to still use more memory, but it actually uses a huge amount less. The reason for that is that even without getFieldMap(), we still have 5 calls to field_entity_bundle_field_info(), and that uses an immense amount of memory.

This, for the same drush script:

Total Incl. MemUse (bytes):	115,405,688 bytes
Total Incl. PeakMemUse (bytes):	151,858,960 bytes
field_entity_bundle_field_info(): 511 calls, 5.4s, 95M inclusive peak memory according to xhprof

#2482295: Rebuilding field map with many bundles/fields is very slow:

Total Incl. MemUse (bytes):	114,236,472 bytes
Total Incl. PeakMemUse (bytes):	236,995,744 bytes
field_entity_bundle_field_info(): 5 calls, 5.4s, 186M inclusive peak memory according to xhprof

So the function uses exactly as much time for 1/100 of the calls, and twice as much memory ;)

But actually, it's perfectly possibly to combine the two patches:

with xhprof:
Total Incl. MemUse (bytes):	60,740,680 bytes
Total Incl. PeakMemUse (bytes):	65,105,712 bytes
field_entity_bundle_field_info(): 5 calls, .1s, 1.2M inclusive peak memory according to xhprof

measured without xhprof:
real	0m3.251s
52.56MB peak memory.

WIN! :)

I think that qualifies as at least major ;)

berdir’s picture

Patch looks good to me.

I agree with @yched that supporting multiple ID conditions doesn't seem necessary, that seems like an extremely rare use case. But I also agree that it's strange that we actually use the last one to filter. So maybe just add a && !$id_condition to the if and update the comment to say that we use the first id condition to filter down?

yched’s picture

+1 to #23

Also, the rerolled code after after #2247379: Optimize config entity query conditions on ID looks a bit confusing between "condition on names" and "condition on IDs"
(but that might be because I didn't really look into what that other issue was doing)

alexpott’s picture

StatusFileSize
new775 bytes
new5.63 KB

Fixed #23.

re #24 - a config name is field.field.block_content.body and an ID is block_content.body. The current code makes this consistent.

berdir’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me now :)

jibran’s picture

:)

berdir’s picture

No, I don't think it does.

This is an internal optimization for certain conditions. There's nothing to configure, update or use, so no need to update any change records.

catch’s picture

Committed/pushed to 8.0.x, thanks!

  • catch committed b480c52 on 8.0.x
    Issue #2247379 by yched, alexpott, Berdir: Optimize config entity query...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.