From 067fe93cef2b9ea492a5ae373018818f62f3ffec Mon Sep 17 00:00:00 2001 From: doctrine Date: Mon, 29 May 2006 09:17:07 +0000 Subject: [PATCH] 200% speed increase for get/set with with new Null object --- classes/Collection.class.php | 10 ++++++++++ classes/Collection/Batch.class.php | 3 ++- classes/Manager.class.php | 16 +++++++++++++++- classes/Record.class.php | 24 ++++++++++++++---------- tests/run.php | 4 ++-- 5 files changed, 43 insertions(+), 14 deletions(-) diff --git a/classes/Collection.class.php b/classes/Collection.class.php index 3d490c510..3a1684896 100644 --- a/classes/Collection.class.php +++ b/classes/Collection.class.php @@ -41,6 +41,10 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator * @var mixed $generator */ protected $generator; + /** + * @var Doctrine_Null $null used for extremely fast SQL null value testing + */ + protected static $null; /** * constructor @@ -53,6 +57,12 @@ class Doctrine_Collection extends Doctrine_Access implements Countable, Iterator $this->generator = new Doctrine_IndexGenerator($name); } } + /** + * initNullObject + */ + public static function initNullObject(Doctrine_Null $null) { + self::$null = $null; + } /** * @return object Doctrine_Table */ diff --git a/classes/Collection/Batch.class.php b/classes/Collection/Batch.class.php index bae8bcaae..27eb8c460 100644 --- a/classes/Collection/Batch.class.php +++ b/classes/Collection/Batch.class.php @@ -80,7 +80,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { $proxies = array(); for($i = $e; $i < $e2 && $i < $this->count(); $i++): - if(is_object($this->data[$i])) + if($this->data[$i] instanceof Doctrine_Record) $id = $this->data[$i]->getID(); elseif(is_array($this->data[$i])) $id = $this->data[$i][$identifier]; @@ -120,6 +120,7 @@ class Doctrine_Collection_Batch extends Doctrine_Collection { return false; } } + /** * get * @param mixed $key the key of the record diff --git a/classes/Manager.class.php b/classes/Manager.class.php index fdef823b8..4728ed6a0 100644 --- a/classes/Manager.class.php +++ b/classes/Manager.class.php @@ -25,13 +25,27 @@ class Doctrine_Manager extends Doctrine_Configurable implements Countable, Itera /** * @var string $root root directory */ - private $root; + private $root; + /** + * @var Doctrine_Null $null Doctrine_Null object, used for extremely fast null value checking + */ + private $null; /** * constructor */ private function __construct() { $this->root = dirname(__FILE__); + $this->null = new Doctrine_Null; + + Doctrine_Record::initNullObject($this->null); + Doctrine_Collection::initNullObject($this->null); + } + /** + * @return Doctrine_Null + */ + final public function getNullObject() { + return $this->null; } /** * setDefaultAttributes diff --git a/classes/Record.class.php b/classes/Record.class.php index 7200846a4..d10a97ce3 100644 --- a/classes/Record.class.php +++ b/classes/Record.class.php @@ -84,9 +84,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite */ private static $index = 1; /** - * @var Doctrine_Null $nullObject a Doctrine_Null object used for SQL null value testing + * @var Doctrine_Null $nullObject a Doctrine_Null object used for extremely fast + * SQL null value testing */ - private static $nullObject; + private static $null; /** * @var integer $oid object identifier */ @@ -164,8 +165,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite /** * initNullObject */ - public static function initNullObject() { - self::$nullObject = new Doctrine_Null; + public static function initNullObject(Doctrine_Null $null) { + self::$null = $null; } /** * setUp @@ -198,7 +199,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite foreach($this->table->getColumnNames() as $name) { if( ! isset($tmp[$name])) { - $this->data[$name] = array(); + $this->data[$name] = self::$null; } else { $cols++; @@ -231,7 +232,10 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->id = array(); foreach($names as $name) { - $this->id[$name] = isset($this->data[$name])?$this->data[$name]:null; + if($this->data[$name] === self::$null) + $this->id[$name] = null; + else + $this->id[$name] = $this->data[$name]; } break; endswitch; @@ -408,8 +412,8 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite public function get($name) { if(isset($this->data[$name])) { - // check if the property is not loaded (= it is an empty array) - if(is_array($this->data[$name])) { + // check if the property is null (= it is the Doctrine_Null object located in self::$null) + if($this->data[$name] === self::$null) { // no use trying to load the data from database if the Doctrine_Record is not a proxy if($this->state == Doctrine_Record::STATE_PROXY) { @@ -423,7 +427,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $this->state = Doctrine_Record::STATE_CLEAN; } - if(is_array($this->data[$name])) + if($this->data[$name] === self::$null) return null; } return $this->data[$name]; @@ -453,7 +457,7 @@ abstract class Doctrine_Record extends Doctrine_Access implements Countable, Ite $value = $id; if(isset($this->data[$name])) { - if( ! is_array($this->data[$name])) { + if($this->data[$name] === self::$null) { if($this->data[$name] !== $value) { switch($this->state): case Doctrine_Record::STATE_CLEAN: diff --git a/tests/run.php b/tests/run.php index 278bbe54c..892fd0e96 100644 --- a/tests/run.php +++ b/tests/run.php @@ -24,12 +24,12 @@ $test = new GroupTest("Doctrine Framework Unit Tests"); //$test->addTestCase(new Sensei_UnitTestCase()); +$test->addTestCase(new Doctrine_RecordTestCase()); + $test->addTestCase(new Doctrine_SessionTestCase()); $test->addTestCase(new Doctrine_TableTestCase()); -$test->addTestCase(new Doctrine_RecordTestCase()); - $test->addTestCase(new Doctrine_ValidatorTestCase()); $test->addTestCase(new Doctrine_ManagerTestCase());