Development

Changeset 24021

You must first sign up to be able to contribute.

Changeset 24021

Show
Ignore:
Timestamp:
11/16/09 16:31:23 (4 years ago)
Author:
FabianLange
Message:

[1.3, 1.4] added short circuit checking for a static route prefix. Improves performance with many routes by up to 25%

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.3/lib/routing/sfRoute.class.php

    r23995 r24021  
    2929    $options           = array(), 
    3030    $pattern           = null, 
     31    $staticPrefix      = null, 
    3132    $regex             = null, 
    3233    $variables         = array(), 
     
    9899    } 
    99100 
    100     if (!preg_match($this->regex, $url, $matches)) 
     101    // check the static prefix uf the URL first. Only use the more expensive preg_match when it matches 
     102    if (0 !== strpos($url, $this->staticPrefix) || !preg_match($this->regex, $url, $matches)) 
    101103    { 
    102104      return false; 
     
    451453    } 
    452454 
    453     $this->regex = "#^\n".implode("\n", $this->segments)."\n".preg_quote($separator, '#')."$#x"; 
     455    $this->regex = "#^".implode("", $this->segments)."".preg_quote($separator, '#')."$#x"; 
    454456  } 
    455457 
     
    477479      $this->segments[$i] = (0 == $i ? '/?' : '').str_repeat(' ', $i - $this->firstOptional).'(?:'.$this->segments[$i]; 
    478480      $this->segments[] = str_repeat(' ', $max - $i - 1).')?'; 
     481    } 
     482 
     483    $this->staticPrefix = ''; 
     484    foreach ($this->tokens as $token) 
     485    { 
     486      switch ($token[0]) 
     487      { 
     488        case 'separator': 
     489          // separator is static 
     490          $this->staticPrefix .= $token[2]; 
     491          break; 
     492        case 'text': 
     493          if ($token[2] !== '*') 
     494          { 
     495            // non-star text is static 
     496            $this->staticPrefix .= $token[2]; 
     497            break; 
     498          } 
     499        default: 
     500          // everything else indicates variable parts. break switch and for loop 
     501          break 2; 
     502      } 
    479503    } 
    480504  } 
     
    789813    $this->compile(); 
    790814    // sfPatternRouting will always re-set defaultParameters, so no need to serialize them 
    791     return serialize(array($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix)); 
     815    return serialize(array($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->staticPrefix, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix)); 
    792816  } 
    793817 
    794818  public function unserialize($data) 
    795819  { 
    796     list($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix) = unserialize($data); 
     820    list($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->staticPrefix, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix) = unserialize($data); 
    797821    $this->compiled = true; 
    798822  } 
  • branches/1.4/lib/routing/sfRoute.class.php

    r23995 r24021  
    2929    $options           = array(), 
    3030    $pattern           = null, 
     31    $staticPrefix      = null, 
    3132    $regex             = null, 
    3233    $variables         = array(), 
     
    9899    } 
    99100 
    100     if (!preg_match($this->regex, $url, $matches)) 
     101    // check the static prefix uf the URL first. Only use the more expensive preg_match when it matches 
     102    if (0 !== strpos($url, $this->staticPrefix) || !preg_match($this->regex, $url, $matches)) 
    101103    { 
    102104      return false; 
     
    451453    } 
    452454 
    453     $this->regex = "#^\n".implode("\n", $this->segments)."\n".preg_quote($separator, '#')."$#x"; 
     455    $this->regex = "#^".implode("", $this->segments)."".preg_quote($separator, '#')."$#x"; 
    454456  } 
    455457 
     
    477479      $this->segments[$i] = (0 == $i ? '/?' : '').str_repeat(' ', $i - $this->firstOptional).'(?:'.$this->segments[$i]; 
    478480      $this->segments[] = str_repeat(' ', $max - $i - 1).')?'; 
     481    } 
     482 
     483    $this->staticPrefix = ''; 
     484    foreach ($this->tokens as $token) 
     485    { 
     486      switch ($token[0]) 
     487      { 
     488        case 'separator': 
     489          // separator is static 
     490          $this->staticPrefix .= $token[2]; 
     491          break; 
     492        case 'text': 
     493          if ($token[2] !== '*') 
     494          { 
     495            // non-star text is static 
     496            $this->staticPrefix .= $token[2]; 
     497            break; 
     498          } 
     499        default: 
     500          // everything else indicates variable parts. break switch and for loop 
     501          break 2; 
     502      } 
    479503    } 
    480504  } 
     
    789813    $this->compile(); 
    790814    // sfPatternRouting will always re-set defaultParameters, so no need to serialize them 
    791     return serialize(array($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix)); 
     815    return serialize(array($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->staticPrefix, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix)); 
    792816  } 
    793817 
    794818  public function unserialize($data) 
    795819  { 
    796     list($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix) = unserialize($data); 
     820    list($this->tokens, $this->defaultOptions, $this->options, $this->pattern, $this->staticPrefix, $this->regex, $this->variables, $this->defaults, $this->requirements, $this->suffix) = unserialize($data); 
    797821    $this->compiled = true; 
    798822  }