Skip to content

Commit 9fbc87d

Browse files
committed
Few microoptimizations
1 parent 2586fca commit 9fbc87d

4 files changed

Lines changed: 11 additions & 13 deletions

File tree

src/FileChunks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class FileChunks implements \IteratorAggregate
1919
* @param string $fileName
2020
* @param int $chunkSize
2121
*/
22-
public function __construct($fileName, $chunkSize = 1024 * 8)
22+
public function __construct($fileName, $chunkSize = 1024 * 16)
2323
{
2424
$this->fileName = $fileName;
2525
$this->chunkSize = $chunkSize;

src/Tokens.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,7 @@ public function getIterator()
3737

3838
private function createGenerator(): Generator
3939
{
40-
$regex = '/
41-
[{}\[\],:]
42-
#| true|false|null
43-
#| [\deE.+-]+
44-
#| (t|tr|tru|f|fa|fal|fals|n|nu|nul)$
45-
| [^\xEF\xBB\xBF\s{}\[\],:]+ # todo make matching logic positive as in comments above and solve 2 failing tests
46-
/x';
40+
$regex = '/ [{}\[\],:] | [^\xEF\xBB\xBF\s{}\[\],:]+ /x';
4741

4842
$inString = 0;
4943
$carry = '';
@@ -58,13 +52,15 @@ private function createGenerator(): Generator
5852
if ($this->stringIsEscaping($chunkItems[$i])) {
5953
$carry .= $chunkItems[$i].'"';
6054
} else {
61-
yield '"'.$carry.$chunkItems[$i].'"';
55+
yield "\"$carry$chunkItems[$i]\"";
6256
$carry = '';
6357
$inString = 0;
6458
}
6559
} else {
6660
preg_match_all($regex, $chunkItems[$i], $matches);
67-
yield from $matches[0];
61+
foreach ($matches[0] as $match) {
62+
yield $match;
63+
}
6864
$inString = 1;
6965
}
7066
}
@@ -74,7 +70,9 @@ private function createGenerator(): Generator
7470
} else {
7571
preg_match_all($regex, $chunkItems[$i], $matches);
7672
$carry = array_pop($matches[0]);
77-
yield from $matches[0];
73+
foreach ($matches[0] as $match) {
74+
yield $match;
75+
}
7876
}
7977
}
8078

test/JsonMachineTest/TokensTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TokensTest extends \PHPUnit_Framework_TestCase
1919
public function bothDebugModes()
2020
{
2121
return [
22-
// 'debug enabled' => [TokensWithDebugging::class],
22+
'debug enabled' => [TokensWithDebugging::class],
2323
'debug disabled' => [Tokens::class],
2424
];
2525
}

test/performance/testPerformance.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ function createBigJsonFile()
7575
$separator = '';
7676
fputs($f, '[');
7777
// for ($i = 0; $i < 1; ++$i) {
78-
for ($i = 0; $i < 6000; ++$i) {
78+
for ($i = 0; $i < 3000; ++$i) {
7979
fputs($f, $separator);
8080
fputs($f, file_get_contents(__DIR__.'/twitter_example_'.($i % 2).'.json'));
8181
$separator = ",\n\n";

0 commit comments

Comments
 (0)