From: Cameron Ball Date: Mon, 15 Dec 2014 11:24:19 +0000 (+0800) Subject: Tricks to reduce memory usage. X-Git-Url: http://git.cameron1729.xyz/?p=rock.divinelegy.git;a=commitdiff_plain;h=293e002cbca1aa39361c5050a1af4b0f7298c4aa Tricks to reduce memory usage. --- diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index 384627d..4936aaf 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -7,6 +7,7 @@ use DataAccess\IDatabaseFactory; use DataAccess\DataMapper\IDataMapper; use DataAccess\Queries\IQueryBuilder; use DataAccess\DataMapper\Helpers\AbstractPopulationHelper; +use DataAccess\DataMapper\LazyLoadedEntities; use ReflectionClass; class DataMapper implements IDataMapper @@ -31,6 +32,12 @@ class DataMapper implements IDataMapper $rows = $statement->fetchAll(); $entities = array(); + + if(count($rows) > 2) + { + //TODO: Factory? + return new LazyLoadedEntities($rows, $entityName, $this->_maps, $this->_db); + } foreach($rows as $row) { @@ -48,7 +55,7 @@ class DataMapper implements IDataMapper $class->setId($row['id']); $entities[$row['id']] = $class; } - + return $entities; } diff --git a/DataAccess/DataMapper/LazyLoadedEntities.php b/DataAccess/DataMapper/LazyLoadedEntities.php new file mode 100644 index 0000000..724e635 --- /dev/null +++ b/DataAccess/DataMapper/LazyLoadedEntities.php @@ -0,0 +1,103 @@ +_rows = $rows; + $this->_entityName = $entityName; + $this->_maps = $maps; + $this->_db = $db; + } + + public function current() + { + return $this->_loadedEntities[$this->_loadedEntitiesIndex]; + } + + public function key() + { + return $this->_loadedEntitiesIndex; + } + + public function next() + { + $keys = array_keys($this->_loadedEntities); + $pos = array_search($this->_loadedEntitiesIndex, $keys); + if(isset($keys[$pos + 1])) + { + $this->_loadedEntitiesIndex = $keys[$pos+1]; + } else { + $this->mapEntities(); //sets the loaded entites index + } + } + + public function rewind() + { + $this->_rowIndex = 0; + $this->mapEntities(); + } + + public function valid() + { + //next will always load more entities when it runs out, therefore if + //we have an empty loadEntities array, it means there were no more and we are done. + if($this->_loadedEntities) + { + return true; + } + + return false; + } + + //unsets the current entities array, and maps in the next 10 + private function mapEntities() + { + $numToMap = 50; + $tick = 0; + + unset($this->_loadedEentities); + $this->_loadedEntities = array(); + + $entityName = $this->_entityName; + for($i = $this->_rowIndex; $i<$this->_rowIndex+$numToMap; $i++) + { + if(isset($this->_rows[$i])) + { + $row = $this->_rows[$i]; + $className = $this->_maps[$entityName]['class']; //the entity to instantiate and return + $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entityName, $row, $this->_db); + + if(count($constructors) == 0) + { + $class = new $className; + } else { + $r = new ReflectionClass($className); + $class = $r->newInstanceArgs($constructors); + } + + $class->setId($row['id']); + $this->_loadedEntities[$row['id']] = $class; + + if($tick == 0) $this->_loadedEntitiesIndex = $row['id']; + $tick++; + } + } + + $this->_rowIndex += $numToMap; + } +} \ No newline at end of file