bug #23413 [VarDumper] Reduce size of serialized Data objects (nicolas-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
-------

70bd2bc41b [VarDumper] Reduce size of serialized Data objects
This commit is contained in:
Fabien Potencier 2017-07-05 16:24:26 +03:00
commit 3e84f7d91d
5 changed files with 76 additions and 5 deletions

View File

@ -414,7 +414,7 @@ class ErrorHandler
$errorAsException = self::$silencedErrorCache[$message]; $errorAsException = self::$silencedErrorCache[$message];
++$errorAsException->count; ++$errorAsException->count;
} else { } 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); $errorAsException = new SilencedErrorContext($type, $file, $line, $lightTrace);
} }

View File

@ -120,6 +120,7 @@ class LoggerDataCollector extends DataCollector implements LateDataCollectorInte
$log['priorityName'] = 'DEBUG'; $log['priorityName'] = 'DEBUG';
$log['channel'] = '-'; $log['channel'] = '-';
$log['scream'] = false; $log['scream'] = false;
unset($log['type'], $log['file'], $log['line'], $log['trace'], $log['trace'], $log['count']);
$logs[] = $log; $logs[] = $log;
} }
@ -251,7 +252,7 @@ class LoggerDataCollector extends DataCollector implements LateDataCollectorInte
} }
foreach ($containerDeprecationLogs as $deprecationLog) { foreach ($containerDeprecationLogs as $deprecationLog) {
$count['deprecation_count'] += $deprecationLog['count']; $count['deprecation_count'] += $deprecationLog['context']['exception']->count;
} }
ksort($count['priorities']); ksort($count['priorities']);

View File

@ -551,7 +551,7 @@ abstract class Kernel implements KernelInterface, TerminableInterface
return; 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. // Clean the trace by removing first frames added by the error handler itself.
for ($i = 0; isset($backtrace[$i]); ++$i) { for ($i = 0; isset($backtrace[$i]); ++$i) {
if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) { if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {

View File

@ -50,7 +50,7 @@ class DumpDataCollectorTest extends TestCase
); );
$this->assertEquals($xDump, $dump); $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(0, $collector->getDumpsCount());
$this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize()); $this->assertSame('a:2:{i:0;b:0;i:1;s:5:"UTF-8";}', $collector->serialize());
} }

View File

@ -16,7 +16,7 @@ use Symfony\Component\VarDumper\Caster\Caster;
/** /**
* @author Nicolas Grekas <p@tchwork.com> * @author Nicolas Grekas <p@tchwork.com>
*/ */
class Data implements \ArrayAccess, \Countable, \IteratorAggregate class Data implements \ArrayAccess, \Countable, \IteratorAggregate, \Serializable
{ {
private $data; private $data;
private $position = 0; private $position = 0;
@ -278,6 +278,57 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate
$this->dumpItem($dumper, new Cursor(), $refs, $this->data[$this->position][$this->key]); $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. * Depth-first dumping of items.
* *
@ -406,4 +457,23 @@ class Data implements \ArrayAccess, \Countable, \IteratorAggregate
return $hashCut; 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;
}
} }