Skip to content

Commit 67b0cf7

Browse files
committed
FOUR-25161 Implement SIMDJSON for json_encode
## Description Implements support for https://github.com/awesomized/simdjson-plus-php-ext. ## Related Tickets & Packages https://processmaker.atlassian.net/browse/FOUR-25161
1 parent 1019cb2 commit 67b0cf7

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

‎ProcessMaker/Support/JsonOptimizer.php‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,30 @@ class JsonOptimizer
1111
*/
1212
public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0)
1313
{
14-
if (extension_loaded('simdjson') && config('app.json_optimization') === true) {
14+
if (extension_loaded('simdjson_plus') && config('app.json_optimization') === true) {
1515
try {
16-
return \SimdJson\decode($json, $assoc, $depth, $options);
16+
return simdjson_decode($json, $assoc, $depth, $options);
1717
} catch (\Throwable $e) {
18-
Log::warning("simdjson failed: {$e->getMessage()}");
18+
Log::warning("simdjson_decode failed: {$e->getMessage()}");
1919
}
2020
}
2121

2222
return json_decode($json, $assoc, $depth, $options);
2323
}
2424

2525
/**
26-
* Uses the native json_encode function (simdjson doesn't support encoding).
26+
* Encodes a JSON using simdjson if available.
2727
*/
2828
public static function encode(mixed $value, int $flags = 0, int $depth = 512): string|false
2929
{
30+
if (extension_loaded('simdjson_plus') && config('app.json_optimization') === true) {
31+
try {
32+
return simdjson_encode($value, $flags, $depth);
33+
} catch (\Throwable $e) {
34+
Log::warning("simdjson_encode failed: {$e->getMessage()}");
35+
}
36+
}
37+
3038
return json_encode($value, $flags, $depth);
3139
}
3240
}

‎tests/unit/ProcessMaker/JsonOptimizerTest.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function test_it_falls_back_to_native_decode_on_invalid_json()
5050

5151
public function test_it_uses_simdjson_when_available()
5252
{
53-
if (!extension_loaded('simdjson')) {
53+
if (!extension_loaded('simdjson_plus')) {
5454
$this->markTestSkipped('SIMDJSON extension not loaded.');
5555
}
5656

@@ -111,7 +111,7 @@ public function test_helper_respects_config_setting()
111111
*/
112112
public function test_helper_uses_simdjson_when_enabled()
113113
{
114-
if (!extension_loaded('simdjson')) {
114+
if (!extension_loaded('simdjson_plus')) {
115115
$this->markTestSkipped('SIMDJSON extension not available');
116116
}
117117

@@ -127,6 +127,8 @@ public function test_helper_uses_simdjson_when_enabled()
127127
*/
128128
public function test_it_encodes_data_correctly()
129129
{
130+
config(['app.json_optimization' => true]);
131+
130132
$encoded = JsonOptimizer::encode($this->data);
131133

132134
$this->assertIsString($encoded);
@@ -142,6 +144,8 @@ public function test_it_encodes_data_correctly()
142144
*/
143145
public function test_helper_encodes_data_correctly()
144146
{
147+
config(['app.json_optimization' => true]);
148+
145149
$encoded = json_optimize_encode($this->data);
146150

147151
$this->assertIsString($encoded, 'The encoded result should be a string.');

0 commit comments

Comments
 (0)