wppaste
WordPress

Text_Diff_Engine_native::_diag( $xoff, $xlim, $yoff, $ylim, $nchunks )

Source
wp-includes/Text/Diff/Engine/native.php:159
Divides the Largest Common Subsequence (LCS) of the sequences (XOFF, XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.

Description

Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of NCHUNKS+1 (X, Y) indexes giving the diving points between sub sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1), the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) == (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).

This function assumes that the first lines of the specified portions of the two files do not match, and likewise that the last lines do not match. The caller must trim matching lines from the beginning and end of the portions it is going to specify.

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

$xoff
$xlim
$yoff
$ylim
$nchunks

Performance profile

How much work a call to Text_Diff_Engine_native::_diag() 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
55–72

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

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 · 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::_diag() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!$yoff && !$chunk && !$n55–72none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev23055–7222
8.523055–7222
8.423055–7222
8.323055–7222
8.223055–72222 more instructions than PHP 8.1
8.122855–752210 fewer instructions than PHP 7.4
7.423859–8022

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 · 1

Used by · 1

Source code

    function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)    {        $flip = false;         if ($xlim - $xoff > $ylim - $yoff) {            /* Things seems faster (I'm not sure I understand why) when the             * shortest sequence is in X. */            $flip = true;            list ($xoff, $xlim, $yoff, $ylim)                = array($yoff, $ylim, $xoff, $xlim);        }         if ($flip) {            for ($i = $ylim - 1; $i >= $yoff; $i--) {                $ymatches[$this->xv[$i]][] = $i;            }        } else {            for ($i = $ylim - 1; $i >= $yoff; $i--) {                $ymatches[$this->yv[$i]][] = $i;            }        }         $this->lcs = 0;        $this->seq[0]= $yoff - 1;        $this->in_seq = array();        $ymids[0] = array();         $numer = $xlim - $xoff + $nchunks - 1;        $x = $xoff;        for ($chunk = 0; $chunk < $nchunks; $chunk++) {            if ($chunk > 0) {                for ($i = 0; $i <= $this->lcs; $i++) {                    $ymids[$i][$chunk - 1] = $this->seq[$i];                }            }             $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);            for (; $x < $x1; $x++) {                $line = $flip ? $this->yv[$x] : $this->xv[$x];                if (empty($ymatches[$line])) {                    continue;                }                $matches = $ymatches[$line];                reset($matches);                while ($y = current($matches)) {                    if (empty($this->in_seq[$y])) {                        $k = $this->_lcsPos($y);                        assert($k > 0);                        $ymids[$k] = $ymids[$k - 1];                        break;                    }                    next($matches);                }                while ($y = current($matches)) {                    if ($y > $this->seq[$k - 1]) {                        assert($y <= $this->seq[$k]);                        /* Optimization: this is a common case: next match is                         * just replacing previous match. */                        $this->in_seq[$this->seq[$k]] = false;                        $this->seq[$k] = $y;                        $this->in_seq[$y] = 1;                    } elseif (empty($this->in_seq[$y])) {                        $k = $this->_lcsPos($y);                        assert($k > 0);                        $ymids[$k] = $ymids[$k - 1];                    }                    next($matches);                }            }        }         $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);        $ymid = $ymids[$this->lcs];        for ($n = 0; $n < $nchunks - 1; $n++) {            $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);            $y1 = $ymid[$n] + 1;            $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);        }        $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);

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.