Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpKernel][VarDumper] Truncate profiler data & optim perf #23465

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 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\VarDumper\Caster\Caster;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Caster\CutStub;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Cloner\VarCloner;

/**
* Data collector for {@link FormInterface} instances.
Expand Down Expand Up @@ -77,11 +73,6 @@ class FormDataCollector extends DataCollector implements FormDataCollectorInterf
*/
private $valueExporter;

/**
* @var ClonerInterface
*/
private $cloner;

private $hasVarDumper;

public function __construct(FormDataExtractorInterface $dataExtractor)
Expand Down Expand Up @@ -255,61 +246,33 @@ public function serialize()
/**
* {@inheritdoc}
*/
protected function cloneVar($var, $isClass = false)
protected function getCasters()
{
if ($var instanceof Data) {
return $var;
}
if (null === $this->cloner) {
if ($this->hasVarDumper) {
$this->cloner = new VarCloner();
$this->cloner->setMaxItems(-1);
$this->cloner->addCasters(array(
'*' => function ($v, array $a, Stub $s, $isNested) {
foreach ($a as &$v) {
if (is_object($v) && !$v instanceof \DateTimeInterface) {
$v = new CutStub($v);
}
}

return $a;
},
\Exception::class => function (\Exception $e, array $a, Stub $s) {
if (isset($a[$k = "\0Exception\0previous"])) {
unset($a[$k]);
++$s->cut;
}

return $a;
},
FormInterface::class => function (FormInterface $f, array $a) {
return array(
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
);
},
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
return array(
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
);
},
));
} else {
@trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
$this->cloner = false;
}
}
if (false !== $this->cloner) {
return $this->cloner->cloneVar($var, Caster::EXCLUDE_VERBOSE);
}

if (null === $this->valueExporter) {
$this->valueExporter = new ValueExporter();
}
return parent::getCasters() + array(
\Exception::class => function (\Exception $e, array $a, Stub $s) {
foreach (array("\0Exception\0previous", "\0Exception\0trace") as $k) {
if (isset($a[$k])) {
unset($a[$k]);
++$s->cut;
}
}

return $this->valueExporter->exportValue($var);
return $a;
},
FormInterface::class => function (FormInterface $f, array $a) {
return array(
Caster::PREFIX_VIRTUAL.'name' => $f->getName(),
Caster::PREFIX_VIRTUAL.'type_class' => new ClassStub(get_class($f->getConfig()->getType()->getInnerType())),
);
},
ConstraintViolationInterface::class => function (ConstraintViolationInterface $v, array $a) {
Copy link
Member

Choose a reason for hiding this comment

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

should we add this caster in the new Validator panel in 3.4 too ?

Copy link
Member Author

Choose a reason for hiding this comment

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

that's for you @ogizanagi

Copy link
Contributor

@ogizanagi ogizanagi Jul 10, 2017

Choose a reason for hiding this comment

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

Sure, I'll check this once this is merged on 3.4.

return array(
Caster::PREFIX_VIRTUAL.'root' => $v->getRoot(),
Caster::PREFIX_VIRTUAL.'path' => $v->getPropertyPath(),
Caster::PREFIX_VIRTUAL.'value' => $v->getInvalidValue(),
);
},
);
}

private function &recursiveBuildPreliminaryFormTree(FormInterface $form, array &$outputByHash)
Expand Down
6 changes: 3 additions & 3 deletions src/Symfony/Component/Form/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/dependency-injection": "~3.3",
"symfony/config": "~2.7|~3.0",
"symfony/http-foundation": "~2.8|~3.0",
"symfony/http-kernel": "~2.8|~3.0",
"symfony/http-kernel": "^3.3.5",
"symfony/security-csrf": "~2.8|~3.0",
"symfony/translation": "~2.8|~3.0",
"symfony/var-dumper": "~3.3"
Expand All @@ -39,8 +39,8 @@
"symfony/dependency-injection": "<3.3",
"symfony/doctrine-bridge": "<2.7",
"symfony/framework-bundle": "<2.7",
"symfony/twig-bridge": "<2.7",
"symfony/var-dumper": "<3.3"
"symfony/http-kernel": "<3.3.5",
"symfony/twig-bridge": "<2.7"
},
"suggest": {
"symfony/validator": "For form validation.",
Expand Down
43 changes: 34 additions & 9 deletions src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
namespace Symfony\Component\HttpKernel\DataCollector;

use Symfony\Component\HttpKernel\DataCollector\Util\ValueExporter;
use Symfony\Component\VarDumper\Caster\ClassStub;
use Symfony\Component\VarDumper\Caster\CutStub;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Cloner\Data;
use Symfony\Component\VarDumper\Cloner\Stub;
use Symfony\Component\VarDumper\Cloner\VarCloner;

/**
Expand All @@ -37,7 +38,7 @@ abstract class DataCollector implements DataCollectorInterface, \Serializable
/**
* @var ClonerInterface
*/
private static $cloner;
private $cloner;

public function serialize()
{
Expand All @@ -61,24 +62,28 @@ public function unserialize($data)
*/
protected function cloneVar($var)
{
if (null === self::$cloner) {
if (class_exists(ClassStub::class)) {
self::$cloner = new VarCloner();
self::$cloner->setMaxItems(-1);
if ($var instanceof Data) {
return $var;
}
if (null === $this->cloner) {
if (class_exists(CutStub::class)) {
$this->cloner = new VarCloner();
$this->cloner->setMaxItems(-1);
$this->cloner->addCasters(self::getCasters());
} else {
@trigger_error(sprintf('Using the %s() method without the VarDumper component is deprecated since version 3.2 and won\'t be supported in 4.0. Install symfony/var-dumper version 3.2 or above.', __METHOD__), E_USER_DEPRECATED);
self::$cloner = false;
$this->cloner = false;
}
}
if (false === self::$cloner) {
if (false === $this->cloner) {
if (null === $this->valueExporter) {
$this->valueExporter = new ValueExporter();
}

return $this->valueExporter->exportValue($var);
}

return self::$cloner->cloneVar($var);
return $this->cloner->cloneVar($var);
}

/**
Expand All @@ -100,4 +105,24 @@ protected function varToString($var)

return $this->valueExporter->exportValue($var);
}

/**
* @return callable[] The casters to add to the cloner
*/
protected function getCasters()
{
return array(
'*' => function ($v, array $a, Stub $s, $isNested) {
Copy link
Member Author

@nicolas-grekas nicolas-grekas Jul 10, 2017

Choose a reason for hiding this comment

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

should be the main fix: nested objects are truncated, which prevent deep recursion into nested structures by default (eg "object" of the security decision log, requests attributes, etc.)

if (!$v instanceof Stub) {
foreach ($a as $k => $v) {
if (is_object($v) && !$v instanceof \DateTimeInterface && !$v instanceof Stub) {
$a[$k] = new CutStub($v);
}
}
}

return $a;
},
);
}
}
13 changes: 11 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/Caster.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,20 @@ public static function castObject($obj, $class, $hasDebugInfo = false)
}

if ($a) {
static $publicProperties = array();

$i = 0;
$prefixedKeys = array();
foreach ($a as $k => $v) {
if (isset($k[0]) && "\0" !== $k[0] && !property_exists($class, $k)) {
Copy link
Member Author

@nicolas-grekas nicolas-grekas Jul 10, 2017

Choose a reason for hiding this comment

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

as shown by profiles in linked issue, this is a hot code path and calling property_exists() should be prevented

$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
if (isset($k[0]) && "\0" !== $k[0]) {
if (!isset($publicProperties[$class])) {
foreach (get_class_vars($class) as $prop => $v) {
$publicProperties[$class][$prop] = true;
}
}
if (!isset($publicProperties[$class][$k])) {
$prefixedKeys[$i] = self::PREFIX_DYNAMIC.$k;
}
} elseif (isset($k[16]) && "\0" === $k[16] && 0 === strpos($k, "\0class@anonymous\0")) {
$prefixedKeys[$i] = "\0".get_parent_class($class).'@anonymous'.strrchr($k, "\0");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static function castSilencedErrorContext(SilencedErrorContext $e, array $
}

unset($a[$sPrefix.'file'], $a[$sPrefix.'line'], $a[$sPrefix.'trace']);
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace);
$a[Caster::PREFIX_VIRTUAL.'trace'] = new TraceStub($trace, self::$traceArgs);

return $a;
}
Expand Down Expand Up @@ -256,7 +256,7 @@ private static function filterExceptionArray($xClass, array $a, $xPrefix, $filte
$trace = array();
}

if (!($filter & Caster::EXCLUDE_VERBOSE)) {
if (!($filter & Caster::EXCLUDE_VERBOSE) && $trace) {
if (isset($a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line'])) {
self::traceUnshift($trace, $xClass, $a[Caster::PREFIX_PROTECTED.'file'], $a[Caster::PREFIX_PROTECTED.'line']);
}
Expand Down
16 changes: 9 additions & 7 deletions src/Symfony/Component/VarDumper/Cloner/AbstractCloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,17 @@ public function cloneVar($var, $filter = 0)
});
$this->filter = $filter;

if ($gc = gc_enabled()) {
gc_disable();
}
try {
$data = $this->doClone($var);
} catch (\Exception $e) {
}
restore_error_handler();
$this->prevErrorHandler = null;

if (isset($e)) {
throw $e;
} finally {
if ($gc) {
gc_enable();
}
restore_error_handler();
$this->prevErrorHandler = null;
}

return new Data($data);
Expand Down