Skip to content

Commit

Permalink
Slight reader optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
pcrov committed Mar 13, 2018
1 parent 3353ed4 commit 8baeee5
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/JsonReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class JsonReader
/* Options */
const FLOATS_AS_STRINGS = 0b00000001;

private static $endTypeMap = [
self::ARRAY => self::END_ARRAY,
self::OBJECT => self::END_OBJECT
];

/**
* @var Parser|null
*/
Expand All @@ -36,6 +41,11 @@ class JsonReader
*/
private $cache = [];

/**
* @var bool A fresh cache that hasn't been entered can be dropped entirely when calling next().
*/
private $cacheIsFresh = false;

/**
* @var int bit field of reader options
*/
Expand Down Expand Up @@ -128,14 +138,19 @@ public function value()
$type = $this->type;
$value = &$this->value;

if ($value === null && ($type === self::ARRAY || $type === self::OBJECT)) {
$value = $this->buildTree($type, empty($this->cache));
}

if ($type === self::NUMBER) {
return $this->castNumber($value);
}

if ($value === null && ($type === self::ARRAY || $type === self::OBJECT)) {
if (empty($this->cache)) {
$value = $this->buildTree($type, true);
$this->cacheIsFresh = true;
} else {
$value = $this->buildTree($type, false);
}
}

return $value;
}

Expand All @@ -153,8 +168,13 @@ public function next(string $target = null): bool
throw new Exception("Load data before trying to read.");
}

if ($this->cacheIsFresh) {
$this->cache = [];
$this->cacheIsFresh = false;
}

$currentDepth = $this->depth;
$endType = $this->getEndType($this->type);
$endType = self::$endTypeMap[$this->type] ?? self::NONE;

while ($result = $this->read()) {
if ($this->depth <= $currentDepth) {
Expand Down Expand Up @@ -196,6 +216,7 @@ public function read(string $target = null): bool
$node = $parser->read();
} else {
$node = \array_shift($this->cache);
$this->cacheIsFresh = false;
}

if ($node === null) {
Expand Down Expand Up @@ -237,7 +258,7 @@ private function buildTree(string $type, bool $writeCache): array

$parser = $this->parser;
$cache = &$this->cache;
$end = $this->getEndType($type);
$endType = self::$endTypeMap[$type] ?? self::NONE;
$result = [];

while (true) {
Expand All @@ -250,16 +271,14 @@ private function buildTree(string $type, bool $writeCache): array
}
list ($type, $name, $value) = $node;

if ($type === $end) {
if ($type === $endType) {
break;
}

if ($type === self::ARRAY || $type === self::OBJECT) {
$value = $this->buildTree($type, $writeCache);
}

if ($type === self::NUMBER) {
$value = $this->castNumber($value);
} elseif ($type === self::ARRAY || $type === self::OBJECT) {
$value = $this->buildTree($type, $writeCache);
}

if ($name !== null) {
Expand All @@ -284,24 +303,13 @@ private function castNumber(string $number)
return $cast;
}

private function getEndType(string $type): string
{
switch ($type) {
case self::ARRAY:
return self::END_ARRAY;
case self::OBJECT:
return self::END_OBJECT;
default:
return self::NONE;
}
}

private function resetNode()
{
$this->type = self::NONE;
$this->name = null;
$this->value = null;
$this->depth = 0;
$this->cache = [];
$this->cacheIsFresh = false;
}
}

0 comments on commit 8baeee5

Please sign in to comment.