From fa7e5bd90199576bb7bce9b57569d3382f0a9993 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Mon, 17 Dec 2018 11:58:55 +0100 Subject: [PATCH] Avoid expensive array_shift where possible array_shift manipulates the original array. This is surprisingly expensive, because it iterates *all* elements in the array and decrements numeric keys. The code touched in this patch does not need this restructured new array, but only the individual elements. Change-Id: Iee28377b2c9930f6de821e041381a1d7564f7633 --- includes/Title.php | 13 +++++++------ includes/cache/CacheHelper.php | 4 ++-- includes/htmlform/HTMLForm.php | 6 +----- includes/htmlform/HTMLFormField.php | 3 +-- 4 files changed, 11 insertions(+), 15 deletions(-) diff --git a/includes/Title.php b/includes/Title.php index 038e8b1b20..f4c3abe1c1 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -2858,10 +2858,12 @@ class Title implements LinkTarget { } $errors = []; - while ( count( $checks ) > 0 && - !( $short && count( $errors ) > 0 ) ) { - $method = array_shift( $checks ); + foreach ( $checks as $method ) { $errors = $this->$method( $action, $user, $errors, $rigor, $short ); + + if ( $short && $errors !== [] ) { + break; + } } return $errors; @@ -5218,10 +5220,9 @@ class Title implements LinkTarget { if ( MWNamespace::hasSubpages( $this->mNamespace ) ) { // Optional notice for page itself and any parent page - $parts = explode( '/', $this->mDbkeyform ); $editnotice_base = $editnotice_ns; - while ( count( $parts ) > 0 ) { - $editnotice_base .= '-' . array_shift( $parts ); + foreach ( explode( '/', $this->mDbkeyform ) as $part ) { + $editnotice_base .= '-' . $part; $msg = wfMessage( $editnotice_base ); if ( $msg->exists() ) { $html = $msg->parseAsBlock(); diff --git a/includes/cache/CacheHelper.php b/includes/cache/CacheHelper.php index 6c6b184735..93685e35af 100644 --- a/includes/cache/CacheHelper.php +++ b/includes/cache/CacheHelper.php @@ -270,8 +270,8 @@ class CacheHelper implements ICacheHelper { $value = null; if ( is_null( $key ) ) { - $itemKey = array_keys( array_slice( $this->cachedChunks, 0, 1 ) ); - $itemKey = array_shift( $itemKey ); + reset( $this->cachedChunks ); + $itemKey = key( $this->cachedChunks ); if ( !is_int( $itemKey ) ) { wfWarn( "Attempted to get item with non-numeric key while " . diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 117a8fb912..1a7f8500d2 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -344,11 +344,7 @@ class HTMLForm extends ContextSource { $setSection =& $loadedDescriptor; if ( $section ) { - $sectionParts = explode( '/', $section ); - - while ( count( $sectionParts ) ) { - $newName = array_shift( $sectionParts ); - + foreach ( explode( '/', $section ) as $newName ) { if ( !isset( $setSection[$newName] ) ) { $setSection[$newName] = []; } diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 5f99aa0ca5..818474d47e 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -137,8 +137,7 @@ abstract class HTMLFormField { for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) { $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys ); $data = $alldata; - while ( $keys ) { - $key = array_shift( $keys ); + foreach ( $keys as $key ) { if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) { continue 2; } -- 2.20.1