aboutsummaryrefslogtreecommitdiffstats
path: root/includes/utils/ClassCollector.php
diff options
context:
space:
mode:
authorTimo Tijhof <krinklemail@gmail.com>2019-06-18 18:26:06 +0100
committerKrinkle <krinklemail@gmail.com>2019-06-19 15:09:49 +0000
commit8035a00e8cf2bd3db6e8dbe0710cd3816a00c97a (patch)
tree7eb803e2dbc2ebb5668eedb93476336c7363c635 /includes/utils/ClassCollector.php
parent8b22883a66bcc7d54be7acb9898defa545751f86 (diff)
downloadmediawikicore-8035a00e8cf2bd3db6e8dbe0710cd3816a00c97a.tar.gz
mediawikicore-8035a00e8cf2bd3db6e8dbe0710cd3816a00c97a.zip
AutoLoader: Skip tokenizing of irrelevant lines in ClassCollector
This makes AutoLoaderStructureTest in PHPUnit and the generateLocalAutoload.php maintenance script much faster. On my machine, it made it 35X faster (or time spent reduced by 97%). Bug: T225730 Change-Id: Ife959bd17ce9c2ae952dfbd158ddb3d8475e8cb2
Diffstat (limited to 'includes/utils/ClassCollector.php')
-rw-r--r--includes/utils/ClassCollector.php25
1 files changed, 25 insertions, 0 deletions
diff --git a/includes/utils/ClassCollector.php b/includes/utils/ClassCollector.php
index c987354d3737..12b8a707bd6f 100644
--- a/includes/utils/ClassCollector.php
+++ b/includes/utils/ClassCollector.php
@@ -59,6 +59,31 @@ class ClassCollector {
$this->alias = null;
$this->tokens = [];
+ // HACK: The PHP tokenizer is slow (T225730).
+ // Speed it up by reducing the input to the three kinds of statement we care about:
+ // - namespace X;
+ // - [final] [abstract] class X … {}
+ // - class_alias( … );
+ $lines = [];
+ $matches = null;
+ preg_match_all(
+ // phpcs:ignore Generic.Files.LineLength.TooLong
+ '#^\t*(?:namespace |(final )?(abstract )?(class|interface|trait) |class_alias\()[^;{]+[;{]\s*\}?#m',
+ $code,
+ $matches
+ );
+ if ( isset( $matches[0][0] ) ) {
+ foreach ( $matches[0] as $match ) {
+ $match = trim( $match );
+ if ( substr( $match, -1 ) === '{' ) {
+ // Keep it balanced
+ $match .= '}';
+ }
+ $lines[] = $match;
+ }
+ }
+ $code = '<?php ' . implode( "\n", $lines ) . "\n";
+
foreach ( token_get_all( $code ) as $token ) {
if ( $this->startToken === null ) {
$this->tryBeginExpect( $token );