From 337771f83bf1d6c45e8e705d3cc21c84df66199f Mon Sep 17 00:00:00 2001 From: Daimona Eaytoy Date: Tue, 1 Oct 2019 16:49:19 +0200 Subject: [PATCH] Replace array_map with foreach This is a micro-optimization, but IMHO it's necessary. The AF parser code is executed for every active filter, for every edit/move/deletion/accountcreation. In PHP, foreach is usually faster than array_map. Especially in the case of variadic functions potentially taking hundreds of strings, foreach will consume less time. Bug: T234427 Change-Id: I1beedf419a6637a9a3dd668635645df950ceda21 --- includes/parser/AbuseFilterCachingParser.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/includes/parser/AbuseFilterCachingParser.php b/includes/parser/AbuseFilterCachingParser.php index 7199a4a6e..d9e4c7e64 100644 --- a/includes/parser/AbuseFilterCachingParser.php +++ b/includes/parser/AbuseFilterCachingParser.php @@ -148,14 +148,23 @@ class AbuseFilterCachingParser extends AbuseFilterParser { // @codeCoverageIgnoreEnd } case AFPTreeNode::ARRAY_DEFINITION: - $items = array_map( [ $this, 'evalNode' ], $node->children ); + $items = []; + // Foreach is usually faster than array_map + // @phan-suppress-next-line PhanTypeSuspiciousNonTraversableForeach children is array here + foreach ( $node->children as $el ) { + $items[] = $this->evalNode( $el ); + } return new AFPData( AFPData::DARRAY, $items ); case AFPTreeNode::FUNCTION_CALL: $functionName = $node->children[0]; $args = array_slice( $node->children, 1 ); - $dataArgs = array_map( [ $this, 'evalNode' ], $args ); + $dataArgs = []; + // Foreach is usually faster than array_map + foreach ( $args as $arg ) { + $dataArgs[] = $this->evalNode( $arg ); + } return $this->callFunc( $functionName, $dataArgs ); case AFPTreeNode::ARRAY_INDEX: