simdjsonphp-高速解析json

29次阅读

共计 2740 个字符,预计需要花费 7 分钟才能阅读完成。

介绍

simdjson_php(https://github.com/crazyxman/…,它绑定 simdjson 来实现快速解析,simdjson 是一个高速的 json 解析器,它使用了大多数 SIMD 单一指令。simdjson 介绍:https://github.com/lemire/sim…

环境依赖

  • php7+
  • 带有 AVX2 的处理器(即,2013 年发布的 Haswell 微体系结构的 Intel 处理器和 2017 年发布的 Zen 微体系结构的 AMD 处理器),大多数 cpu 都是支持的
  • 最近的 C ++ 编译器(例如,GNU GCC 或 LLVM CLANG 或 Visual Studio 2017),我们假设 C ++ 17。GNU GCC 7 或更高版本或 LLVM 的 clang 6 或更高版本
  • 检查操作系统 / 处理器是否支持它:

    • OS X: sysctl -a | grep machdep.cpu.leaf7_features
    • Linux: grep avx2 /proc/cpuinfo

使用简介

  1. 当需要获取一个较大 json 串中的某个 key 时 使用 simdjson_key_value() 是比较合适的,不像 json_decode() 把整个 json 串解析成数组,开辟不必要的内存,当然在性能上是略逊于 hash 查找的。
  2. 当验证一个字符串是否为 json 时 simdjson_isvaild() 是比较合适的,并且是非常快的,同样不需要通过 json_decode() 来验证。
// 检查字符串是否为一个有效的 json:
$isValid = simdjson_isvalid($jsonString); //return bool

// 解析一个 json 字符串,返回数组,对象,null,类似 json_decode(),第三个参数为解析的深度
$parsedJSON = simdjson_decode($jsonString, true, 512); //return array|object|null. "null" string is not a standard json

/*
{
  "Image": {
    "Width":  800,
    "Height": 600,
    "Title":  "View from 15th Floor",
    "Thumbnail": {
      "Url":    "http://www.example.com/image/481989943",
      "Height": 125,
      "Width":  100
    },
    "Animated" : false,
    "IDs": [116, 943, 234, 38793, {"p": "30"}]
  }
}
*/

// 注意. "\t" 是一个分割符. 它必须是一个控制字符. 它用来分割对象的 key 或数组的下标
// 例如. "Image\tThumbnail\tUrl" 是正确. 'Image\tThumbnail\tUrl' 是错误的


// 根据 json 串获取指定 key 的值
$value = simdjson_key_value($jsonString, "Image\tThumbnail\tUrl");
var_dump($value); // string(38) "http://www.example.com/image/481989943"

$value = simdjson_key_value($jsonString, "Image\tIDs\t4", true);
var_dump($value); 
/*
array(1) {["p"]=>
  string(2) "30"
}
*/

// 获取 json 解析后的资源,只解析一次,后续使用不再解析
$resource = simdjson_resource($jsonString);
// 根据 json 资源获取指定 key 的值
$value = simdjson_key_value($resource, "Image\tThumbnail\tUrl");
var_dump($value); // string(38) "http://www.example.com/image/481989943"

$value = simdjson_key_value($resource, "Image\tIDs\t4", true);
var_dump($value); 
/*
array(1) {["p"]=>
  string(2) "30"
}
*/

// 检查 key 是否存在,参数可以是一个 json 串也可以是一个 json 资源,返回 true,false,null。当第一个参数是字符串时返回 null 代表解析失败
$res = simdjson_key_exists($jsonString, "Image\tIDs\t1");
var_dump($res) //bool(true)
$res = simdjson_key_exists($resource, "Image\tIDs\t1");
var_dump($res) //bool(true)

性能测试(秒)

filename json_decode simdjson_decode simdjson_isvalid
apache_builds.json 0.00307300 0.00225200 0.00018100
canada.json 0.13955000 0.02773900 0.00358300
citm_catalog.json 0.03030900 0.01334000 0.00117000
github_events.json 0.00294100 0.00090400 0.00008500
gsoc-2018.json 0.04292500 0.01112000 0.00186700
instruments.json 0.00509700 0.00231800 0.00017500
marine_ik.json 0.09833600 0.04417500 0.00463400
mesh.json 0.01869200 0.00722600 0.00114800
mesh.pretty.json 0.03576200 0.00738100 0.00163400
numbers.json 0.00263600 0.00069900 0.00018200
random.json 0.01713500 0.00973900 0.00063000
twitter.json 0.01258600 0.00618400 0.00057400
twitterescaped.json 0.01435900 0.00650400 0.00074300
update-center.json 0.01506000 0.00869100 0.00047800

You may run the benchmarks by running the commands:

  • php benchmark/benchmark.php

如有不足之处下方留言即可,欢迎大家批评指正,我会虚心接纳。

正文完
 0