Problem/Motivation

1. When saving a node with revisions and with 10+ paragraphs it is slow

Steps to reproduce

1. Create a node with a lot of translation and has 10+ paragraphs in it.
2. Edit and save it
3. Saving it is slow, Sometimes leads you to 503 error

Comments

erwindaez created an issue.

yashaswi18’s picture

I took some time and checked the ParagraphsWidget.php code and found a couple of things that are likely causing this.

In massageFormValues(), every time you save a node it loops through all paragraphs and runs heavy validation and form extraction on each one. With 10+ paragraphs and multiple translations, this fires a lot of database queries in one request, which is likely why you are also hitting the 503 timeout. I also noticed that EntityFormDisplay::collectRenderDisplay() in formElement() hits the database once per paragraph even when they are all the same type.

Some solutions that might possibly help:
1. Cache the form display so it only loads once per paragraph type:

static $display_cache = [];
$cache_key = $paragraphs_entity->bundle() . ':' . $this->getSetting('form_display_mode');
if (!isset($display_cache[$cache_key])) {
  $display_cache[$cache_key] = EntityFormDisplay::collectRenderDisplay(
    $paragraphs_entity,
    $this->getSetting('form_display_mode')
  );
}
$display = $display_cache[$cache_key];

2. Skip validation for paragraphs that haven't changed:
In massageFormValues(), add a check before running the full validate() call

3. Disable paragraph revisions if you don't need them