Merge "Rewrite ImmutableRange::findCommonAncestorContainer for speed"

This commit is contained in:
jenkins-bot 2023-11-02 18:41:29 +00:00 committed by Gerrit Code Review
commit e2f396fc99

View file

@ -47,19 +47,34 @@ class ImmutableRange {
$ancestorsA = []; $ancestorsA = [];
$ancestorsB = []; $ancestorsB = [];
$parent = $a;
do { do {
$ancestorsA[] = $a; // While walking up the parents of $a we found $b is a parent of $a or even identical
} while ( ( $a = $a->parentNode ) ); if ( $parent === $b ) {
return $b;
}
$ancestorsA[] = $parent;
} while ( $parent = $parent->parentNode );
$parent = $b;
do { do {
$ancestorsB[] = $b; // While walking up the parents of $b we found $a is a parent of $b or even identical
} while ( ( $b = $b->parentNode ) ); if ( $parent === $a ) {
return $a;
}
$ancestorsB[] = $parent;
} while ( $parent = $parent->parentNode );
$node = null; $node = null;
while ( end( $ancestorsA ) && end( $ancestorsA ) === end( $ancestorsB ) ) { // Start with the top-most (hopefully) identical root node, walk down, skip everything
$node = end( $ancestorsA ); // that's identical, and stop at the first mismatch
array_pop( $ancestorsA ); $indexA = count( $ancestorsA );
array_pop( $ancestorsB ); $indexB = count( $ancestorsB );
while ( $indexA-- && $indexB-- && $ancestorsA[$indexA] === $ancestorsB[$indexB] ) {
// Remember the last match closest to $a and $b
$node = $ancestorsA[$indexA];
} }
if ( !$node ) { if ( !$node ) {
throw new DOMException( 'Nodes are not in the same document' ); throw new DOMException( 'Nodes are not in the same document' );
} }