wppaste
WordPress

WP_Text_Diff_Renderer_Table::interleave_changed_lines( array $orig, array $final ): array

Since
2.6.0
Source
wp-includes/class-wp-text-diff-renderer-table.php:400
Takes changed blocks and matches which rows in orig turned into which rows in final.

Compatibility

WordPress
since 2.6.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.

Parameters

$origarray
Lines of the original version of the text.
$finalarray
Lines of the final version of the text.

Return value

array
Array containing results of comparing the original text to the final text.
  • $orig_matchesarray

    Associative array of original matches. Index == row number of $orig, value == corresponding row number of that same line in $final or 'x' if there is no corresponding row (indicating it is a deleted line).
  • $final_matchesarray

    Associative array of final matches. Index == row number of $final, value == corresponding row number of that same line in $orig or 'x' if there is no corresponding row (indicating it is a new line).
  • $orig_rowsarray

    Associative array of interleaved rows of $orig with blanks to keep matches aligned with side-by-side diff of $final. A value >= 0 corresponds to index of $orig. Value < 0 indicates a blank row.
  • $final_rowsarray

    Associative array of interleaved rows of $final with blanks to keep matches aligned with side-by-side diff of $orig. A value >= 0 corresponds to index of $final. Value < 0 indicates a blank row.

Performance profile

How much work a call to WP_Text_Diff_Renderer_Table::interleave_changed_lines() 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
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
44–51

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 1 distinct outcome

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

WhenInstructionsCalls it makes
!$diff_count44–51array_keys(), asort(), ksort(), ksort(), array_keys(), array_keys()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17544–5121
8.517544–5121
8.417544–5121
8.317544–5121
8.217544–51212 more instructions than PHP 8.1
8.117344–5821
7.417344–5821

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 · 1

Used by · 1

Source code

	public function interleave_changed_lines( $orig, $final ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.finalFound 		// Contains all pairwise string comparisons. Keys are such that this need only be a one dimensional array.		$matches = array();		foreach ( array_keys( $orig ) as $o ) {			foreach ( array_keys( $final ) as $f ) {				$matches[ "$o,$f" ] = $this->compute_string_distance( $orig[ $o ], $final[ $f ] );			}		}		asort( $matches ); // Order by string distance. 		$orig_matches  = array();		$final_matches = array(); 		foreach ( $matches as $keys => $difference ) {			list($o, $f) = explode( ',', $keys );			$o           = (int) $o;			$f           = (int) $f; 			// Already have better matches for these guys.			if ( isset( $orig_matches[ $o ] ) && isset( $final_matches[ $f ] ) ) {				continue;			} 			// First match for these guys. Must be best match.			if ( ! isset( $orig_matches[ $o ] ) && ! isset( $final_matches[ $f ] ) ) {				$orig_matches[ $o ]  = $f;				$final_matches[ $f ] = $o;				continue;			} 			// Best match of this final is already taken? Must mean this final is a new row.			if ( isset( $orig_matches[ $o ] ) ) {				$final_matches[ $f ] = 'x';			} elseif ( isset( $final_matches[ $f ] ) ) {				// Best match of this orig is already taken? Must mean this orig is a deleted row.				$orig_matches[ $o ] = 'x';			}		} 		// We read the text in this order.		ksort( $orig_matches );		ksort( $final_matches ); 		// Stores rows and blanks for each column.		$orig_rows      = array_keys( $orig_matches );		$orig_rows_copy = $orig_rows;		$final_rows     = array_keys( $final_matches ); 		/*		 * Interleaves rows with blanks to keep matches aligned.		 * We may end up with some extraneous blank rows, but we'll just ignore them later.		 */		foreach ( $orig_rows_copy as $orig_row ) {			$final_pos = array_search( $orig_matches[ $orig_row ], $final_rows, true );			$orig_pos  = (int) array_search( $orig_row, $orig_rows, true ); 			if ( false === $final_pos ) { // This orig is paired with a blank final.				array_splice( $final_rows, $orig_pos, 0, -1 );			} elseif ( $final_pos < $orig_pos ) { // This orig's match is up a ways. Pad final with blank rows.				$diff_array = range( -1, $final_pos - $orig_pos );				array_splice( $final_rows, $orig_pos, 0, $diff_array );			} elseif ( $final_pos > $orig_pos ) { // This orig's match is down a ways. Pad orig with blank rows.				$diff_array = range( -1, $orig_pos - $final_pos );				array_splice( $orig_rows, $orig_pos, 0, $diff_array );			}		} 		// Pad the ends with blank rows if the columns aren't the same length.		$diff_count = count( $orig_rows ) - count( $final_rows );		if ( $diff_count < 0 ) {			while ( $diff_count < 0 ) {				array_push( $orig_rows, $diff_count++ );			}		} elseif ( $diff_count > 0 ) {			$diff_count = -1 * $diff_count;			while ( $diff_count < 0 ) {				array_push( $final_rows, $diff_count++ );			}		}

Changelog

Introduced in 2.6.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/class-wp-text-diff-renderer-table.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.