Text_Diff_Engine_native::diff( $from_lines, $to_lines )
- Source
wp-includes/Text/Diff/Engine/native.php:41
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_native::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
- 90–103
- 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 238.
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_native::diff() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$xi && !$yi | 90–103 | array_walk(), array_walk(), ->_compareseq(), ->_shiftBoundaries(), ->_shiftBoundaries() |
This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 238 | 90–103 | 30 | |
| 8.5 | 238 | 90–103 | 30 | |
| 8.4 | 238 | 90–103 | 30 | |
| 8.3 | 238 | 90–103 | 30 | |
| 8.2 | 238 | 90–103 | 30 | |
| 8.1 | 238 | 90–103 | 30 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 240 | 92–105 | 30 |
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
- Text_Diff_Engine_native::_compareseq()Finds LCS of two sequences.
- Text_Diff_Engine_native::_shiftBoundaries()Adjusts inserts/deletes of identical lines to join changes as much as possible.
- Text_Diff_Op_copy::__construct()PHP5 constructor.
- Text_Diff_Op_change::__construct()PHP5 constructor.
- Text_Diff_Op_delete::__construct()PHP5 constructor.
- Text_Diff_Op_add::__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')); $n_from = count($from_lines); $n_to = count($to_lines); $this->xchanged = $this->ychanged = array(); $this->xv = $this->yv = array(); $this->xind = $this->yind = array(); unset($this->seq); unset($this->in_seq); unset($this->lcs); // Skip leading common lines. for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) { if ($from_lines[$skip] !== $to_lines[$skip]) { break; } $this->xchanged[$skip] = $this->ychanged[$skip] = false; } // Skip trailing common lines. $xi = $n_from; $yi = $n_to; for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) { if ($from_lines[$xi] !== $to_lines[$yi]) { break; } $this->xchanged[$xi] = $this->ychanged[$yi] = false; } // Ignore lines which do not exist in both files. for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { $xhash[$from_lines[$xi]] = 1; } for ($yi = $skip; $yi < $n_to - $endskip; $yi++) { $line = $to_lines[$yi]; if (($this->ychanged[$yi] = empty($xhash[$line]))) { continue; } $yhash[$line] = 1; $this->yv[] = $line; $this->yind[] = $yi; } for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { $line = $from_lines[$xi]; if (($this->xchanged[$xi] = empty($yhash[$line]))) { continue; } $this->xv[] = $line; $this->xind[] = $xi; } // Find the LCS. $this->_compareseq(0, count($this->xv), 0, count($this->yv)); // Merge edits when possible. $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged); $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged); // Compute the edit operations. $edits = array(); $xi = $yi = 0; while ($xi < $n_from || $yi < $n_to) { assert($yi < $n_to || $this->xchanged[$xi]); assert($xi < $n_from || $this->ychanged[$yi]); // Skip matching "snake". $copy = array(); while ($xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi]) { $copy[] = $from_lines[$xi++]; ++$yi; } if ($copy) { $edits[] = new Text_Diff_Op_copy($copy); } // Find deletes & adds.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/native.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.