Skip to content

Commit

Permalink
Merge pull request #8 from luizperes/proper-fix-issue-6
Browse files Browse the repository at this point in the history
Proper fix for issue #6
  • Loading branch information
luizperes authored Mar 4, 2019
2 parents 2e1a6e3 + 4e397c0 commit 411bf5b
Show file tree
Hide file tree
Showing 11 changed files with 969 additions and 36 deletions.
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
# simdjson_nodejs
> Node.js bindings for [simdjson](https://github.com/lemire/simdjson), a gigabytes-per-second JSON parser, possibly the fastest JSON parser at the moment. *simdjson* works on popular platforms such as OS X, Linux, and Windows and the Node.js bindings up-to-date have only been tested on OS X.
> Node.js bindings for [simdjson](https://github.com/lemire/simdjson), a gigabytes-per-second JSON parser, possibly the fastest JSON parser at the moment. *simdjson* as well as *simdjson_nodejs* work on popular platforms such as OS X, Linux, and Windows.
## Installation
The installation can be done in one step with `npm`:

`npm install simdjson`

### AVX2

*simdjson* requires AVX2 support to function. Check to see if your OS/processor supports it:

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

## Usage

##### Check if a JSON string is valid:
```Javascript
const simdjson = require('simdjson');

const jsonString = ...
const valid = simdjson.isValid(jsonString) // true
const valid = simdjson.isValid(jsonString); // true
```

##### Parsing a JSON string
```Javascript
const simdjson = require('simdjson');

const jsonString = ...
const parsedJSON = simdjson.parse(jsonString) // parsed JSON object
const parsedJSON = simdjson.parse(jsonString); // parsed JSON object
```

## Benchmarks
Expand All @@ -46,7 +39,7 @@ node benchmark
```


| filename | JSON file | simdjson file |
| filename | default JSON | simdjson |
| :---------------: | :------------: | :-------------: |
| apache_builds.json | 0.0007187123801716652 | 0.00013120465355748363 |
| canada.json | 0.023433125946428573 | 0.004240565511370573 |
Expand Down Expand Up @@ -102,3 +95,11 @@ You may run the benchmarks by running the commands:

###### Observation:
Please refer to the the original repository benchmarks for more information about the performance of *simdjson* [https://github.com/lemire/simdjson](https://github.com/lemire/simdjson).


### AVX2

*simdjson* will choose the default JS `JSON` library in case that your machine does not have AVX2 support. Therefore, it is required AVX2 support in order to use all of its powers. You may want to check whether your OS/processor supports it:

- OS X: `sysctl -a | grep machdep.cpu.leaf7_features`
- Linux: `grep avx2 /proc/cpuinfo`
68 changes: 48 additions & 20 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -1,21 +1,49 @@
{
"targets": [{
"target_name": "simdjson",
"default_configuration": "Release",
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions", "-std=gnu++0x", "-std=gnu++1y" ],
"cflags_cc+": ["-march=native", "-std=c++17"],
"sources": [
"simdjson/main.cpp",
"simdjson/bindings.cpp",
"simdjson/src/simdjson.cpp"
],
'include_dirs': [
"<!@(node -p \"require('node-addon-api').include\")"
],
'dependencies': [
"<!(node -p \"require('node-addon-api').gyp\")"
],
'defines': [ 'NAPI_DISABLE_CPP_EXCEPTIONS' ]
}]
}
"targets": [
{
"target_name": "simdjson",
"default_configuration": "Release",
"cflags": ["-O3", "-std=c99"],
"cflags_cc": ["-O3", "-std=c++17"],
"sources": [
"simdjson/main.cpp",
"simdjson/nonavx2.cpp"
],
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
}
],
"conditions": [
[
"target_arch in \"x64 x86_64\"",
{
"targets": [
{
"target_name": "simdjson-avx2",
"default_configuration": "Release",
"cflags!": ["-fno-exceptions"],
"cflags_cc!": ["-O3", "-fno-exceptions", "-std=gnu++0x", "-std=gnu++1y", "-mavx2", "-mavx", "-mbmi", "-mpclmul"],
"cflags_cc+": ["-O3", "-march=native", "-std=c++17", "-mavx2", "-mavx", "-mbmi", "-mpclmul"],
"sources": [
"simdjson/main.cpp",
"simdjson/src/simdjson.cpp",
"simdjson/bindings.cpp"
],
"xcode_settings": {
"GCC_ENABLE_SSE42_EXTENSIONS": "YES",
"CLANG_X86_VECTOR_INSTRUCTIONS": "avx2",
"OTHER_CFLAGS": ["-mavx2", "-mavx", "-mbmi", "-mpclmul"],
},
"msvs_settings": {
"VCCLCompilerTool": {
"AdditionalOptions": ["/arch:AVX2", "/mavx2", "/mavx", "/mbmi", "/mpclmul"]
}
},
"include_dirs": ["<!@(node -p \"require('node-addon-api').include\")"],
"defines": ["NAPI_DISABLE_CPP_EXCEPTIONS"]
}
]
}
]
]
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const simdjson = require('./build/Release/simdjson.node');
const simdjson = require('./lib/simdjsontarget');
module.exports = simdjson;
8 changes: 8 additions & 0 deletions lib/moduleexists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function moduleExists(modulePath) {
try {
return !!require.resolve(modulePath)
} catch (e) {
return false
}
}
module.exports = moduleExists;
30 changes: 30 additions & 0 deletions lib/simdjsontarget.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const os = require('os');
const moduleExists = require('./moduleexists');

const nodePaths = {
AVX2: '../build/Release/simdjson-avx2.node',
PLAIN: '../build/Release/simdjson.node'
};

let path = nodePaths.PLAIN;
if (moduleExists(nodePaths.AVX2) && os.arch() === 'x64') {
const simdjson = require(nodePaths.AVX2);
if (simdjson && simdjson.hasAVX2()) {
path = nodePaths.AVX2;
}
}

if (path === nodePaths.PLAIN) {
const simdjson = JSON;
simdjson["isValid"] = (str) => {
try {
simdjson.parse(str);
} catch (e) {
return false;
}
return true;
};
module.exports = simdjson;
} else {
module.exports = require(nodePaths.AVX2);
}
Loading

0 comments on commit 411bf5b

Please sign in to comment.