Text_Diff_Engine_shell::diff( array $from_lines, array $to_lines ): array
- Source
wp-includes/Text/Diff/Engine/shell.php:34
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_linesarray- lines of text from old file
$to_linesarray- lines of text from new file
Return value
array- all changes made (array with Text_DiffOp* objects)
Performance profile
How much work a call to Text_Diff_Engine_shell::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
- Heavy
- Scaling
- Scales with input
- Instructions
- 71–105
- Plugin surface
- None
- Called by
- 0
Reads or writes the filesystem via fopen().
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 225.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- filesystemfilesystem access
fopen()called directly - regexregular expression over the whole input
preg_match_all()called directly
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost Text_Diff_Engine_shell::diff() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$diff === null | 71 | array_walk(), array_walk(), ::Text_Diff(), tempnam(), tempnam(), fopen(), fwrite(), fclose(), fopen(), fwrite(), fclose(), shell_exec(), unlink(), unlink() |
$diff !== null && empty($from_lines) | 80–81 | array_walk(), array_walk(), ::Text_Diff(), tempnam(), tempnam(), fopen(), fwrite(), fclose(), fopen(), fwrite(), fclose(), shell_exec(), unlink(), unlink(), preg_match_all() |
$diff !== null && !empty($from_lines) | 104–105 | array_walk(), array_walk(), ::Text_Diff(), tempnam(), tempnam(), fopen(), fwrite(), fclose(), fopen(), fwrite(), fclose(), shell_exec(), unlink(), unlink(), preg_match_all(), ->_getLines(), ->_getLines(), array_push() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 225 | 71–105 | 14 | |
| 8.5 | 225 | 71–105 | 14 | |
| 8.4 | 225 | 71–105 | 14 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 231 | 77–111 | 14 | |
| 8.2 | 231 | 77–111 | 14 | 1 more instruction than PHP 8.1 |
| 8.1 | 230 | 77–111 | 14 | |
| 7.4 | 230 | 77–111 | 14 |
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::_getTempDir()Determines the location of the system temporary directory.
- Text_Diff_Op_copy::__construct()PHP5 constructor.
- Text_Diff_Engine_shell::_getLines()Get lines from either the old or new text
- Text_Diff_Op_delete::__construct()PHP5 constructor.
- Text_Diff_Op_change::__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')); $temp_dir = Text_Diff::_getTempDir(); // Execute gnu diff or similar to get a standard diff file. $from_file = tempnam($temp_dir, 'Text_Diff'); $to_file = tempnam($temp_dir, 'Text_Diff'); $fp = fopen($from_file, 'w'); fwrite($fp, implode("\n", $from_lines)); fclose($fp); $fp = fopen($to_file, 'w'); fwrite($fp, implode("\n", $to_lines)); fclose($fp); $diff = shell_exec($this->_diffCommand . ' ' . $from_file . ' ' . $to_file); unlink($from_file); unlink($to_file); if (is_null($diff)) { // No changes were made return array(new Text_Diff_Op_copy($from_lines)); } $from_line_no = 1; $to_line_no = 1; $edits = array(); // Get changed lines by parsing something like: // 0a1,2 // 1,2c4,6 // 1,5d6 preg_match_all('#^(\d+)(?:,(\d+))?([adc])(\d+)(?:,(\d+))?$#m', $diff, $matches, PREG_SET_ORDER); foreach ($matches as $match) { if (!isset($match[5])) { // This paren is not set every time (see regex). $match[5] = false; } if ($match[3] == 'a') { $from_line_no--; } if ($match[3] == 'd') { $to_line_no--; } if ($from_line_no < $match[1] || $to_line_no < $match[4]) { // copied lines assert($match[1] - $from_line_no == $match[4] - $to_line_no); array_push($edits, new Text_Diff_Op_copy( $this->_getLines($from_lines, $from_line_no, $match[1] - 1), $this->_getLines($to_lines, $to_line_no, $match[4] - 1))); } switch ($match[3]) { case 'd': // deleted lines array_push($edits, new Text_Diff_Op_delete( $this->_getLines($from_lines, $from_line_no, $match[2]))); $to_line_no++; break; case 'c': // changed lines array_push($edits, new Text_Diff_Op_change( $this->_getLines($from_lines, $from_line_no, $match[2]), $this->_getLines($to_lines, $to_line_no, $match[5]))); break; case 'a': // added lines array_push($edits, new Text_Diff_Op_add(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/shell.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.