#58327 closed enhancement (duplicate)
Remove unneeded sanitize term in `populate_terms` method
| Reported by: |
|
Owned by: |
|
|---|---|---|---|
| Milestone: | Priority: | normal | |
| Severity: | normal | Version: | 6.0 |
| Component: | Taxonomy | Keywords: | has-patch |
| Focuses: | performance | Cc: |
Description
In [52836] ( #37189 ) the WP_Term_Query class was converted to just get ids and then convert those ids into WP_Term objects using get_term. But in the case where fields all_with_object_id is passed, this results in lots of calls to sanitize_term. These calls can be extensive and effect performance. all_with_object_id expects an object as a result, but does not necessarily need to be sanitized.
Change History (10)
This ticket was mentioned in PR #4460 on WordPress/wordpress-develop by @spacedmonkey.
3 years ago
#1
- Keywords has-patch added
This ticket was mentioned in Slack in #core-performance by spacedmonkey. View the logs.
2 years ago
@spacedmonkey commented on PR #4460:
2 years ago
#5
@felixarntz @joemcgill
Basically, get_term has some overhead attached to it. It has filters and after the filtering, it rerun the sanitisation. When all_with_object_id is requsted ( in update_object_term_cache ), you only need the raw object. update_object_term_cache only needs to the term ids, so those filters and sanitisation are not needed. The sanitisation is run in WP_Term::get_instance() meaning no break in B/C. But as update_object_term_cache run on every WP_Query, which can be run 10+ times on many themes, ( block template parts, menus etc ), these calls to get_term can really really mount up, resulting in thousands of calls to sanative_term and apply_filters.
This change is a side effect of https://github.com/WordPress/wordpress-develop/commit/4a9f5fe3be47914b3f2b30aaf863761335cf1ad2 , resulting in the unexpected overhead.
This ticket was mentioned in Slack in #core-performance by mukeshpanchal27. View the logs.
2 years ago
@peterwilsoncc commented on PR #4460:
2 years ago
#7
@spacedmonkey I think get_post had the same overhead and was solved by bypassing the filtering when possible:
Would this work in WP_Term::get_instance instead of changing WP Query? The terms are cached with raw sanitization so this should save a bunch of work for sites using get_term() too.
-
src/wp-includes/class-wp-term.php
diff --git a/src/wp-includes/class-wp-term.php b/src/wp-includes/class-wp-term.php index 0f5631353e..f98c132017 100644
a b final class WP_Term { 182 182 } 183 183 184 184 $term_obj = new WP_Term( $_term ); 185 $term_obj->filter( $term_obj->filter ); 185 if ( empty( $term_obj->filter ) || 'raw' !== $term_obj->filter ) { 186 $term_obj->filter( $term_obj->filter ); 187 } 186 188 187 189 return $term_obj; 188 190 }
@spacedmonkey commented on PR #4460:
2 years ago
#8
@peterwilsoncc I have created a PR based on your idea. https://github.com/WordPress/wordpress-develop/pull/5260. This solution multiple problems, of double sanitation. Please take a look.
Trac ticket: https://core.trac.wordpress.org/ticket/58327