Skip to content

Commit

Permalink
explicitly enable simdjson parsing with an argument to the JsonRpc se…
Browse files Browse the repository at this point in the history
…rver
  • Loading branch information
1ma committed Aug 11, 2019
1 parent 602b3c9 commit 6ed839b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Internal/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ private function __construct($data, int $error)
}
}

public static function fromString(string $raw): Input
public static function fromString(string $raw, bool $simdJson): Input
{
if (!\extension_loaded('simdjson')) {
if (!$simdJson || !\extension_loaded('simdjson')) {
return new self(\json_decode($raw), \json_last_error());
}

Expand Down
10 changes: 8 additions & 2 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ class Server
*/
private $batchLimit;

public function __construct(ContainerInterface $container, int $batchLimit = null)
/**
* @var bool
*/
private $simdJson;

public function __construct(ContainerInterface $container, int $batchLimit = null, bool $simdJson = false)
{
$this->container = $container;
$this->simdJson = $simdJson;
$this->batchLimit = $batchLimit;
$this->methods = [];
$this->middlewares = [];
Expand Down Expand Up @@ -72,7 +78,7 @@ public function attach(string $serviceId): Server
*/
public function run(string $raw): ?string
{
$input = Input::fromString($raw);
$input = Input::fromString($raw, $this->simdJson);

if (!$input->parsable()) {
return self::end(Error::parsing());
Expand Down
43 changes: 39 additions & 4 deletions tests/Unit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class InputTest extends TestCase
*/
public function testValidInputs(string $raw): void
{
$sut = Input::fromString($raw);
$sut = Input::fromString($raw, false);

self::assertTrue($sut->parsable());
self::assertEquals($sut->data(), \json_decode($raw));
Expand All @@ -36,7 +36,18 @@ public function validInputsProvider(): array
*/
public function testInputParseError(string $raw): void
{
$sut = Input::fromString($raw);
$sut = Input::fromString($raw, false);

self::assertFalse($sut->parsable());
self::assertNull($sut->data());

self::assertFalse($sut->isArray());

if (!self::simdJsonSupport()) {
return;
}

$sut = Input::fromString($raw, true);

self::assertFalse($sut->parsable());
self::assertNull($sut->data());
Expand All @@ -49,7 +60,7 @@ public function parseErrorProvider(): array
return [
[''],
['}"jsonrpc":"2.0'],
[random_bytes(6)]
[\random_bytes(6)]
];
}

Expand All @@ -58,12 +69,24 @@ public function parseErrorProvider(): array
*/
public function testInvalidInputs(string $raw): void
{
$sut = Input::fromString($raw);
$sut = Input::fromString($raw, false);

self::assertTrue($sut->parsable());
self::assertEquals($sut->data(), \json_decode($raw));

self::assertFalse($sut->isArray());

if (!self::simdJsonSupport()) {
return;
}

$sut = Input::fromString($raw, true);

self::assertTrue($sut->parsable());
self::assertEquals($sut->data(), \json_decode($raw));

self::assertFalse($sut->isArray());

}

public function invalidInputsProvider(): array
Expand All @@ -78,4 +101,16 @@ public function invalidInputsProvider(): array
'empty array' => ['[]']
];
}

/**
* Return whether the PHP installation on which the tests are running has the
* simdjson extension loaded, is running on Linux and the machine has either
* the AVX2 or SSE4.2 instruction sets.
*/
private static function simdJsonSupport(): bool
{
return \extension_loaded('simdjson')
&& 'Linux' === PHP_OS
&& (null !== \shell_exec('grep avx2 /proc/cpuinfo') || null !== \shell_exec('grep sse4_2 /proc/cpuinfo'));
}
}

0 comments on commit 6ed839b

Please sign in to comment.