r48808 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48807‎ | r48808 | r48809 >
Date:11:48, 25 March 2009
Author:werdna
Status:ok
Tags:
Comment:
Faster brace short-circuit in Abuse Filter Parser. Patch by Robert Rohde.
Modified paths:
  • /trunk/extensions/AbuseFilter/AbuseFilter.parser.php (modified) (history)

Diff [purge]

Index: trunk/extensions/AbuseFilter/AbuseFilter.parser.php
@@ -284,7 +284,7 @@
285285 }
286286
287287 class AbuseFilterParser {
288 - var $mParams, $mVars, $mCode, $mTokens, $mPos, $mCur, $mShortCircuit;
 288+ var $mParams, $mVars, $mCode, $mTokens, $mPos, $mCur, $mShortCircuit, $mAllowShort;
289289
290290 // length,lcase,ccnorm,rmdoubles,specialratio,rmspecials,norm,count
291291 static $mFunctions = array(
@@ -337,14 +337,20 @@
338338 $this->mTokens = array();
339339 $this->mVars = new AbuseFilterVariableHolder;
340340 $this->mPos = 0;
 341+ $this->mShortCircuit = false;
 342+ $this->mAllowShort = true;
341343 }
342344
343345 public function checkSyntax( $filter ) {
344346 try {
 347+ $origAS = $this->mAllowShort;
 348+ $this->mAllowShort = false;
345349 $this->parse($filter);
346350 } catch (AFPUserVisibleException $excep) {
 351+ $this->mAllowShort = $origAS;
347352 return array($excep->getMessage(), $excep->mPosition);
348353 }
 354+ $this->mAllowShort = $origAS;
349355 return true;
350356 }
351357
@@ -374,6 +380,28 @@
375381 return $this->mCur = $token;
376382 }
377383
 384+ protected function skipOverBraces() {
 385+ if( !($this->mCur->type == AFPToken::TBrace && $this->mCur->value == '(') || !$this->mShortCircuit ) {
 386+ return;
 387+ }
 388+
 389+ $braces = 1;
 390+ wfProfileIn( __METHOD__ );
 391+ while( $this->mCur->type != AFPToken::TNone && $braces > 0 ) {
 392+ $this->move();
 393+ if( $this->mCur->type == AFPToken::TBrace ) {
 394+ if( $this->mCur->value == '(' ) {
 395+ $braces++;
 396+ } elseif ($this->mCur->value == ')') {
 397+ $braces--;
 398+ }
 399+ }
 400+ }
 401+ wfProfileOut( __METHOD__ );
 402+ if( !($this->mCur->type == AFPToken::TBrace && $this->mCur->value == ')') )
 403+ throw new AFPUserVisibleException( 'expectednotfound', $this->mCur->pos, array(')') );
 404+ }
 405+
378406 public function parse( $code ) {
379407 return $this->intEval( $code )->toBool();
380408 }
@@ -387,6 +415,7 @@
388416 $this->mCode = $code;
389417 $this->mPos = 0;
390418 $this->mLen = strlen( $code );
 419+ $this->mShortCircuit = false;
391420
392421 // Parse the first token
393422 $this->move();
@@ -438,7 +467,7 @@
439468
440469 if ($isTrue) {
441470 $scOrig = $this->mShortCircuit;
442 - $this->mShortCircuit = true;
 471+ $this->mShortCircuit = $this->mAllowShort;
443472 }
444473 $this->doLevelConditions( $r1 );
445474 if ($isTrue) {
@@ -453,7 +482,7 @@
454483
455484 if (!$isTrue) {
456485 $scOrig = $this->mShortCircuit;
457 - $this->mShortCircuit = true;
 486+ $this->mShortCircuit = $this->mAllowShort;
458487 }
459488 $this->doLevelConditions( $r2 );
460489 if (!$isTrue) {
@@ -483,7 +512,7 @@
484513
485514 if ($isTrue) {
486515 $scOrig = $this->mShortCircuit;
487 - $this->mShortCircuit = true;
 516+ $this->mShortCircuit = $this->mAllowShort;
488517 }
489518 $this->doLevelConditions( $r1 );
490519 if ($isTrue) {
@@ -498,7 +527,7 @@
499528
500529 if (!$isTrue) {
501530 $scOrig = $this->mShortCircuit;
502 - $this->mShortCircuit = true;
 531+ $this->mShortCircuit = $this->mAllowShort;
503532 }
504533 $this->doLevelConditions( $r2 );
505534 if (!$isTrue) {
@@ -525,7 +554,7 @@
526555 if ( $op == '&' && !( $result->toBool() ) ) {
527556 wfProfileIn( __METHOD__.'-shortcircuit' );
528557 $orig = $this->mShortCircuit;
529 - $this->mShortCircuit = true;
 558+ $this->mShortCircuit = $this->mAllowShort;
530559 $this->doLevelCompares( $r2 );
531560 $this->mShortCircuit = $orig;
532561 $result = new AFPData( AFPData::DBool, false );
@@ -536,7 +565,7 @@
537566 if ( $op == '|' && $result->toBool() ) {
538567 wfProfileIn( __METHOD__.'-shortcircuit' );
539568 $orig = $this->mShortCircuit;
540 - $this->mShortCircuit = true;
 569+ $this->mShortCircuit = $this->mAllowShort;
541570 $this->doLevelCompares( $r2 );
542571 $this->mShortCircuit = $orig;
543572 $result = new AFPData( AFPData::DBool, true );
@@ -662,8 +691,12 @@
663692
664693 protected function doLevelBraces( &$result ) {
665694 if( $this->mCur->type == AFPToken::TBrace && $this->mCur->value == '(' ) {
666 - $this->move();
667 - $this->doLevelSet( $result );
 695+ if( $this->mShortCircuit ) {
 696+ $this->skipOverBraces();
 697+ } else {
 698+ $this->move();
 699+ $this->doLevelSet( $result );
 700+ }
668701 if( !($this->mCur->type == AFPToken::TBrace && $this->mCur->value == ')') )
669702 throw new AFPUserVisibleException( 'expectednotfound',
670703 $this->mCur->pos,
@@ -683,6 +716,14 @@
684717 throw new AFPUserVisibleException( 'expectednotfound',
685718 $this->mCur->pos,
686719 array('(', $this->mCur->type, $this->mCur->value ) );
 720+
 721+ if ($this->mShortCircuit) {
 722+ $this->skipOverBraces();
 723+ $this->move();
 724+ wfProfileOut( __METHOD__ );
 725+ return; // The result doesn't matter.
 726+ }
 727+
687728 wfProfileIn( __METHOD__."-loadargs" );
688729 $args = array();
689730 do {
@@ -701,11 +742,6 @@
702743
703744 wfProfileOut( __METHOD__."-loadargs" );
704745
705 - if ($this->mShortCircuit) {
706 - wfProfileOut( __METHOD__ );
707 - return; // The result doesn't matter.
708 - }
709 -
710746 wfProfileIn( __METHOD__."-$func" );
711747
712748 $funcHash = md5($func.serialize($args));

Status & tagging log