Reduced the #calls of rawurlencode() using a cache mechanism #3045

Closed
wants to merge 1 commit into
from
@@ -25,6 +25,11 @@
class Segment implements RouteInterface
{
/**
+ * @var array Cache for the encode output
+ */
+ private static $__cacheEncode = array();
@Maks3w

Maks3w Nov 21, 2012

Member

var name should not start with underscores

@weierophinney

weierophinney Nov 21, 2012

Owner

Our CS allows it; we use it in several places where the functionality is an
implementation detail we want to call out as something to avoid modifying.

On Wednesday, November 21, 2012, Maks wrote:

In library/Zend/Mvc/Router/Http/Segment.php:

@@ -25,6 +25,11 @@
class Segment implements RouteInterface
{
/**

  • \* @var array Cache for the encode output
    
  • */
    
  • private static $__cacheEncode = array();

var name should not start with underscores


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/3045/files#r2198911.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

@Maks3w

Maks3w Nov 21, 2012

Member

For me the private visibility is enough since forbid "unauthorized" modifications in extended classes

+
+ /**
* Map of allowed special chars in path segments.
*
* http://tools.ietf.org/html/rfc3986#appendix-A
@@ -408,9 +413,11 @@ public function getAssembledParams()
*/
private function encode($value)
{
- $encoded = rawurlencode($value);
- $encoded = strtr($encoded, static::$urlencodeCorrectionMap);
- return $encoded;
+ if (!isset(static::$__cacheEncode[$value])) {
+ static::$__cacheEncode[$value] = rawurlencode($value);
+ static::$__cacheEncode[$value] = strtr(static::$__cacheEncode[$value], static::$urlencodeCorrectionMap);
+ }
+ return static::$__cacheEncode[$value];
}
/**