wppaste
WordPress

Text_Diff_Engine_shell::diff( array $from_lines, array $to_lines ): array

Source
wp-includes/Text/Diff/Engine/shell.php:34
Returns the array of differences.

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

Reads or writes the filesystem via fopen().

Scaling
Scales with input

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

Instructions
71–105

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What it touches

  • filesystemfilesystem accessfopen()called directly
  • regexregular expression over the whole inputpreg_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.

WhenInstructionsCalls it makes
$diff === null71array_walk(), array_walk(), ::Text_Diff(), tempnam(), tempnam(), fopen(), fwrite(), fclose(), fopen(), fwrite(), fclose(), shell_exec(), unlink(), unlink()
$diff !== null && empty($from_lines)80–81array_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–105array_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

PHPCompiledExecutedBranchesNotes
8.6-dev22571–10514
8.522571–10514
8.422571–105146 fewer instructions than PHP 8.3
8.323177–11114
8.223177–111141 more instruction than PHP 8.1
8.123077–11114
7.423077–11114

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

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.

  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.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.