Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[VarDumper] Dont use Stub objects for arrays - lower GC pressure #23644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[VarDumper] Dont use Stub objects for arrays
  • Loading branch information
nicolas-grekas committed Jul 25, 2017
commit 0d5012d20ec42824b8e871f7de2d68200800e6bc
4 changes: 1 addition & 3 deletions 4 src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,16 +213,14 @@ public function cloneVar($var, $filter = 0)
gc_disable();
}
try {
$data = $this->doClone($var);
return new Data($this->doClone($var));
} finally {
if ($gc) {
gc_enable();
}
restore_error_handler();
$this->prevErrorHandler = null;
}

return new Data($data);
}

/**
Expand Down
97 changes: 24 additions & 73 deletions 97 src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializable
class Data implements \ArrayAccess, \Countable, \IteratorAggregate
{
private $data;
private $position = 0;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function getValue($recursive = false)
if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
$item = $item->value;
}
if (!$item instanceof Stub) {
if (!($item = $this->getStub($item)) instanceof Stub) {
return $item;
}
if (Stub::TYPE_STRING === $item->type) {
Expand All @@ -82,20 +82,20 @@ public function getValue($recursive = false)
$children = $item->position ? $this->data[$item->position] : array();

foreach ($children as $k => $v) {
if ($recursive && !$v instanceof Stub) {
if ($recursive && !($v = $this->getStub($v)) instanceof Stub) {
continue;
}
$children[$k] = clone $this;
$children[$k]->key = $k;
$children[$k]->position = $item->position;

if ($recursive) {
if ($v instanceof Stub && Stub::TYPE_REF === $v->type && $v->value instanceof Stub) {
if (Stub::TYPE_REF === $v->type && ($v = $this->getStub($v->value)) instanceof Stub) {
$recursive = (array) $recursive;
if (isset($recursive[$v->value->position])) {
if (isset($recursive[$v->position])) {
continue;
}
$recursive[$v->value->position] = true;
$recursive[$v->position] = true;
}
$children[$k] = $children[$k]->getValue($recursive);
}
Expand Down Expand Up @@ -123,7 +123,7 @@ public function getIterator()
public function __get($key)
{
if (null !== $data = $this->seek($key)) {
$item = $data->data[$data->position][$data->key];
$item = $this->getStub($data->data[$data->position][$data->key]);

return $item instanceof Stub || array() === $item ? $data : $item;
}
Expand Down Expand Up @@ -236,7 +236,7 @@ public function seek($key)
if ($item instanceof Stub && Stub::TYPE_REF === $item->type && !$item->position) {
$item = $item->value;
}
if (!$item instanceof Stub || !$item->position) {
if (!($item = $this->getStub($item)) instanceof Stub || !$item->position) {
return;
}
$keys = array($key);
Expand Down Expand Up @@ -278,57 +278,6 @@ public function dump(DumperInterface $dumper)
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
}

/**
* @internal
*/
public function serialize()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and we neither need implementing Serializable anymore: that adds to consumed CPU cycles, and the real issues (GC & size of serialized string) comes from arrays really.

{
$data = $this->data;

foreach ($data as $i => $values) {
foreach ($values as $k => $v) {
if ($v instanceof Stub) {
if (Stub::TYPE_ARRAY === $v->type) {
$v = self::mapStubConsts($v, false);
$data[$i][$k] = array($v->class, $v->position, $v->cut);
} else {
$v = self::mapStubConsts($v, false);
$data[$i][$k] = array($v->class, $v->position, $v->cut, $v->type, $v->value, $v->handle, $v->refCount, $v->attr);
}
}
}
}

return serialize(array($data, $this->position, $this->key, $this->maxDepth, $this->maxItemsPerDepth, $this->useRefHandles));
}

/**
* @internal
*/
public function unserialize($serialized)
{
list($data, $this->position, $this->key, $this->maxDepth, $this->maxItemsPerDepth, $this->useRefHandles) = unserialize($serialized);

foreach ($data as $i => $values) {
foreach ($values as $k => $v) {
if ($v && is_array($v)) {
$s = new Stub();
if (3 === count($v)) {
$s->type = Stub::TYPE_ARRAY;
$s = self::mapStubConsts($s, false);
list($s->class, $s->position, $s->cut) = $v;
$s->value = $s->cut + count($data[$s->position]);
} else {
list($s->class, $s->position, $s->cut, $s->type, $s->value, $s->handle, $s->refCount, $s->attr) = $v;
}
$data[$i][$k] = self::mapStubConsts($s, true);
}
}
}

$this->data = $data;
}

/**
* Depth-first dumping of items.
*
Expand All @@ -346,7 +295,10 @@ private function dumpItem($dumper, $cursor, &$refs, $item)

if (!$item instanceof Stub) {
$cursor->attr = array();
$type = gettype($item);
$type = \gettype($item);
if ($item && 'array' === $type) {
$item = $this->getStub($item);
}
} elseif (Stub::TYPE_REF === $item->type) {
if ($item->handle) {
if (!isset($refs[$r = $item->handle - (PHP_INT_MAX >> 1)])) {
Expand All @@ -360,7 +312,7 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
}
$cursor->attr = $item->attr;
$type = $item->class ?: gettype($item->value);
$item = $item->value;
$item = $this->getStub($item->value);
}
if ($item instanceof Stub) {
if ($item->refCount) {
Expand Down Expand Up @@ -458,21 +410,20 @@ private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCu
return $hashCut;
}

private static function mapStubConsts(Stub $stub, $resolve)
private function getStub($item)
{
static $stubConstIndexes, $stubConstValues;

if (null === $stubConstIndexes) {
$r = new \ReflectionClass(Stub::class);
$stubConstIndexes = array_flip(array_values($r->getConstants()));
$stubConstValues = array_flip($stubConstIndexes);
if (!$item || !\is_array($item)) {
return $item;
}

$map = $resolve ? $stubConstValues : $stubConstIndexes;

$stub = clone $stub;
$stub->type = $map[$stub->type];
$stub->class = isset($map[$stub->class]) ? $map[$stub->class] : $stub->class;
$stub = new Stub();
$stub->type = Stub::TYPE_ARRAY;
foreach ($item as $stub->class => $stub->position) {
}
if (isset($item[0])) {
$stub->cut = $item[0];
}
$stub->value = $stub->cut + \count($this->data[$stub->position]);

return $stub;
}
Expand Down
36 changes: 26 additions & 10 deletions 36 src/Symfony/Component/VarDumper/Cloner/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class Stub
class Stub implements \Serializable
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's implement Serializable on Stub instead of Data

{
const TYPE_REF = 'ref';
const TYPE_STRING = 'string';
const TYPE_ARRAY = 'array';
const TYPE_OBJECT = 'object';
const TYPE_RESOURCE = 'resource';
const TYPE_REF = 1;
const TYPE_STRING = 2;
const TYPE_ARRAY = 3;
const TYPE_OBJECT = 4;
const TYPE_RESOURCE = 5;

const STRING_BINARY = 'bin';
const STRING_UTF8 = 'utf8';
const STRING_BINARY = 1;
const STRING_UTF8 = 2;

const ARRAY_ASSOC = 'assoc';
const ARRAY_INDEXED = 'indexed';
const ARRAY_ASSOC = 1;
const ARRAY_INDEXED = 2;

public $type = self::TYPE_REF;
public $class = '';
Expand All @@ -38,4 +38,20 @@ class Stub
public $refCount = 0;
public $position = 0;
public $attr = array();

/**
* @internal
*/
public function serialize()
{
return \serialize(array($this->class, $this->position, $this->cut, $this->type, $this->value, $this->handle, $this->refCount, $this->attr));
}

/**
* @internal
*/
public function unserialize($serialized)
{
list($this->class, $this->position, $this->cut, $this->type, $this->value, $this->handle, $this->refCount, $this->attr) = \unserialize($serialized);
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.