Text_Diff_Engine_xdiff::diff( $from_lines, $to_lines )
- Source
wp-includes/Text/Diff/Engine/xdiff.php:20
Compatibility
- WordPress
- core
- 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
$from_lines$to_lines
Performance profile
How much work a call to Text_Diff_Engine_xdiff::diff() 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
- 28–29
- Plugin surface
- None
- Called by
- 0
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 65.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
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 Text_Diff_Engine_xdiff::diff() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 28–29 | array_walk(), array_walk(), xdiff_string_diff(), explode() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 65 | 28–29 | 7 | |
| 8.5 | 65 | 28–29 | 7 | |
| 8.4 | 65 | 28–29 | 7 | 17 fewer instructions than PHP 8.3 |
| 8.3 | 82 | 36–37 | 7 | |
| 8.2 | 82 | 36–37 | 7 | 1 more instruction than PHP 8.1 |
| 8.1 | 81 | 36–37 | 7 | |
| 7.4 | 81 | 36–37 | 7 |
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 · 3
- Text_Diff_Op_copy::__construct()PHP5 constructor.
- Text_Diff_Op_add::__construct()PHP5 constructor.
- Text_Diff_Op_delete::__construct()PHP5 constructor.
Source code
function diff($from_lines, $to_lines) { array_walk($from_lines, array('Text_Diff', 'trimNewlines')); array_walk($to_lines, array('Text_Diff', 'trimNewlines')); /* Convert the two input arrays into strings for xdiff processing. */ $from_string = implode("\n", $from_lines); $to_string = implode("\n", $to_lines); /* Diff the two strings and convert the result to an array. */ $diff = xdiff_string_diff($from_string, $to_string, count($to_lines)); $diff = explode("\n", $diff); /* Walk through the diff one line at a time. We build the $edits * array of diff operations by reading the first character of the * xdiff output (which is in the "unified diff" format). * * Note that we don't have enough information to detect "changed" * lines using this approach, so we can't add Text_Diff_Op_changed * instances to the $edits array. The result is still perfectly * valid, albeit a little less descriptive and efficient. */ $edits = array(); foreach ($diff as $line) { if (!strlen($line)) { continue; } switch ($line[0]) { case ' ': $edits[] = new Text_Diff_Op_copy(array(substr($line, 1))); break; case '+': $edits[] = new Text_Diff_Op_add(array(substr($line, 1))); break; case '-': $edits[] = new Text_Diff_Op_delete(array(substr($line, 1))); break; } } return $edits; }Changelog
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 7.1.0 tag, from
src/wp-includes/Text/Diff/Engine/xdiff.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.