wppaste
WordPress

Text_Diff_Engine_native::_shiftBoundaries( $lines, $changed, $other_changed )

Source
wp-includes/Text/Diff/Engine/native.php:340
Adjusts inserts/deletes of identical lines to join changes as much as possible.

Description

We do something when a run of changed lines include a line at one end and has an excluded, identical line at the other. We are free to choose which identical line is included. `compareseq' usually chooses the one at the beginning, but usually it is cleaner to consider the following identical line to be the "change".

This is extracted verbatim from analyze.c (GNU diffutils-2.7).

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

$lines
$changed
$other_changed

Performance profile

How much work a call to Text_Diff_Engine_native::_shiftBoundaries() 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
16–28

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost Text_Diff_Engine_native::_shiftBoundaries() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$i == false16–21none
$i == false23–28assert()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17016–2837
8.517016–2837
8.417016–2837
8.317016–2837
8.217016–28373 more instructions than PHP 8.1
8.116716–43376 fewer instructions than PHP 7.4
7.417318–10537

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.

Used by · 1

Source code

    function _shiftBoundaries($lines, &$changed, $other_changed)    {        $i = 0;        $j = 0;         assert(count($lines) == count($changed));        $len = count($lines);        $other_len = count($other_changed);         while (1) {            /* Scan forward to find the beginning of another run of             * changes. Also keep track of the corresponding point in the             * other file.             *             * Throughout this code, $i and $j are adjusted together so that             * the first $i elements of $changed and the first $j elements of             * $other_changed both contain the same number of zeros (unchanged             * lines).             *             * Furthermore, $j is always kept so that $j == $other_len or             * $other_changed[$j] == false. */            while ($j < $other_len && $other_changed[$j]) {                $j++;            }             while ($i < $len && ! $changed[$i]) {                assert($j < $other_len && ! $other_changed[$j]);                $i++; $j++;                while ($j < $other_len && $other_changed[$j]) {                    $j++;                }            }             if ($i == $len) {                break;            }             $start = $i;             /* Find the end of this run of changes. */            while (++$i < $len && $changed[$i]) {                continue;            }             do {                /* Record the length of this run of changes, so that we can                 * later determine whether the run has grown. */                $runlength = $i - $start;                 /* Move the changed region back, so long as the previous                 * unchanged line matches the last changed one.  This merges                 * with previous changed regions. */                while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {                    $changed[--$start] = 1;                    $changed[--$i] = false;                    while ($start > 0 && $changed[$start - 1]) {                        $start--;                    }                    assert($j > 0);                    while ($other_changed[--$j]) {                        continue;                    }                    assert($j >= 0 && !$other_changed[$j]);                }                 /* Set CORRESPONDING to the end of the changed run, at the                 * last point where it corresponds to a changed run in the                 * other file. CORRESPONDING == LEN means no such point has                 * been found. */                $corresponding = $j < $other_len ? $i : $len;                 /* Move the changed region forward, so long as the first                 * changed line matches the following unchanged one.  This                 * merges with following changed regions.  Do this second, so                 * that if there are no merges, the changed region is moved                 * forward as far as possible. */                while ($i < $len && $lines[$start] == $lines[$i]) {                    $changed[$start++] = false;                    $changed[$i++] = 1;                    while ($i < $len && $changed[$i]) {

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