Skip to content

Commit

Permalink
Implemented #24702477: Huge memory footprint.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelpichler committed Feb 14, 2012
1 parent 23ffa2d commit 75c9755
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 182 deletions.
Expand Up @@ -129,7 +129,7 @@ public function accept(PHP_Depend_Code_ASTVisitorI $visitor, $data = null)
* Magic method which returns the names of all those properties that should
* be cached for this node instance.
*
* @return array(string)
* @return array
* @since 0.10.0
*/
public function __sleep()
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHP/Depend/Code/ASTSelfReference.php
Expand Up @@ -138,7 +138,7 @@ public function getType()
* before an instance of this class gets serialized. It should return an
* array with those property names that should be serialized for this class.
*
* @return array(string)
* @return array
* @since 0.10.0
*/
public function __sleep()
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/PHP/Depend/Code/AbstractClassOrInterface.php
Expand Up @@ -293,7 +293,7 @@ public function getAllMethods()
$methods[strtolower($method->getName())] = $method;
}

foreach ($this->methods as $method) {
foreach ($this->getMethods() as $method) {
$methods[strtolower($method->getName())] = $method;
}

Expand Down Expand Up @@ -387,7 +387,7 @@ private function _initConstants()
* names of all those properties that should be cached for this class or
* interface instance.
*
* @return array(string)
* @return array
* @since 0.10.0
*/
public function __sleep()
Expand Down
69 changes: 49 additions & 20 deletions src/main/php/PHP/Depend/Code/AbstractType.php
Expand Up @@ -83,13 +83,6 @@ abstract class PHP_Depend_Code_AbstractType extends PHP_Depend_Code_AbstractItem
*/
private $_package = null;

/**
* List of {@link PHP_Depend_Code_Method} objects in this class.
*
* @var array(PHP_Depend_Code_Method)
*/
protected $methods = array();

/**
* An <b>array</b> with all constants defined in this class or interface.
*
Expand Down Expand Up @@ -142,6 +135,22 @@ abstract class PHP_Depend_Code_AbstractType extends PHP_Depend_Code_AbstractItem
*/
protected $cached = false;

/**
* The modifiers for this class instance.
*
* @var integer $_modifiers
*/
protected $modifiers = 0;

/**
* Temporary property that only holds methods during the parsing process.
*
* @var PHP_Depend_Code_Method[]
* @since 1.0.2
*/
private $_methods = array();


/**
* Setter method for the currently used token cache, where this class or
* interface instance can store the associated tokens.
Expand Down Expand Up @@ -213,11 +222,13 @@ public function getFirstChildOfType($targetType)
return $child;
}
}
foreach ($this->methods as $method) {
$methods = $this->getMethods();
foreach ($methods as $method) {
if (($child = $method->getFirstChildOfType($targetType)) !== null) {
return $child;
}
}

return null;
}

Expand All @@ -239,9 +250,11 @@ public function findChildrenOfType($targetType, array &$results = array())
}
$node->findChildrenOfType($targetType, $results);
}
foreach ($this->methods as $method) {

foreach ($this->getMethods() as $method) {
$method->findChildrenOfType($targetType, $results);
}

return $results;
}

Expand Down Expand Up @@ -274,7 +287,20 @@ public function setUserDefined()
*/
public function getMethods()
{
return new PHP_Depend_Code_NodeIterator($this->methods);
if (is_array($this->_methods)) {
return new PHP_Depend_Code_NodeIterator($this->_methods);
}

$methods = (array) $this->cache
->type('methods')
->restore($this->getUUID());

foreach ($methods as $method) {
$method->sourceFile = $this->sourceFile;
$method->setParent($this);
}

return new PHP_Depend_Code_NodeIterator($methods);
}

/**
Expand All @@ -288,7 +314,7 @@ public function addMethod(PHP_Depend_Code_Method $method)
{
$method->setParent($this);

$this->methods[] = $method;
$this->_methods[] = $method;

return $method;
}
Expand Down Expand Up @@ -332,7 +358,7 @@ protected function getTraitMethods()
/**
* Returns an <b>array</b> with all tokens within this type.
*
* @return array(PHP_Depend_Token)
* @return PHP_Depend_Token[]
*/
public function getTokens()
{
Expand All @@ -344,7 +370,7 @@ public function getTokens()
/**
* Sets the tokens for this type.
*
* @param array(PHP_Depend_Token) $tokens The generated tokens.
* @param PHP_Depend_Token[] $tokens The generated tokens.
*
* @return void
*/
Expand Down Expand Up @@ -451,12 +477,19 @@ public abstract function getAllMethods();
*/
public function __sleep()
{
if (is_array($this->_methods)) {
$this->cache
->type('methods')
->store($this->uuid, $this->_methods);

$this->_methods = null;
}

return array(
'cache',
'context',
'docComment',
'endLine',
'methods',
'modifiers',
'name',
'nodes',
Expand All @@ -478,11 +511,7 @@ public function __sleep()
*/
public function __wakeup()
{
$this->cached = true;

foreach ($this->methods as $method) {
$method->sourceFile = $this->sourceFile;
$method->setParent($this);
}
$this->cached = true;
$this->_methods = null;
}
}
7 changes: 0 additions & 7 deletions src/main/php/PHP/Depend/Code/Class.php
Expand Up @@ -74,13 +74,6 @@ class PHP_Depend_Code_Class extends PHP_Depend_Code_AbstractClassOrInterface
*/
private $_properties = null;

/**
* The modifiers for this class instance.
*
* @var integer $_modifiers
*/
protected $modifiers = 0;

/**
* Returns <b>true</b> if this is an abstract class or an interface.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHP/Depend/Code/Trait.php
Expand Up @@ -77,7 +77,7 @@ public function getAllMethods()
{
$methods = $this->getTraitMethods();

foreach ($this->methods as $method) {
foreach ($this->getMethods() as $method) {
$methods[strtolower($method->getName())] = $method;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHP/Depend/Util/Cache/Driver.php
Expand Up @@ -67,7 +67,7 @@ interface PHP_Depend_Util_Cache_Driver
/**
* The current cache version.
*/
const VERSION = '@version:035395f82dc6bb34f55db6e155139980:@';
const VERSION = '@version:8735e5eed8b182a60337f39b38d9cec8:@';

/**
* Sets the type for the next <em>store()</em> or <em>restore()</em> method
Expand Down
46 changes: 46 additions & 0 deletions src/main/php/PHP/Depend/Util/Cache/Driver/Memory.php
Expand Up @@ -90,6 +90,28 @@ class PHP_Depend_Util_Cache_Driver_Memory implements PHP_Depend_Util_Cache_Drive
*/
protected $type = self::ENTRY_TYPE;

/**
* Unique identifier within the same cache instance.
*
* @var string
*/
protected $staticId = null;

/**
* Global stack, mainly used during testing.
*
* @var array
*/
protected static $staticCache = array();

/**
* Instantiates a new in memory cache instance.
*/
public function __construct()
{
$this->staticId = sha1(uniqid(rand(0, PHP_INT_MAX)));
}

/**
* Sets the type for the next <em>store()</em> or <em>restore()</em> method
* call. A type is something like a namespace or group for cache entries.
Expand Down Expand Up @@ -182,4 +204,28 @@ protected function getCacheKey($key)

return "{$key}.{$type}";
}

/**
* PHP's magic serialize sleep method.
*
* @return array
* @since 1.0.2
*/
public function __sleep()
{
self::$staticCache[$this->staticId] = $this->cache;

return array('staticId');
}

/**
* PHP's magic serialize wakeup method.
*
* @return void
* @since 1.0.2
*/
public function __wakeup()
{
$this->cache = self::$staticCache[$this->staticId];
}
}
9 changes: 9 additions & 0 deletions src/site/docx/changes.xml
Expand Up @@ -9,6 +9,15 @@
</properties>

<body>
<release version="1.0.2"
date=""
description="" >

<action date="" dev="mapi" issue="24702477" system="pivotaltracker" type="add" due-to="bluszcz">
Huge memory footprint
</action>
</release>

<release version="1.0.1"
date="2012/02/08"
description="This release fixes two bugs in PHP_Depend's
Expand Down
96 changes: 96 additions & 0 deletions src/test/php/PHP/Depend/AbstractTest.php
Expand Up @@ -441,6 +441,102 @@ protected function createPDependFixture()
return new PHP_Depend($this->createConfigurationFixture());
}

/**
* Creates a ready to use class fixture.
*
* @param string $name Optional class name.
*
* @return PHP_Depend_Code_Class
* @since 1.0.2
*/
protected function createClassFixture($name = null)
{
$name = $name ? $name : get_class($this);

$class = new PHP_Depend_Code_Class($name);
$class->setSourceFile(new PHP_Depend_Code_File($GLOBALS['argv'][0]));
$class->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
$class->setContext($this->getMock('PHP_Depend_Builder_Context'));

return $class;
}

/**
* Creates a ready to use interface fixture.
*
* @param string $name Optional interface name.
*
* @return PHP_Depend_Code_Interface
* @since 1.0.2
*/
protected function createInterfaceFixture($name = null)
{
$name = $name ? $name : get_class($this);

$interface = new PHP_Depend_Code_Interface($name);
$interface->setSourceFile(new PHP_Depend_Code_File($GLOBALS['argv'][0]));
$interface->setCache(new PHP_Depend_Util_Cache_Driver_Memory());

return $interface;
}

/**
* Creates a ready to use trait fixture.
*
* @param string $name Optional trait name.
*
* @return PHP_Depend_Code_Trait
* @since 1.0.2
*/
protected function createTraitFixture($name = null)
{
$name = $name ? $name : get_class($this);

$trait = new PHP_Depend_Code_Trait($name);
$trait->setCache(new PHP_Depend_Util_Cache_Driver_Memory());

return $trait;
}

/**
* Creates a ready to use function fixture.
*
* @param string $name Optional function name.
*
* @return PHP_Depend_Code_Function
* @since 1.0.2
*/
protected function createFunctionFixture($name = null)
{
$name = $name ? $name : get_class($this);

$function = new PHP_Depend_Code_Function($name);
$function->setSourceFile(new PHP_Depend_Code_File($GLOBALS['argv'][0]));
$function->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
$function->addChild(new PHP_Depend_Code_ASTFormalParameters());

return $function;
}

/**
* Creates a ready to use method fixture.
*
* @param string $name Optional method name.
*
* @return PHP_Depend_Code_Method
* @since 1.0.2
*/
protected function createMethodFixture($name = null)
{
$name = $name ? $name : get_class($this);

$method = new PHP_Depend_Code_Method($name);
$method->setCache(new PHP_Depend_Util_Cache_Driver_Memory());
$method->addChild(new PHP_Depend_Code_ASTFormalParameters());

return $method;
}

/**
* Creates a temporary resource for the given file name.
*
Expand Down

0 comments on commit 75c9755

Please sign in to comment.