Problem

  • If I click to add new paragraph, it takes 15 seconds for the form to respond.
  • If I try opening the form in the first place, it takes 5 seconds to respond.
  • Profiling shows that functions like "file_exists, RecursiveDirectoryIterator::hasChildren, realpath, filemtime" takes all the time. For example, function "file_exists" is called 118,462 times.
  • And these calls are initiated by "Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery::getDefinitions", but all that function is doing, is searches for "...Plugin/conditional_fields/handler" across all the folders. It repeats that search 726 times, so this multiples to file_exists alone being called 118,462 times.

Steps to reproduce

  • I have these modules installed: inline_entity_form + paragraphs + conditional_fields.
  • Have many fields with many conditions
  • Locally on my computer (virtualbox), website works fast and does not care about gazzilion of file searches, but the server responds to this very slowly. (Usually, it should be the other way around, so this confused me in processes of finding the slowness cause)

Expected bahaviour

It should only search for plugins a single time, not 726 times.

Proposed resolution

Improve plugin discrovery code, for example, cache things or make vars static.

Comments

mindaugasd created an issue. See original summary.

mindaugasd’s picture

Issue summary: View changes
mindaugasd’s picture

sboden’s picture

I can confirm this happening in 8.x-1.0-alpha6. It's a bug in the conditional_fields plugin discovery, discovery fails and then tries again every time while discovery was supposed to run over all installed modules only once (hence the large amount of file_exists() you see being executed) ... Currently the problem seems to be fixed in 8.x-1.x-dev as the source code was "reshuffled".

solide-echt’s picture

AFAIK this is still happening in 8.x-1.x-dev. My guess was this was due to the ConditionalFieldsHandler Annotation class not defining an @ingroup (as does for example the different views plugin classes do) but the issue is still present after adding @ingroup conditional_fields_handler_plugins to both the Annotation class and the individual handlers. I think there is a more architectural issue here: the discovery is done per field whereas in principe it should be possible to do the discovery per form.

No quick fix if that's at stake here... Digging deeper though.

Eric

sboden’s picture

Attached a patch against 8.x-1.x which solves the super slowness, but there are more issues even in that version to make conditional_fields work ok in a project.

What this particular patch changes is the way form validation is done. In our project we saw one function being called 150.000 times before this patch, and only 200 times after the patch, on a form with 200 conditionals. Before the patch, validation is done for the whole form for every field, afterwards the validation should only be done once for the whole form.

It's not that easy making a patch against alpha6 as the code was changed a lot afterwards.

solide-echt’s picture

Could not apply the patch cleanly but it wasn't hard to apply the changes by hand and it definitely improves performance a lot. Thnx for that.

To further elaborate on the architecture: I still have no clue why even field based validation is *this* slow. I've written a client specific module (for D7) that stores validations as custom entities (so we can give better feedback on what needs to be done on a form) that contains more than 100 fields and it's not slow (to be honest: it's not superfast either...). And the States API works on field level too but it's not slow and the discovery process is not run 10.000's of times.

sboden’s picture

In alpha6 there's another performance problem, this issue https://www.drupal.org/project/conditional_fields/issues/3115279 ... For some reason the discovery of the plugins is broken, and so conditional_fields keeps trying to scan the whole filesystem under modules. I had a patch for that too, but since I started from a newer version afterwards, I forgot about that (in a newer version, that was fixed).

It will take me a little bit of time to find that patch again.

kris77’s picture

Thank a lot @sboden.

I tried your patch and it greatly improved the speed of my form.

I have 6 dependent fields built with the business_rules module.

socialnicheguru’s picture

Status: Active » Needs review
tbenice’s picture

Just re-rolling the patch against 1.x-dev to make it apply. Also applies against alpha7.

  • colan authored 03aafa3 on 8.x-1.x
    Issue #3086250 by colan: Reversed the order of functions for readability...
  • sboden authored 9b62ef0 on 8.x-1.x
    Issue #3086250 by sboden, tbenice, mindaugasd, solide-echt, Kris77,...
colan’s picture

Status: Needs review » Fixed
Issue tags: +Performance
Related issues: +#3115279: Plugin handlers don't load properly

Thanks for this! I got a form submission down from 1min33s to 11 seconds.

Added another commit to clean things up a bit afterwards.

I'm appreciate some insight into #3115279: Plugin handlers don't load properly. Is that still a problem? Are there any other performance issues we should be looking at?

Status: Fixed » Closed (fixed)

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