Skip to content

Commit 5d7576c

Browse files
committed
minor symfony#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 ------- 3b910a9 [Serializer] improved CsvEncoder::decode performance by caching duplicate count calls
2 parents 1aa06b8 + 3b910a9 commit 5d7576c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public function decode($data, $format, array $context = array())
115115

116116
$headers = null;
117117
$nbHeaders = 0;
118+
$headerCount = array();
118119
$result = array();
119120

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

128129
foreach ($cols as $col) {
129-
$headers[] = explode($keySeparator, $col);
130+
$header = explode($keySeparator, $col);
131+
$headers[] = $header;
132+
$headerCount[] = count($header);
130133
}
131134

132135
continue;
133136
}
134137

135138
$item = array();
136139
for ($i = 0; ($i < $nbCols) && ($i < $nbHeaders); ++$i) {
137-
$depth = count($headers[$i]);
140+
$depth = $headerCount[$i];
138141
$arr = &$item;
139142
for ($j = 0; $j < $depth; ++$j) {
140143
// Handle nested arrays

0 commit comments

Comments
 (0)