Avoid expensive array_shift where possible
authorThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Mon, 17 Dec 2018 10:58:55 +0000 (11:58 +0100)
committerThiemo Kreuz <thiemo.kreuz@wikimedia.de>
Mon, 17 Dec 2018 10:58:55 +0000 (11:58 +0100)
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
includes/cache/CacheHelper.php
includes/htmlform/HTMLForm.php
includes/htmlform/HTMLFormField.php

index 038e8b1..f4c3abe 100644 (file)
@@ -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();
index 6c6b184..93685e3 100644 (file)
@@ -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 " .
index 117a8fb..1a7f850 100644 (file)
@@ -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] = [];
                                        }
index 5f99aa0..818474d 100644 (file)
@@ -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;
                                }