[limb-svn] r6403 - in 3.x/trunk/limb/core: . src tests/bench

svn at limb-project.com svn at limb-project.com
Wed Oct 10 01:27:41 MSD 2007


Author: pachanga
Date: 2007-10-10 01:27:41 +0400 (Wed, 10 Oct 2007)
New Revision: 6403
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6403

Modified:
   3.x/trunk/limb/core/common.inc.php
   3.x/trunk/limb/core/src/lmbObject.class.php
   3.x/trunk/limb/core/tests/bench/object-attr.php
Log:
-- adding misc optimizations into lmbObject getters


Modified: 3.x/trunk/limb/core/common.inc.php
===================================================================
--- 3.x/trunk/limb/core/common.inc.php	2007-10-09 15:18:40 UTC (rev 6402)
+++ 3.x/trunk/limb/core/common.inc.php	2007-10-09 21:27:41 UTC (rev 6403)
@@ -168,11 +168,16 @@
 
 function lmb_under_scores($str)
 {
+  static $cache = array();
+  if(isset($cache[$str]))
+    return $cache[$str];
   $items = preg_split('~([A-Z][a-z0-9]+)~', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
   $res = '';
   foreach($items as $item)
     $res .= ($item == '_' ? '' : '_') . strtolower($item);
-  return substr($res, 1);
+  $res = substr($res, 1);
+  $cache[$str] = $res;
+  return $res;
 }
 
 function lmb_humanize($str)

Modified: 3.x/trunk/limb/core/src/lmbObject.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbObject.class.php	2007-10-09 15:18:40 UTC (rev 6402)
+++ 3.x/trunk/limb/core/src/lmbObject.class.php	2007-10-09 21:27:41 UTC (rev 6403)
@@ -159,20 +159,6 @@
     return $names;
   }
   /**
-   * Returns property value if it exists and not guarded.
-   * Magically maps getter to fine-grained method if it exists, e.g. get('foo') => getFoo()
-   * @param string property name
-   * @return mixed|null
-   */
-  function get($name)
-  {
-    if($method = $this->_mapPropertyToMethod($name))
-      return $this->$method();
-
-    if(!$this->_isGuarded($name))
-      return $this->_getRaw($name);
-  }
-  /**
    * Removes specified property
    * @param string
    */
@@ -198,15 +184,19 @@
       $this->remove($name);
   }
 
-  protected function _getRaw($name)
+  /**
+   * Returns property value if it exists and not guarded.
+   * Magically maps getter to fine-grained method if it exists, e.g. get('foo') => getFoo()
+   * @param string property name
+   * @return mixed|null
+   */
+  function get($name)
   {
-    if(isset($this->$name))
-      return $this->$name;
-  }
+    if($method = $this->_mapPropertyToMethod($name))
+      return $this->$method();
 
-  protected function _getObjectVars()
-  {
-    return get_object_vars($this);
+    if(!$this->_isGuarded($name))
+      return $this->_getRaw($name);
   }
   /**
    * Sets property value
@@ -248,6 +238,17 @@
   }
   /**#@-*/
 
+  protected function _getRaw($name)
+  {
+    if(isset($this->$name))
+      return $this->$name;
+  }
+
+  protected function _getObjectVars()
+  {
+    return get_object_vars($this);
+  }
+
   protected function _setRaw($name, $value)
   {
     $this->$name = $value;
@@ -262,7 +263,8 @@
   {
     if($property = $this->_mapGetToProperty($method))
     {
-      return $this->get($property);
+      if(!$this->_isGuarded($property))
+        return $this->_getRaw($property);
     }
     elseif($property = $this->_mapSetToProperty($method))
     {
@@ -287,13 +289,24 @@
 
   protected function _mapPropertyToMethod($property)
   {
+    static $map = array();
+    if(isset($map[$property]))
+      return $map[$property];
+
     $capsed = lmb_camel_case($property);
     $method = 'get' . $capsed;
     if(method_exists($this, $method))
+    {
+      $map[$property] = $method;
       return $method;
+    }
     //'is_foo' property is mapped to 'isFoo' method if it exists
     if(strpos($property, 'is_') === 0 && method_exists($this, $capsed))
+    {
+      $map[$property] = $capsed;
       return $capsed;
+    }
+    $map[$property] = false;
   }
 
   protected function _mapPropertyToSetMethod($property)

Modified: 3.x/trunk/limb/core/tests/bench/object-attr.php
===================================================================
--- 3.x/trunk/limb/core/tests/bench/object-attr.php	2007-10-09 15:18:40 UTC (rev 6402)
+++ 3.x/trunk/limb/core/tests/bench/object-attr.php	2007-10-09 21:27:41 UTC (rev 6403)
@@ -13,6 +13,9 @@
 }
 $object = new Foo(array('foo' => 'foo'));
 
+for($i=0;$i<1000;$i++)
+  $object->get('heatingUp');
+
 $mark = microtime(true);
 
 for($i=0;$i<1000;$i++)



More information about the limb-svn mailing list