Skip to content

Commit

Permalink
Use simd extension for json decode and tune performance in core Netwo…
Browse files Browse the repository at this point in the history
…rk Request
  • Loading branch information
donhardman committed Jan 16, 2025
1 parent 79c175c commit ad7a039
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/ManticoreSearch/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public function getSettings(): Settings {
protected function fetchSettings(): Settings {
$resp = $this->sendRequest('SHOW SETTINGS');
/** @var array{0:array{columns:array<mixed>,data:array{Setting_name:string,Value:string}}} */
$data = (array)json_decode($resp->getBody(), true);
$data = (array)simdjson_decode($resp->getBody(), true);
$settings = new Vector();
foreach ($data[0]['data'] as ['Setting_name' => $key, 'Value' => $value]) {
// If the key is plugin_dir check env first and after choose
Expand Down Expand Up @@ -442,7 +442,7 @@ protected function fetchSettings(): Settings {
// Gather variables also
$resp = $this->sendRequest('SHOW VARIABLES');
/** @var array{0:array{columns:array<mixed>,data:array{Setting_name:string,Value:string}}} */
$data = (array)json_decode($resp->getBody(), true);
$data = (array)simdjson_decode($resp->getBody(), true);
foreach ($data[0]['data'] as ['Variable_name' => $key, 'Value' => $value]) {
$settings->push(
new Map(
Expand Down
7 changes: 4 additions & 3 deletions src/Network/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public static function default(string $id = '0'): static {
* @param string $id
* @return static
*/

public static function fromString(string $data, string $id = '0'): static {
$self = new static;
$self->id = $id;
Expand Down Expand Up @@ -155,7 +156,7 @@ public static function validateOrFail(string $data): array {
* message:array{path_query:string,body:string},
* version:int} $result
*/
$result = json_decode($data, true, 512, JSON_INVALID_UTF8_SUBSTITUTE);
$result = simdjson_decode($data, true, 512);
if (!is_array($result)) {
throw new InvalidNetworkRequestError('Invalid request payload is passed');
}
Expand Down Expand Up @@ -234,11 +235,11 @@ protected function parseOrFail(array $payload): static {
$this->path = $path;
$this->format = $format;
$this->endpointBundle = $endpointBundle;
$this->mySQLTool = static::detectMySQLTool($payload['message']['body']);
$this->mySQLTool = $format === RequestFormat::SQL ? static::detectMySQLTool($payload['message']['body']) : null;
$this->payload = (in_array($endpointBundle, [Endpoint::Elastic, Endpoint::Bulk]))
? trim($payload['message']['body'])
: static::removeComments($payload['message']['body']);
$this->command = strtok(strtolower($this->payload), ' ') ?: '';
$this->command = strtolower(strtok($this->payload, ' ') ?: '');
$this->error = $payload['error']['message'];
$this->errorBody = $payload['error']['body'] ?? [];
$this->version = match ($payload['version']) {
Expand Down
7 changes: 3 additions & 4 deletions src/Network/Struct.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ public function addBigIntField(string $field): void {
* @return bool
*/
public static function isValid(string $json): bool {
// TODO: replace with json_validate once we more to 8.3
$result = json_decode($json, true, static::JSON_DEPTH, static::JSON_FLAGS);
return !!$result;
return simdjson_is_valid($json);
}

/**
Expand All @@ -123,9 +121,10 @@ public static function isValid(string $json): bool {
*/
public static function fromJson(string $json): self {
/** @var array<TKey, TValue> */
$result = (array)json_decode($json, true, static::JSON_DEPTH, static::JSON_FLAGS);
$result = (array)simdjson_decode($json, true, static::JSON_DEPTH);
$bigIntFields = [];
if (static::hasBigInt($json)) {
// We need here to keep original json decode cuzit has bigIntFields
/** @var array<TKey, TValue> */
$modified = json_decode($json, true, static::JSON_DEPTH, static::JSON_FLAGS | JSON_BIGINT_AS_STRING);
static::traverseAndTrack($modified, $result, $bigIntFields);
Expand Down
6 changes: 3 additions & 3 deletions src/Plugin/Pluggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public function getList(): array {
return [];
}
/** @var array{require?:array<string,string>} $composerJson */
$composerJson = json_decode($composerContent, true);
$composerJson = simdjson_decode($composerContent, true);
if (!isset($composerJson['require'])) {
return [];
}
Expand Down Expand Up @@ -274,7 +274,7 @@ public function getClassNamespaceByFullName(string $name): string {
throw new Exception("Failed to get contents of composer.json for plugin: $name");
}

$composerJson = json_decode($composerContent, true);
$composerJson = simdjson_decode($composerContent, true);
if (!$composerJson) {
throw new Exception("Failed to decode contents of composer.json file for plugin: $name");
}
Expand Down Expand Up @@ -465,7 +465,7 @@ public function fetchLocalPlugins(): array {
throw new Exception("Failed to read composer file for plugin: $shortName");
}
/** @var array{name:?string} */
$composer = json_decode($composerContent, true);
$composer = simdjson_decode($composerContent, true);
if (!isset($composer['name'])) {
throw new Exception("Failed to detect local plugin name from file: $composerFile");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tool/KeyboardLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ protected static function getLangMap(): array {
}

/** @var array<string,array<string>> $langMap */
$langMap = json_decode($configContent, true);
$langMap = simdjson_decode($configContent, true);
if (!is_array($langMap)) {
throw new Exception("Invalid keyboard layout config file at '$configPath'");
}
Expand Down
1 change: 0 additions & 1 deletion src/Tool/SqlQueryParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ private static function getCreator(): PHPSQLCreator {
* @throws GenericError
*/
public static function parse(string $payload, Closure $preProcessorCallback, mixed $args): ?array {

$result = (bool)call_user_func($preProcessorCallback, $args);
if ($result === false) {
return null;
Expand Down
6 changes: 3 additions & 3 deletions test/src/Trait/TestFunctionalTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,13 @@ protected static function runHttpQuery(

/** @var array<int,array{error:string,data:array<int,array<string,string>>,total?:string,columns?:string}> $result */
$result = match ($path) {
'cli_json', 'sql', 'sql?mode=raw' => (array)json_decode(implode(PHP_EOL, $output), true),
'cli_json', 'sql', 'sql?mode=raw' => (array)simdjson_decode(implode(PHP_EOL, $output), true),
'cli' => [
['columns' => implode(PHP_EOL, $output), 'data' => [], 'error' => ''],
],
// assuming Elastic-like endpoint is passed
default => [
['data' => [(array)json_decode($output[0] ?? '{}', true)], 'error' => ''],
['data' => [(array)simdjson_decode($output[0] ?? '{}', true)], 'error' => ''],
],
};
print_r($output);
Expand Down Expand Up @@ -361,7 +361,7 @@ protected static function runHttpBuddyRequest(
$redirect = $redirectOutput ? '2>&1' : '';
exec("curl -s 127.0.0.1:$port -H 'Content-type: application/json' -d @$payloadFile $redirect", $output);
/** @var array{version:int,type:string,message:array<int,array{columns:array<string>,data:array<int,array<string,string>>}>} $result */
$result = (array)json_decode($output[0] ?? '{}', true);
$result = (array)simdjson_decode($output[0] ?? '{}', true);
return $result;
}

Expand Down

0 comments on commit ad7a039

Please sign in to comment.