Skip to content

Commit

Permalink
minor #25299 [Serializer] improved CsvEncoder::decode performance (Ro…
Browse files Browse the repository at this point in the history
…man 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
-------

3b910a9 [Serializer] improved CsvEncoder::decode performance by caching duplicate count calls
  • Loading branch information
fabpot committed Dec 7, 2017
2 parents 1aa06b8 + 3b910a9 commit 5d7576c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Symfony/Component/Serializer/Encoder/CsvEncoder.php
Expand Up @@ -115,6 +115,7 @@ public function decode($data, $format, array $context = array())

$headers = null;
$nbHeaders = 0;
$headerCount = array();
$result = array();

list($delimiter, $enclosure, $escapeChar, $keySeparator) = $this->getCsvOptions($context);
Expand All @@ -126,15 +127,17 @@ public function decode($data, $format, array $context = array())
$nbHeaders = $nbCols;

foreach ($cols as $col) {
$headers[] = explode($keySeparator, $col);
$header = explode($keySeparator, $col);
$headers[] = $header;
$headerCount[] = count($header);
}

continue;
}

$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
Expand Down

0 comments on commit 5d7576c

Please sign in to comment.