wppaste
WordPress

_wp_batch_split_terms()

Since
4.3.0
Source
wp-includes/taxonomy.php:4405
Splits a batch of shared taxonomy terms.

Compatibility

WordPress
since 4.3.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to _wp_batch_split_terms() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via ->query().

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
33–99

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 159.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • optionoption read or writeget_option()called directly
  • sqldatabase query->query()called directly
  • hookthird-party callbacksapply_filters()one call below _wp_batch_split_terms()
  • cacheobject cachewp_cache_get()one call below _wp_batch_split_terms()
  • serializeserialisationmaybe_unserialize()one call below _wp_batch_split_terms()
  • querycontent queryget_terms()one call below _wp_batch_split_terms()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 6 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_batch_split_terms() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always33time(), ->prepare(), ->query(), get_option(), time(), wp_schedule_single_event()
$lock_result39time(), ->prepare(), ->query(), get_option(), time(), time(), wp_schedule_single_event()
always44time(), ->prepare(), ->query(), time(), update_option(), ->get_results(), update_option(), delete_option()
!$lock_result55time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_results(), update_option(), delete_option()
always85–88time(), ->prepare(), ->query(), time(), update_option(), ->get_results(), time(), wp_schedule_single_event(), array_keys(), ->get_results(), get_option(), array_keys(), update_option(), delete_option()
!$lock_result96–99time(), ->prepare(), ->query(), get_option(), time(), time(), update_option(), ->get_results(), time(), wp_schedule_single_event(), array_keys(), ->get_results(), get_option(), array_keys(), update_option(), delete_option()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev15933–9913
8.515933–9913
8.415933–99134 fewer instructions than PHP 8.3
8.316333–10313
8.216333–10313
8.116333–10313
7.416333–10313

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Source code

function _wp_batch_split_terms() {	global $wpdb; 	$lock_name = 'term_split.lock'; 	// Try to lock.	$lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'off') /* LOCK */", $lock_name, time() ) ); 	if ( ! $lock_result ) {		$lock_result = get_option( $lock_name ); 		// Bail if we were unable to create a lock, or if the existing lock is still valid.		if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {			wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );			return;		}	} 	// Update the lock, as by this point we've definitely got a lock, just need to fire the actions.	update_option( $lock_name, time() ); 	// Get a list of shared terms (those with more than one associated row in term_taxonomy).	$shared_terms = $wpdb->get_results(		"SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt		 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id		 GROUP BY t.term_id		 HAVING term_tt_count > 1		 LIMIT 10"	); 	// No more terms, we're done here.	if ( ! $shared_terms ) {		update_option( 'finished_splitting_shared_terms', true );		delete_option( $lock_name );		return;	} 	// Shared terms found? We'll need to run this script again.	wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' ); 	// Rekey shared term array for faster lookups.	$_shared_terms = array();	foreach ( $shared_terms as $shared_term ) {		$term_id                   = (int) $shared_term->term_id;		$_shared_terms[ $term_id ] = $shared_term;	}	$shared_terms = $_shared_terms; 	// Get term taxonomy data for all shared terms.	$shared_term_ids = implode( ',', array_keys( $shared_terms ) );	$shared_tts      = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" ); 	// Split term data recording is slow, so we do it just once, outside the loop.	$split_term_data    = get_option( '_split_terms', array() );	$skipped_first_term = array();	$taxonomies         = array();	foreach ( $shared_tts as $shared_tt ) {		$term_id = (int) $shared_tt->term_id; 		// Don't split the first tt belonging to a given term_id.		if ( ! isset( $skipped_first_term[ $term_id ] ) ) {			$skipped_first_term[ $term_id ] = 1;			continue;		} 		if ( ! isset( $split_term_data[ $term_id ] ) ) {			$split_term_data[ $term_id ] = array();		} 		// Keep track of taxonomies whose hierarchies need flushing.		if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {			$taxonomies[ $shared_tt->taxonomy ] = 1;		} 		// Split the term.		$split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );	} 	// Rebuild the cached hierarchy for each affected taxonomy.	foreach ( array_keys( $taxonomies ) as $tax ) {

Changelog

Introduced in 4.3.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.0.4 tag, from src/wp-includes/taxonomy.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.