Make WordPress Core

Opened 6 weeks ago

Last modified 6 weeks ago

#66242 new defect (bug)

Taxonomy: Repeated hierarchy decoding slows hierarchical term queries

Reported by: serhatsoylu06 Owned by:
Priority: normal Milestone: Awaiting Review
Component: Taxonomy Version:
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses: performance

Description

get_term_children() calls _get_term_hierarchy() at each recursive step. The hierarchy option may already be in the object cache, but get_option() still unserializes the complete map on every read. WP_Term_Query's hierarchical hide_empty pass repeats the traversal for each empty parent.

This can make ordinary category lists slow even when SQL is cheap. A local fixture with 8,000 terms in a balanced tree (up to four children per parent, empty parents and nonempty leaves) needed 12,182 hierarchy reads to build one list. Only two SQL queries were executed. The fixture has one published post assigned to the leaves of each taxonomy; it is a controlled fixture rather than a production-site measurement.

PHP 8.3.0, MariaDB 11.4, OPcache enabled in both versions. Medians of five alternating runs:

Operation Terms Before After
wp_list_categories() 1,000 0.116 s 0.030 s
wp_list_categories() 4,000 1.706 s 0.131 s
wp_list_categories() 8,000 6.909 s 0.227 s
WP_Query including descendants 8,000 1.269 s 0.152 s

The option cache was warmed before timing. Each measurement used a fresh PHP process, so the term-query result cache was cold. WordPress bootstrap was excluded from the timer. List HTML, returned terms and post IDs matched between versions in all 90 measurements.

To reproduce the path, create a sizeable hierarchical taxonomy with empty parents and posts assigned to its leaves, then time these calls in separate requests without a pre-existing term-query result cache:

$start = microtime( true );
$html = wp_list_categories( array(
    'taxonomy' => 'category',
    'echo' => false,
    'title_li' => '',
) );
$elapsed = microtime( true ) - $start;

The proposed change passes an already loaded hierarchy into the existing descendant traversal and reuses that hierarchy within the hide_empty pass, keyed by taxonomy. No persistent or request-global cache is introduced. A warm term-query result cache bypasses the affected hide_empty processing, so the same gain should not be expected on cache hits.

Eight regression tests cover ordering, invalid taxonomies, leaves, insertion/reparenting/deletion between calls, descendants excluded from the result set, multiple taxonomies and hierarchy read counts. All eight pass with the patch; the two read-count tests fail without it. The broader term, category and query/taxQuery groups complete 753 tests and 1,933 assertions with no failures. One pre-existing PHPUnit warning also appears on the original code; strict --fail-on-warning runs exit nonzero.

Compatibility point for review: option filters are called fewer times. A get_term callback that changes hierarchy relationships during the same hide_empty pass may observe the pass's existing hierarchy snapshot. Changes between separate calls are covered by tests.

Attachments (1)

taxonomy-hierarchy-performance.patch​ (9.3 KB ) - added by serhatsoylu06 6 weeks ago.
Reuse the taxonomy hierarchy during descendant traversal and hide_empty filtering. Includes eight regression tests.

Download all attachments as: .zip

Change History (7)

@serhatsoylu06
6 weeks ago

Reuse the taxonomy hierarchy during descendant traversal and hide_empty filtering. Includes eight regression tests.

#1 @serhatsoylu06
6 weeks ago

  • Keywords has-patch has-unit-tests added

This ticket was mentioned in ​PR #13987 on ​WordPress/wordpress-develop by ​@serhatsoylu06.


6 weeks ago
#2

Load the hierarchy once for descendant traversal and reuse it within the hierarchical hide_empty pass. Preserve descendant ordering and visibility checks, with regression coverage for hierarchy reads and term changes.

Trac ticket: https://core.trac.wordpress.org/ticket/66242

​@westonruter commented on ​PR #13987:


6 weeks ago
#3

I've corrected the reference to Core-66242

#4 @westonruter
6 weeks ago

@serhatsoylu06 FYI: No need to attach patch files when you've already opened a pull request. Pull requests are preferred over patches anyway, since they facilitate review and have automated checks run.

​@serhatsoylu06 commented on ​PR #13987:


6 weeks ago
#6

@sekalan Please include the AI Disclosure in the PR description:

​https://github.com/WordPress/wordpress-develop/blob/794e4798f6db09357da54f05f5be8cf961b77a52/.github/pull_request_template.md?plain=1#L22-L33

Thanks @westonruter, sorry for the confusion with the ticket reference. i started investigating this second issue using gpt, but hit my usage limit right before completing it, so i switched to gemini 3.5 flash to finish the task and submit the pr. that's why i mixed up the ticket number and was unsure about how to format the disclosure. i appreciate you fixing the reference! I've also updated the pr description with the ai disclosure.

Note: See TracTickets for help on using tickets.