wppaste
WordPress

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
28–29

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

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

WhenInstructionsCalls it makes
always28–29array_walk(), array_walk(), xdiff_string_diff(), explode()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev6528–297
8.56528–297
8.46528–29717 fewer instructions than PHP 8.3
8.38236–377
8.28236–3771 more instruction than PHP 8.1
8.18136–377
7.48136–377

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

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.

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