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:395
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_matchesarrayAssociative array of original matches. Index == row number of$orig, value == corresponding row number of that same line in$finalor 'x' if there is no corresponding row (indicating it is a deleted line).$final_matchesarrayAssociative array of final matches. Index == row number of$final, value == corresponding row number of that same line in$origor 'x' if there is no corresponding row (indicating it is a new line).$orig_rowsarrayAssociative array of interleaved rows of$origwith 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_rowsarrayAssociative array of interleaved rows of$finalwith 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
- Scaling
- Scales with input
- Instructions
- 44–51
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 175.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
!$diff_count | 44–51 | array_keys(), asort(), ksort(), ksort(), array_keys(), array_keys() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 175 | 44–51 | 21 | |
| 8.5 | 175 | 44–51 | 21 | |
| 8.4 | 175 | 44–51 | 21 | |
| 8.3 | 175 | 44–51 | 21 | |
| 8.2 | 175 | 44–51 | 21 | 2 more instructions than PHP 8.1 |
| 8.1 | 173 | 44–58 | 21 | |
| 7.4 | 173 | 44–58 | 21 |
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
- WP_Text_Diff_Renderer_Table::compute_string_distance()Computes a number that is intended to reflect the "distance" between two strings.
Used by · 1
- WP_Text_Diff_Renderer_Table::_changed()Process changed lines to do word-by-word diffs for extra highlighting.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 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.