Skip to content

Commit 093fd57

Browse files
committed
README: Update
1 parent 1bf9b92 commit 093fd57

File tree

1 file changed

+53
-33
lines changed

1 file changed

+53
-33
lines changed

README.md

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
# simdjson_php
22

3-
🚀 Super fast JSON encoding and decoding for PHP that utilise [simdjson project](https://github.com/lemire/simdjson).
3+
🚀 Blazing-fast JSON encoding and decoding for PHP, powered by the [simdjson project](https://github.com/lemire/simdjson).
44

55
*This is a fork of [crazyxman/simdjson_php](https://github.com/crazyxman/simdjson_php) with new optimisations and encoding support.*
66

77
[![Build Status](https://github.com/JakubOnderka/simdjson_php/actions/workflows/integration.yml/badge.svg?branch=master)](https://github.com/JakubOnderka/simdjson_php/actions/workflows/integration.yml?query=branch%3Amaster)
88

9-
## How fast is simdjson_php?
9+
## Performance Comparison: How Fast is simdjson_php?
1010

11-
* Decoding is 3× faster compared to PHP's `json_decode()`
12-
* Encoding is 2.5× faster compared to PHP's `json_encode()`
13-
* Validation is 6× faster compared to PHP's `json_validate()`
11+
| Operation | PHP Built-in | simdjson_php | Speedup |
12+
|-----------------------|--------------|--------------|----------|
13+
| Decode to array | 1.48 ms | 0.49 ms | **3.0×** |
14+
| Decode to object | 1.59 ms | 0.69 ms | **2.3×** |
15+
| Encode | 0.67 ms | 0.26 ms | **2.5×** |
16+
| Encode (pretty print) | 0.83 ms | 0.31 ms | **2.6×** |
17+
| Validate | 1.37 ms | 0.22 ms | **6.2×** |
1418

15-
| Method | Original | simdjson_php | Speedup |
16-
|---------------------|----------|--------------|----------|
17-
| Decode to array | 1.48 ms | 0.49 ms | **3.0×** |
18-
| Decode to object | 1.59 ms | 0.69 ms | **2.3×** |
19-
| Encode | 0.67 ms | 0.26 ms | **2.5×** |
20-
| Encode pretty print | 0.83 ms | 0.31 ms | **2.6×** |
21-
| Validate | 1.37 ms | 0.22 ms | **6.2×** |
19+
Tests were conducted using PHP 8.3 on an [Apple M1 Max](https://en.wikipedia.org/wiki/Apple_M1#M1_Pro_and_M1_Max). For test specification see `TwitterDecodeBench.php` and `TwitterEncoderBench.php`.
2220

23-
Using PHP 8.3 on [Apple M1 Max](https://en.wikipedia.org/wiki/Apple_M1#M1_Pro_and_M1_Max), for test specification see `TwitterDecodeBench.php` and `TwitterEncoderBench.php`.
24-
Memory usage is also reduced when decoding JSON compared to `json_decode()`, as array keys are deduplicated. When decoding [`twitter.json`](jsonexamples/twitter.json), memory usage decrees from 3.01 MB to 2.47 MB.
21+
Additionally, simdjson_php reduces memory usage compared to `json_decode()`. For example, when decoding twitter.json, memory consumption drops from 3.01 MB to 2.47 MB due to efficient array key deduplication.
2522

2623
## Requirement
2724

28-
- PHP 8.0+ (PHP 8.2+ recommended for maximum performance)
29-
- Prerequisites: g++ (version 7 or better) or clang++ (version 6 or better), and a 64-bit system with a command-line shell (e.g., Linux, macOS, FreeBSD).
25+
* PHP 8.0+ (PHP 8.2+ recommended for maximum performance)
26+
* g++ (version 7 or better) or clang++ (version 6 or better)
27+
* A 64-bit system with a command-line shell (e.g., Linux, macOS, FreeBSD)
3028

31-
## Compile simdjson_php in Linux
32-
```
33-
$ phpize
34-
$ ./configure
35-
$ make
36-
$ make test
37-
$ make install
38-
```
29+
## Compilation Instructions for Linux
3930

40-
Add the following line to your php.ini
31+
To compile simdjson_php, run the following commands:
4132

33+
```bash
34+
phpize
35+
./configure
36+
make
37+
make test
38+
make install
4239
```
40+
41+
Once installed, add this line to your `php.ini` file:
42+
43+
```ini
4344
extension=simdjson.so
4445
```
4546

46-
## Usage
47+
## Usage Examples
4748
```php
4849
$jsonString = <<<'JSON'
4950
{
@@ -66,7 +67,7 @@ JSON;
6667
$isValid = simdjson_validate($jsonString); //return bool
6768
var_dump($isValid); // true
6869

69-
// Parsing a JSON string. similar to the json_decode() function but without the fourth argument
70+
// Parsing a JSON string. Similar to the json_decode() function but without the fourth argument
7071
try {
7172
// returns array|stdClass|string|float|int|bool|null.
7273
$parsedJSON = simdjson_decode($jsonString, true, 512);
@@ -110,18 +111,18 @@ var_dump($res) //int(5)
110111

111112
Most of available options of default `json_encode()` method are not supported by `simdjson_encode()` as they are usually useless.
112113

113-
`simdjson_encode($value)` method has very similar behaviour as `json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)`
114+
`simdjson_encode($value)` method has similar behaviour as `json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR)`
114115

115116
Supported options are:
116-
* `SIMDJSON_PRETTY_PRINT` - use whitespace in returned data to format it.
117+
* `SIMDJSON_PRETTY_PRINT` - use whitespace in returned data to format it
117118
* `SIMDJSON_INVALID_UTF8_SUBSTITUTE` - convert invalid UTF-8 characters to `\0xfffd` (Unicode Character 'REPLACEMENT CHARACTER' �)
118119
* `SIMDJSON_INVALID_UTF8_IGNORE` - ignore invalid UTF-8 characters
119-
* `SIMDJSON_APPEND_NEWLINE` - append new line character (`\n`) to end of encoded string. This is useful when encoding multiple objects to JSONL format as PHP strigns are immutable.
120+
* `SIMDJSON_APPEND_NEWLINE` - append new line character (`\n`) to end of encoded string. This is useful when encoding data to JSONL format as PHP strings are immutable.
120121

121122
Differences are:
122123
* uses different algorithm to convert floating-point number to string, so string format can be slightly different
123124
* even when `JSON_UNESCAPED_UNICODE` is enabled, PHP `json_encode()` escapes some Unicode chars that do not need to be escaped. `simdjson_encode()` escape just Unicode chars that needs to be escaped by JSON spec.
124-
* `simdjson_encode_to_stream()` method allows you to write encoded string directly to PHP stream
125+
* simdjson will throw `SimdJsonEncoderException` exception in case of error
125126

126127
### Base64 encoding
127128

@@ -135,9 +136,19 @@ $fileContentEncoded = new SimdJsonBase64Encode($fileContent);
135136
simdjson_encode(['image' => $fileContentEncoded]); // returns {"image":"TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu..."}
136137
```
137138

138-
## Decoder edge cases
139+
### Encode to stream
140+
141+
For large data sets, simdjson_php provides the `simdjson_encode_to_stream()` function to save data directly to a file or output buffer.
142+
143+
```php
144+
$bigStructure = [...];
145+
simdjson_encode_to_stream($bigStructure, fopen("file.json", "w")); // save to file.json
146+
simdjson_encode_to_stream($bigStructure, fopen("php://output", "w")); // send to output buffer
147+
```
148+
149+
## Decoder
139150

140-
There are some differences from `json_decode()` due to the implementation of the underlying simdjson library. This will throw a RuntimeException if simdjson rejects the JSON.
151+
There are some differences from `json_decode()` due to the implementation of the underlying simdjson library. This will throw a `SimdJsonDecoderException` if simdjson rejects the JSON.
141152

142153
Note that the simdjson PECL is using a fork of the simdjson C library to imitate php's handling of integers and floats in JSON.
143154

@@ -150,5 +161,14 @@ Note that the simdjson PECL is using a fork of the simdjson C library to imitate
150161
For typical use cases, this shouldn't matter.
151162
(e.g. `simdjson_decode('[[]]', true, 2)` will succeed but `json_decode('[[]]', true, 2)` and `simdjson_decode('[[1]]', true, 2)` will fail.)
152163

164+
### Decode from stream
165+
166+
If you need to decode a big file from JSON format that you want to save to a file or send to a user, you can use the `simdjson_decode_from_stream` method.
167+
168+
```php
169+
simdjson_decode_from_stream(fopen("file.json", "r")); // load from file.json
170+
simdjson_decode_from_stream(fopen("php://input", "r")); // send by user
171+
```
172+
153173
## Benchmarks
154174
See the [benchmark](./benchmark) folder for more benchmarks.

0 commit comments

Comments
 (0)