Skip to content

Commit

Permalink
bug #23413 [VarDumper] Reduce size of serialized Data objects (nicola…
Browse files Browse the repository at this point in the history
…s-grekas)

This PR was merged into the 3.3 branch.

Discussion
----------

[VarDumper] Reduce size of serialized Data objects

| Q             | A
| ------------- | ---
| Branch?       | 3.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | no
| Fixed tickets | #23233
| License       | MIT
| Doc PR        | -

By using `Stub` objects a lot, especially for arrays, `Data` objects generate heavy serialized strings.
By implemeting `Serializable` on `Data`, this PR removes most of the boilerplate.

This PR also removes duplicate data in `LoggerDataCollector`, and reduces the backtrace of silenced errors to their 3 last items - which should be enough - and is otherwise responsible for a significant portion of the serialized payloads.

This is not the last possible step towards shrinking serialized profiles, but the next one is more complex -and maybe this one is good enough? Please give feedback if you can.

Commits
-------

70bd2bc [VarDumper] Reduce size of serialized Data objects
  • Loading branch information
fabpot committed Jul 5, 2017
2 parents bef2142 + 70bd2bc commit 3e84f7d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Debug/ErrorHandler.php
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
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
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
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
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;
}
}

0 comments on commit 3e84f7d

Please sign in to comment.