Skip to content

[VarDumper] Reduce size of serialized Data objects #23413

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 5, 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
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function handleError($type, $message, $file, $line)
$errorAsException = self::$silencedErrorCache[$message];
++$errorAsException->count;
} else {
$lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), $type, $file, $line, false) : array();
$lightTrace = $this->tracedErrors & $type ? $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3), $type, $file, $line, false) : array();
$errorAsException = new SilencedErrorContext($type, $file, $line, $lightTrace);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ private function getContainerDeprecationLogs()
$log['priorityName'] = 'DEBUG';
$log['channel'] = '-';
$log['scream'] = false;
unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
$logs[] = $log;
}

Expand Down Expand Up @@ -251,7 +252,7 @@ private function computeErrorsCount(array $containerDeprecationLogs)
}

foreach ($containerDeprecationLogs as $deprecationLog) {
$count['deprecation_count'] += $deprecationLog['count'];
$count['deprecation_count'] += $deprecationLog['context']['exception']->count;
}

ksort($count['priorities']);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ protected function initializeContainer()
return;
}

$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
// Clean the trace by removing first frames added by the error handler itself.
for ($i = 0; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testDump()
);
$this->assertEquals($xDump, $dump);

$this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
$this->assertStringMatchesFormat('a:3:{i:0;a:5:{s:4:"data";%c:39:"Symfony\Component\VarDumper\Cloner\Data":%a', $collector->serialize());
$this->assertSame(0, $collector->getDumpsCount());
$this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize());
}
Expand Down
72 changes: 71 additions & 1 deletion 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
class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializable
{
private $data;
private $position = 0;
Expand Down Expand Up @@ -278,6 +278,57 @@ public function dump(DumperInterface $dumper)
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]);
}

/**
* @internal
*/
public function serialize()
{
$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 Down Expand Up @@ -406,4 +457,23 @@ private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCu

return $hashCut;
}

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

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

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

$stub = clone $stub;
$stub->type = $map[$stub->type];
$stub->class = isset($map[$stub->class]) ? $map[$stub->class] : $stub->class;

return $stub;
}
}