From d4b15cb7ee03429067820ccf1e15c5cb56dd9243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Such=C3=A1nek?= Date: Tue, 3 May 2022 10:59:09 +0200 Subject: [PATCH] Optimize loop in 'diff-split' case The "substr( $line, 0, 1 )" expression has already assumed the prefix has length 1. Therefore, it's pointless to compute its length later. The assumption does hold, the only two prefixes the code works with are '+' and '-'. Not changing the check to use str_starts_with now, because it was suggested in I113a8d052b6845852c15969a2f0e6fbbe3e9f8d9 that this shouldn't be done for performance-sensitive code at least until we are on PHP 8. Change-Id: I00cb2fc50ed534bb2bbef3ee1e5f6f466afeeb27 --- includes/Variables/LazyVariableComputer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/Variables/LazyVariableComputer.php b/includes/Variables/LazyVariableComputer.php index 94751601f..05475a111 100644 --- a/includes/Variables/LazyVariableComputer.php +++ b/includes/Variables/LazyVariableComputer.php @@ -179,8 +179,8 @@ class LazyVariableComputer { $diff_lines = explode( "\n", $diff ); $result = []; foreach ( $diff_lines as $line ) { - if ( substr( $line, 0, 1 ) === $line_prefix ) { - $result[] = substr( $line, strlen( $line_prefix ) ); + if ( ( $line[0] ?? '' ) === $line_prefix ) { + $result[] = substr( $line, 1 ); } } break;