minor #25299 [Serializer] improved CsvEncoder::decode performance (Roman Orlov)

This PR was merged into the 3.4 branch.

Discussion
----------

[Serializer] improved CsvEncoder::decode performance

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT

Improved CsvEncoder::decode performance by caching duplicate count calls.
Blackfire profiles before and after change tested on collection of 10000 elements:
[before] https://blackfire.io/profiles/9c08f789-cd29-4eae-92c8-046e3849a2b8/graph
[after] https://blackfire.io/profiles/a17bfb6b-ef82-41ee-9edd-9403f829d6ab/graph

Commits
-------

3b910a9fad [Serializer] improved CsvEncoder::decode performance by caching duplicate count calls
This commit is contained in:
Fabien Potencier 2017-12-07 10:36:42 -08:00
commit 5d7576cf84

View File

@ -115,6 +115,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$headers = null;
$nbHeaders = 0;
$headerCount = array();
$result = array();
list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
@ -126,7 +127,9 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$nbHeaders = $nbCols;
foreach ($cols as $col) {
$headers[] = explode($keySeparator, $col);
$header = explode($keySeparator, $col);
$headers[] = $header;
$headerCount[] = count($header);
}
continue;
@ -134,7 +137,7 @@ class CsvEncoder implements EncoderInterface, DecoderInterface
$item = array();
for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) {
$depth = count($headers[$i]);
$depth = $headerCount[$i];
$arr = &$item;
for ($j = 0; $j < $depth; ++$j) {
// Handle nested arrays