Problem/Motivation

When a taxonomy vocabulary contains too many terms, it is absolutely impossible to reach some pages like taxonomy overview, even if their content is paginated, because of huge memory usage.

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in /var/www/html/web/core/lib/Drupal/Core/Cache/DatabaseBackend.php on line 167

This is caused by the taxonomy_get_tree function for huge taxonomies, but for medium sized taxonomies, this is also caused by the fact that we never clean unused variables.

For information, with a 72000 terms taxonomy (what is not so many for big websites) :

$start = memory_get_usage(TRUE);
$tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid); // 72000 terms
$used_memory = memory_get_usage(TRUE) - $start;
echo $used_memory; // ~ 242 MB

Proposed resolution

Unset loadTree result when it is not needed anymore. This will not save the life of huge websites, but this will definetely help medium ones to stay usables.

Refactor the implementation to use less memory in general and especially with pagination.

Remaining tasks

Post the patch

User interface changes

None

API changes

None

Beta phase evaluation

-->

Reference: https://www.drupal.org/core/beta-changes
Issue category Bug because it can cause fatal errors on very common features
Issue priority Major because that's a bug
Prioritized changes The main goal of this issue is performance. (By clearing that unused variables we can free up a lot of memory)
Disruption Not disruptive for core, contributed and custom modules because the variables are cleared while neither the core or a hook uses them.
CommentFileSizeAuthor
#94 2024-12-12_09-23-17.png35.44 KBrobgreeniowa
#84 2183565-84.patch9.33 KBalexpott
#84 83-84-interdiff.txt1.27 KBalexpott
#83 2183565-83.patch9.47 KBalexpott
#83 81-83-interdiff.txt6.22 KBalexpott
#81 2183565-81.patch7.08 KBplopesc
#80 2183565-80.patch7.08 KBplopesc
#80 interdiff-80-77.txt2.25 KBplopesc
#78 interdiff-77-69.patch1.85 KBplopesc
#77 2183565-77.patch6.25 KBplopesc
#71 Screenshot 2023-06-08 at 3.18.48 PM.png82.78 KBsmustgrave
#71 Screenshot 2023-06-08 at 3.18.43 PM.png84.9 KBsmustgrave
#69 2183565-69.patch5.71 KBplopesc
#69 interdiff-67-69.txt817 bytesplopesc
#67 2183565-67.patch5.7 KBplopesc
#67 2183565-67-test-only.patch2.61 KBplopesc
#67 interdiff-65-57.txt5.57 KBplopesc
#65 2183565-65.patch1.44 KBplopesc
#56 2183565_56.patch8.57 KBdrugan
#54 2183565_54.patch1.3 KBsivaji_ganesh_jojodae
#51 increase-memory-on-specific-vocabularies-2183565.patch780 bytesbetoaveiga
#49 2183565-49.patch1.3 KBcesarmiquel
#45 interdiff.txt1.54 KBgrndlvl
#45 2183565-45.patch1.31 KBgrndlvl
#39 2183565-39-0902-term-overview-memory-limit.patch2.02 KBsweetchuck
#38 2183565-38.patch9.23 KByogeshmpawar
#28 2183565-28.patch10.07 KBmarvil07
#25 reduce_memory-2183565-25.patch10.04 KBmelvinlouwerse
#20 reduce_memory-2183565-20.patch9.89 KBmelvinlouwerse
#17 reduce_memory-2183565-17.patch9.82 KBduaelfr
#10 interdiff.txt4.7 KBjcnventura
#10 reduce_memory-2183565-10.patch9.85 KBjcnventura
#7 reduce_memory-2183565-7.patch6.34 KBjcnventura
#1 drupal-8.x-reduce_taxonomy_memory_consumption-2183565-1.patch6.42 KBduaelfr

Issue fork drupal-2183565

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

duaelfr’s picture

Assigned: duaelfr » Unassigned
Status: Active » Needs review
StatusFileSize
new6.42 KB

Here is the patch.
I hope it will help someone.

This patch is part of the #1day1patch initiative.

marcingy’s picture

Priority: Major » Normal

You can work around this with modules such as taxonomy manager. Downgrading to normal.

duaelfr’s picture

Priority: Normal » Major

First, we are talking about D8 so we cannot rely on contrib modules that have not been upgraded for the moment.
Second, it is a Core issue. People would not understand why it is happening and why we can only say them to look for contrib modules which will lower their website performances to give them a core feature that is not working.
Third, this patch is really simple and a core maintainer would only need 20 minutes to review.
Finally, the official documentation about issues' priority tends to say that this IS major.

xjm’s picture

Status: Needs review » Needs work

The last submitted patch, 1: drupal-8.x-reduce_taxonomy_memory_consumption-2183565-1.patch, failed testing.

jibran’s picture

jcnventura’s picture

Status: Needs work » Needs review
StatusFileSize
new6.34 KB

Re-rolling. Interdiff didn't make sense as most files have different paths now.

jcnventura’s picture

Issue summary: View changes
duaelfr’s picture

Thank you for reviving this issue and rerolling that patch.

jcnventura’s picture

Title: Reduce memory consumption of the functions using taxonomy_get_tree (includes the taxonomy overview and forums) » Reduce memory consumption of the functions using taxonomy loadTree (includes the taxonomy overview and forums)
StatusFileSize
new9.85 KB
new4.7 KB

Discovered a few more cases where the results of the loadTree can be cleaned. I think the results of this patch are probably only useful for those using the forum module and several thousand taxonomy terms.

lauriii’s picture

duaelfr’s picture

Issue summary: View changes
Issue tags: -Needs beta evaluation

Added beta evaluation.

@lauriii I'd need some help to profile this issue. Do you have some resources about profiling D8 or can you guide me through that? I've already used xhprof a bit but I'm not really comfortable with it.

lauriii’s picture

There is quite an extensive documentation available about profiling here: https://www.drupal.org/contributor-tasks/profiling . If you have any further questions please feel free to ping me on IRC! :)

Status: Needs review » Needs work

The last submitted patch, 10: reduce_memory-2183565-10.patch, failed testing.

duaelfr’s picture

Issue tags: +Needs reroll, +Barcelona2015

Starting the reroll now.

duaelfr’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new9.82 KB

Done!

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

melvinlouwerse’s picture

StatusFileSize
new9.89 KB

I rerolled the patch so it applies to 8.2.x-dev so i will add it here. For me however, i still have memory issues even after this patch.

duaelfr’s picture

Thank you!
It's a minor improvement, sadly we can't do magic here unless we make a deep and hard refactor.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dillix’s picture

I think we need to reroll patch for 8.4.x-dev, then I can test on vocabulary with 40k terms.

melvinlouwerse’s picture

StatusFileSize
new10.04 KB

Adding reroll for 8.4.x-dev, doubt it will work for a vocabulary with 40k items but is good to try.

mstef’s picture

Still exhausting 256MB on a vocabulary overview page containing 15000 terms using patch from #25.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

marvil07’s picture

StatusFileSize
new10.07 KB

A re-roll to follow core changes.

steven.wichers’s picture

These unset calls aren't likely to help anyone much. PHP is already going to free that memory when the $tree variable de-scopes. The unset will help in the scenario where you are just at the edge of your memory limit and the next thing within the same function that defined the $tree variable pushes you over.

On a site with 10714 terms

Without the patch:

Total Incl. MemUse (bytes): 235,988,560 bytes
Total Incl. PeakMemUse (bytes): 242,675,400 bytes

With the patch:

Total Incl. MemUse (bytes): 236,111,760 bytes
Total Incl. PeakMemUse (bytes): 242,864,088 bytes

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

toprak’s picture

Patches are still not useful. I think there is no solution yet.
Does anyone have a different approach?

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

schnydszch’s picture

Hi! I'm on Drupal 8.8.1 Php version 7.3 Mariadb 5.5.5-10.0.38-MariaDB-0ubuntu0.16.04.1 Apache web server. I imported 23,855 taxonomy terms. It seems I'm getting same error. I applied the patches in comment #28 and #25 it seems the files in the patch are not the same with Drupal installation.
Below is what I'm getting when applying the patch:

git apply -v 2183565-28.patch
Checking patch core/modules/forum/src/Form/ForumForm.php...
Hunk #1 succeeded at 144 (offset 1 line).
Hunk #2 succeeded at 156 (offset 1 line).
Checking patch core/modules/forum/src/ForumManager.php...
Hunk #1 succeeded at 438 (offset 22 lines).
Checking patch core/modules/node/tests/src/Functional/NodeAccessPagerTest.php...
error: while searching for:
$tree = \Drupal::entityManager()->getStorage('taxonomy_term')->loadTree($vid, 0, 1);
$tid = reset($tree)->tid;
$this->assertTrue($tid, 'General discussion term is found in the forum vocabulary.');

// Create 30 nodes.
for ($i = 0; $i < 30; $i++) {

error: patch failed: core/modules/node/tests/src/Functional/NodeAccessPagerTest.php:77
error: core/modules/node/tests/src/Functional/NodeAccessPagerTest.php: patch does not apply
Checking patch core/modules/taxonomy/src/Form/OverviewTerms.php...
error: while searching for:
$current_page[$key] = $term;
} while (isset($tree[++$tree_index]));

// Because we didn't use a pager query, set the necessary pager variables.
$total_entries = $before_entries + $page_entries + $after_entries;
$pager_total_items[0] = $total_entries;

error: patch failed: core/modules/taxonomy/src/Form/OverviewTerms.php:200
error: core/modules/taxonomy/src/Form/OverviewTerms.php: patch does not apply
Checking patch core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php...
error: while searching for:
foreach ($terms as $term) {
$options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityManager->getTranslationFromContext($term)->label());
}
}
}
}

error: patch failed: core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php:65
error: core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php: patch does not apply
Checking patch core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php...
error: while searching for:
$choice->option = [$term->id() => str_repeat('-', $term->depth) . \Drupal::entityManager()->getTranslationFromContext($term)->label()];
$options[] = $choice;
}
}
}
else {

error: patch failed: core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php:185
error: core/modules/taxonomy/src/Plugin/views/filter/TaxonomyIndexTid.php: patch does not apply
Checking patch core/modules/taxonomy/src/TermForm.php...
Hunk #1 succeeded at 50 (offset 2 lines).
Hunk #2 succeeded at 65 (offset 2 lines).
Checking patch core/modules/taxonomy/src/Tests/TermTest.php...
error: core/modules/taxonomy/src/Tests/TermTest.php: No such file or directory
Checking patch core/modules/taxonomy/taxonomy.module...
error: while searching for:
$hierarchy = VocabularyInterface::HIERARCHY_SINGLE;
}
}
if ($hierarchy != $vocabulary->getHierarchy()) {
$vocabulary->setHierarchy($hierarchy);
$vocabulary->save();

error: patch failed: core/modules/taxonomy/taxonomy.module:193
error: core/modules/taxonomy/taxonomy.module: patch does not apply
Checking patch core/modules/taxonomy/tests/src/Kernel/TermKernelTest.php...
Hunk #1 succeeded at 109 (offset -4 lines).
Hunk #2 succeeded at 123 (offset -4 lines).

Thanks in advance!

super_romeo’s picture

Version: 8.9.x-dev » 9.2.x-dev
Status: Needs review » Needs work

Same for D9.2

$tree = $this->storageController->loadTree($taxonomy_vocabulary->id(), 0, NULL, TRUE);

Loads all entities of vocabulary (no pager value used), but why?

sweetchuck’s picture

Issue tags: +Needs reroll

Patch #28 can not be applied to core 9.2.7 and to the latest 9.2.x (1a5cc0d9ab68d8c7b22143c4642a691365171cd1 2021-10-25 23:15:55 +0100). Which makes sense because patch #28 was created for 8.5.x.

yogeshmpawar’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
StatusFileSize
new9.23 KB
sweetchuck’s picture

Thanks for the reroll.
I think the unset() not going to solve this memory problem. PHP's garbage collector does a better job than we can do manually. I think the unset also pointless in tests files where there is only a few amount of terms (less than 10).

I have a vocabulary with >34000 terms without hierarchy and I can't view the /admin/structure/taxonomy/manage/my_voc_01/overview page with memory limit 512MB.

The attached patch solves the problem, but I am not sure if this is the right direction.
However the original code also uses expressions like these: $term->depth and $term->parents

Status: Needs review » Needs work

The last submitted patch, 39: 2183565-39-0902-term-overview-memory-limit.patch, failed testing. View results

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.1.10 (June 4, 2021) and Drupal 9.2.10 (November 24, 2021) were the last bugfix releases of those minor version series. Drupal 9 bug reports should be targeted for the 9.3.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

grndlvl’s picture

Status: Needs work » Needs review

Not sure why the "PHP 7.3 & MySQL 5.7" failed. I retested using PHP 7.4and it passed fine.

I agree with Sweetchuck I don't see unset adding much benefit to the patch. This appears to lower the load in manual testing.

g_miric’s picture

Status: Needs review » Needs work

This creates other sort issues:

1. Create a vocabulary that has parent/child terms.
- If you go to the overview page you will not be able to see the tree just the flat list
- If you try to update the tree and save it (on the overview page), you will get a bunch of notices.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

grndlvl’s picture

Status: Needs work » Needs review
StatusFileSize
new1.31 KB
new1.54 KB

Re-roll to address issues in 43

cesarmiquel’s picture

I can confirm the patch #45 solves the issue. I just applied this patch on our dev instance of a large newspaper site and it works great! Kudos! Not only does the page load (right now we are not able to look at the taxonomy overview page because we used to run out of memory) but it loads and works super fast. This was tested on the taxonomy overview for a large site with over 56000 tags. +1 on this.

cesarmiquel’s picture

Tested the patch #45 o newer Drupal (the previous test was done on a Drupal 8.6.18. Surprisingly the patch applied cleanly!). I tested now on a Drupal 9.3.12 and also worked fine. It was running out of memory with PHP 7.4 and 256M of memory limit. The taxonomy had around 39000 tags. With the latest patch it worked perfectly. Also tested on much smaller taxonomies with terms of depth 2 and it worked fine. Probably a someone should test on something with deeper taxonomies. The ones I have are large but have depth = 1. Still looking very promising in my opinion. Great work everybody!

super_romeo’s picture

Patch #45 does not apply on D9.4.x.

cesarmiquel’s picture

StatusFileSize
new1.3 KB

Hey @super_romeo! I created a patch manually since its just a couple of lines. Please test it. It should apply cleanly to D9.4.x.

g_miric’s picture

Status: Needs review » Reviewed & tested by the community

Works fine with D9.4.x
Also the issue reported in #43 is fixed too.

betoaveiga’s picture

This is not a patch to reduce memory consumption, but it is closely related and may fix the issue for some.
For this patch to work, you need to set the memory per vocabulary on your settings.php file.

$config['taxonomy_load_tree_memory']['big_vocabulary'] = '750M';
$config['taxonomy_load_tree_memory']['big_vocabulary_2'] = '1250M';`
catch’s picture

Title: Reduce memory consumption of the functions using taxonomy loadTree (includes the taxonomy overview and forums) » Avoid loading all terms on the taxonomy overview form
Status: Reviewed & tested by the community » Needs work

Retitling since this is changing the taxonomy overview controller, not the ::loadTree() method itself.

The patch in #49 looks good, however we should add an inline comment explaining that we're avoiding loading all terms, and that the ones shown on the current page get loaded later.

tinny’s picture

This patch doesn't work for me with 4000 child terms on a single parent (502 error).

sivaji_ganesh_jojodae’s picture

Version: 9.4.x-dev » 10.1.x-dev
Status: Needs work » Needs review
StatusFileSize
new1.3 KB

The issue still seems to exist in D10 as well. Attached is patch #49 from branch 10.1.x.

Status: Needs review » Needs work

The last submitted patch, 54: 2183565_54.patch, failed testing. View results

drugan’s picture

Status: Needs work » Needs review
StatusFileSize
new8.57 KB

This is an extension of the #73 patch which fixes memory leak error not only on the terms overview page but also on a term edit page.

It allows to reduce number of loaded terms by reflecting pager "page" parameter into the sql query range().

Pager still works the same with one exception for the "Last" button which moves by 10 pages forward while clicking on it.

If you like me have an insane number of terms / pager pages then the "page" parameter in the browser address bar can be edited directly to get you to the very end of the pager.

Status: Needs review » Needs work

The last submitted patch, 56: 2183565_56.patch, failed testing. View results

grevil’s picture

We just ran into this issue migrating a Drupal 7 website with a large taxonomy to Drupal 10. So this is still important to fix, as it breaks large sites. Reading the comments above, I think we still need a plan on how to fix this deep underlying issue, before we invest more time in code here?

At least with pagination, this should not break:
drush config:set taxonomy.settings terms_per_page_admin 100

But a written above, changing the page size has no effect on this error.

Is any of the above approaches at least an improvement from the Core maintainers perspective? Who could have a look?

grevil’s picture

Issue summary: View changes
cesarmiquel’s picture

Not sure why this issue derailed. The patch in #45 worked for me in several tests/sites with large taxonomies and had the blessing by Catch. All that remained was to add a comment. In all honesty I didn't understand exactly what the comment was but I believe once that comment is added this was RTBC.

There is also a version for 10.x. in #54. I recommend you test that version and leave a comment if it works.

I don't recommend the solutions that try to increase the memory size as suggested as they will eventually break. The proposed solution worked for me on two large sites I tested.

hongqing’s picture

Patch #54 works well with the latest Drupal 10.0.9. Thanks.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

plopesc made their first commit to this issue’s fork.

plopesc’s picture

Status: Needs work » Needs review
StatusFileSize
new1.44 KB

Created MR and patch against 11.x based on #49and including suggestions by catch in #52

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

As a bug will need a test case showing the issue please

Thanks!

plopesc’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new5.57 KB
new2.61 KB
new5.7 KB

Thank you for the heads-up smustgrave.

Attaching new version of the patch including tests and improving the load terms logic for the submit handelr as well.

The last submitted patch, 67: 2183565-67-test-only.patch, failed testing. View results

plopesc’s picture

StatusFileSize
new817 bytes
new5.71 KB

New patch version fixing typo.

smustgrave credited bbrala.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new84.9 KB
new82.78 KB

Giving credit to bbrala for pointing me out to a good way for doing a profile using ddev and phpstorm.

Using devel generate created 1000 terms and went to the taxonomy overview page of my vocabulary

Attaching before/after

Before
before

After
after

So seeing a slight faster render.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 69: 2183565-69.patch, failed testing. View results

golubovicm’s picture

Patch 2183565-69.patch is not working for me. Still getting:
Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 4096 bytes) in /var/www/web/modules/contrib/taxonomy_menu/src/TaxonomyMenuHelper.php on line 104
Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 24576 bytes) in Unknown on line 0
Could it be because I'm also using Taxonomy menu module?
Update: yes, when disabling TM it works well.

amavi’s picture

Hi :)

Since 9.4 all my taxo more big 2000 is block, I put this patch and works now:
https://www.drupal.org/project/drupal/issues/3327118#comment-15252799

But, all big content is still block: Error Vanish cache server.

I can not make Sitemap too for big content, Drupal 9.4 break all web site... :(

poker10’s picture

Status: Needs work » Reviewed & tested by the community

Moving back to RTBC (per #71) - there was a random failure.

Re #73: I have tested the patch #69 and is working for us on a test site. Taxonomy with 2000+ terms: without the patch - out of memory, after patch is applied: the page loaded without any problems.

catch’s picture

Status: Reviewed & tested by the community » Needs review
+++ b/core/modules/taxonomy/src/Form/OverviewTerms.php
@@ -520,9 +527,10 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
 
     // Build a list of all terms that need to be updated on previous pages.
     $weight = 0;
-    $term = $tree[0];
-    while ($term->id() != $form['#first_tid']) {
-      if ($term->parents[0] == 0 && $term->getWeight() != $weight) {
+    $raw_term = $tree[0];
+    while ($raw_term->tid != $form['#first_tid']) {
+      if ($raw_term->parents[0] == 0 && $raw_term->weight != $weight) {
+        $term = $this->storageController->load($raw_term->tid);
         $term->setWeight($weight);

I think it would be worth building the list of term IDs in advance, and then using ::loadMultiple() here.

Similar in the next hunk of the MR too.

Otherwise looks good though.

plopesc’s picture

StatusFileSize
new6.25 KB

Good point.

Added new version of the patch storing in an index all the entities and their weight to load all of them in a single call afterwards.

plopesc’s picture

StatusFileSize
new1.85 KB

Add missing interdiff.

poker10’s picture

There are no test failures in the patch #77, but I am curious how is the while loop supposed to work now, as the $raw_term variable is not being changed at all?

@@ -526,14 +533,13 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
     $weight = 0;
+    $raw_term = $tree[0];
+    $term_weights = [];
+    while ($raw_term->tid != $form['#first_tid']) {
+      if ($raw_term->parents[0] == 0 && $raw_term->weight != $weight) {
+        $term_weights[$raw_term->tid] = $weight;
       }
       $weight++;
-      $term = $tree[$weight];
     }
plopesc’s picture

StatusFileSize
new2.25 KB
new7.08 KB

Good catch!

That part of the code is only affected if we are loading a page that's not the first and root terms weight is not starting by 0.

Added extra tests to cover that case.

While working on this, I found out there are no tests to ensure that overview terms form submit callback behaves as expected. Should they be added here or maybe better to open a follow up issue for that?

Thanks!

plopesc’s picture

StatusFileSize
new7.08 KB

Fix typo in comment.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Failed asserting that actual size 11 matches expected size 5.

Ran new tests to make sure they failed and clearly they did.

Seems feedback has been updated.

alexpott’s picture

Status: Reviewed & tested by the community » Needs review
StatusFileSize
new6.22 KB
new9.47 KB
  1. +++ b/core/modules/taxonomy/tests/modules/taxonomy_test/taxonomy_test.module
    @@ -46,3 +46,16 @@ function taxonomy_test_form_taxonomy_term_form_alter(&$form, FormStateInterface
    +  $value = \Drupal::state()->get(__FUNCTION__);
    +  if (isset($value)) {
    +    foreach ($entities as $entity) {
    +      $value[$entity->id()] = $entity;
    +    }
    +    \Drupal::state()->set(__FUNCTION__, $value);
    +  }
    

    I don't think we should be save the term objects to state.

    I think this could be

    $value = \Drupal::state()->get(__FUNCTION__);
    // Only record loaded terms is the test has set this to an empty array.
    if (is_array($value)) {
      $value = array_merge($value, array_keys($entities));
      
      \Drupal::state()->set(__FUNCTION__, array_unique($value));
    }
    
  2. +++ b/core/modules/taxonomy/tests/src/Functional/TaxonomyTermPagerTest.php
    @@ -73,4 +73,48 @@ public function testTaxonomyTermOverviewPager() {
    +  public function testTaxonomyTermOverviewTermLoad() {
    +    // Set limit to 3 terms per page.
    +    $this->config('taxonomy.settings')
    +      ->set('terms_per_page_admin', '3')
    +      ->save();
    +
    +    $state = $this->container->get('state');
    +    $state->set('taxonomy_test_taxonomy_term_load', []);
    

    The first state->set() is not required.

I've attached a patch that does the above. It also adjusted things so that we use loadMultiple() in the buildForm - this is very similar to what @catch requested in for the submit in #76

alexpott’s picture

StatusFileSize
new1.27 KB
new9.33 KB

Ah actually we already have a list of tids to load :) no need to built yet-another-list-of-tids...

alexpott’s picture

Updated the MR with all the changes from the issue after the last commit to it. Hiding all the files.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems you addressed your own feedback :)

poker10’s picture

This is not a deep performance benchmark, but I can confirm that the MR !4110 is working on few our sites with thousands of taxonomy terms. Before changes from MR were applied, there was an OOM error on the taxonomy overview page (admin/structure/taxonomy/manage/vocabulary/overview). After applying the changes, the page is working again.

I think a more detailed benchmark could help for this to be committed (for example memory usage on that page before/after, etc..), as there is a "needs profiling" tag, but otherwise I think this works pretty well.

catch’s picture

I think it should be possible to write a performance test for this using #3352851: Allow assertions on the number of database queries run during tests - I wouldn't block it on that, this issue has existing for many years longer than that capability, but I'll open a spin-off and try to do a before/after comparison if I can and then we can commit the test coverage in its own issue.

  • catch committed 7c1e3a1b on 11.x
    Issue #2183565 by plopesc, alexpott, jcnventura, DuaelFr, melvinlouwerse...

  • catch committed 8d2f88bf on 10.2.x
    Issue #2183565 by plopesc, alexpott, jcnventura, DuaelFr, melvinlouwerse...
catch’s picture

Version: 11.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: -needs profiling

I opened #3410092: Performance tests for the taxonomy overview page and wrote the test, then merged the branch here in and ran the test again - and as I was writing the test coverage, realised we multiple load taxonomy terms in taxonomy get tree, which will also multiple get the cache items, and multiple set the cache items too. Given that, we can't detect the performance improvement here with PerformanceTestBase and the test run after the merged branch confirmed that - we're exchanging a massive database query for a smaller one, and nothing runs per-term.

Bit of a shame that the tests can't detect it, but also if it had detected a difference, there'd be something wrong with multiple loading, it the test would have least have probably detected the multiple load regression fixed in #84 though so worth adding anyway

Committed/pushed to 11x and cherry-picked to 10.2.x, thanks!

giorgio79’s picture

PS this issue impacts not just the taxonomy overview page, but taxonomy edit, batch update etc etc. (using Drupal 10.2.0 and php 8.1)
I have a large location based taxonomy with continents -> countries -> cities and unable to make edits, bulk update url aliases due to 500 timeout errors as Drupal is trying to load all terms...

Status: Fixed » Closed (fixed)

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

robgreeniowa’s picture