From 6f2ada2bb4fc6098e296d89eb7eb71fb574345fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Tue, 20 Oct 2020 10:45:59 +0200 Subject: [PATCH] ImmutableRange: Avoid doing expensive TreeWalker computation twice If A follows B, then we can assume that B does not follow A. Calling the function recursively computes that twice, we can instead make some simple changes to "invert" the result. Change-Id: I709aca7cb997dd2fe3980468a8c6bde6f366fb5b --- includes/ImmutableRange.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/includes/ImmutableRange.php b/includes/ImmutableRange.php index f9ca36399..b40795568 100644 --- a/includes/ImmutableRange.php +++ b/includes/ImmutableRange.php @@ -435,14 +435,9 @@ class ImmutableRange { $tw->currentNode = $boundaryPointB[0]; $AFollowsB = $tw->nextNode(); - if ( $AFollowsB ) { - switch ( $this->computePosition( $boundaryPointB, $boundaryPointA ) ) { - case 'after': - return 'before'; - case 'before': - return 'after'; - } + // Swap variables + [ $boundaryPointB, $boundaryPointA ] = [ $boundaryPointA, $boundaryPointB ]; } $ancestor = $boundaryPointB[0]->parentNode; @@ -467,11 +462,11 @@ class ImmutableRange { } if ( CommentUtils::childIndexOf( $child ) < $boundaryPointA[1] ) { - return 'after'; + return $AFollowsB ? 'before' : 'after'; } } - return 'before'; + return $AFollowsB ? 'after' : 'before'; } }