mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-04-25 09:22:19 +00:00
first commit lua ffi
This commit is contained in:
parent
9276c0aa02
commit
ea7599c012
4
benchmark/benches_lua_vs_rust/.gitignore
vendored
Normal file
4
benchmark/benches_lua_vs_rust/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
.idea/*
|
||||||
|
.vscode
|
||||||
|
/target/
|
||||||
|
Cargo.lock
|
13
benchmark/benches_lua_vs_rust/Cargo.toml
Normal file
13
benchmark/benches_lua_vs_rust/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "lua_vs_rust_benchmark"
|
||||||
|
version = "0.1.0"
|
||||||
|
authors = ["Changseok Han <freestrings@gmail.com>"]
|
||||||
|
license = "MIT"
|
||||||
|
[dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = { version = "1.0", features = ["preserve_order"] }
|
||||||
|
jsonpath_lib = { path = "../../" }
|
||||||
|
|
||||||
|
[[bin]]
|
||||||
|
name = "lua_vs_rust_benchmark"
|
||||||
|
path = "example.rs"
|
25
benchmark/benches_lua_vs_rust/bench_lua_vs_rust.sh
Executable file
25
benchmark/benches_lua_vs_rust/bench_lua_vs_rust.sh
Executable file
@ -0,0 +1,25 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# http://luajit.org/index.html
|
||||||
|
|
||||||
|
cargo clean && \
|
||||||
|
cargo build --release
|
||||||
|
|
||||||
|
export JSONPATH_LIB_PATH="${PWD}/../../target/release"
|
||||||
|
export LUA_PATH="${PWD}/../../lua/?.lua;"
|
||||||
|
|
||||||
|
echo
|
||||||
|
time cargo run --release -- 1000
|
||||||
|
echo
|
||||||
|
time luajit example.lua 1000
|
||||||
|
echo
|
||||||
|
time cargo run --release -- 5000
|
||||||
|
echo
|
||||||
|
time luajit example.lua 5000
|
||||||
|
echo
|
||||||
|
time cargo run --release -- 10000
|
||||||
|
echo
|
||||||
|
time luajit example.lua 10000
|
||||||
|
|
20
benchmark/benches_lua_vs_rust/example.lua
Normal file
20
benchmark/benches_lua_vs_rust/example.lua
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
require("lib")
|
||||||
|
|
||||||
|
local iter;
|
||||||
|
if arg[1] == nil or arg[1] == '' then
|
||||||
|
iter = 5000;
|
||||||
|
else
|
||||||
|
iter = tonumber(arg[1]);
|
||||||
|
end
|
||||||
|
|
||||||
|
print(string.format("%s - %u", "lua iter", iter));
|
||||||
|
|
||||||
|
local file = io.open("../../benchmark/example.json", "r");
|
||||||
|
io.input(file)
|
||||||
|
local data = io.read("*a");
|
||||||
|
io.close(file);
|
||||||
|
local cb = compile("$..book[?(@.price<30 && @.category==\"fiction\")]");
|
||||||
|
for i = 0, iter do
|
||||||
|
local r = cb(data);
|
||||||
|
-- print(r);
|
||||||
|
end
|
46
benchmark/benches_lua_vs_rust/example.rs
Normal file
46
benchmark/benches_lua_vs_rust/example.rs
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
extern crate jsonpath_lib as jsonpath;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
|
|
||||||
|
use std::io::Read;
|
||||||
|
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
fn read_json(path: &str) -> String {
|
||||||
|
let mut f = std::fs::File::open(path).unwrap();
|
||||||
|
let mut contents = String::new();
|
||||||
|
f.read_to_string(&mut contents).unwrap();
|
||||||
|
contents
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_string() -> String {
|
||||||
|
read_json("../../benchmark/example.json")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_json() -> Value {
|
||||||
|
let string = get_string();
|
||||||
|
serde_json::from_str(string.as_str()).unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_path() -> &'static str {
|
||||||
|
r#"$..book[?(@.price<30 && @.category=="fiction")]"#
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let args: Vec<String> = std::env::args().collect();
|
||||||
|
let iter = if args.len() < 2 { 5000_usize } else { args[1].as_str().parse::<usize>().unwrap() };
|
||||||
|
|
||||||
|
println!("rust iter - {}", iter);
|
||||||
|
|
||||||
|
let json = get_json();
|
||||||
|
for _ in 0..iter {
|
||||||
|
let mut selector = jsonpath::Selector::default();
|
||||||
|
let _ = selector.str_path(get_path());
|
||||||
|
selector.value(&json);
|
||||||
|
let r = selector.select();
|
||||||
|
if r.is_err() {
|
||||||
|
panic!();
|
||||||
|
}
|
||||||
|
// println!("{:?}", serde_json::to_string(&r.expect("")).unwrap());
|
||||||
|
}
|
||||||
|
}
|
0
benchmark/benches_lua_vs_rust/lib.rs
Normal file
0
benchmark/benches_lua_vs_rust/lib.rs
Normal file
25
lua/lib.lua
Normal file
25
lua/lib.lua
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
local ffi = require('ffi')
|
||||||
|
|
||||||
|
local ext
|
||||||
|
|
||||||
|
if ffi.os == 'Linux' then
|
||||||
|
ext = 'so'
|
||||||
|
else
|
||||||
|
ext = 'dylib'
|
||||||
|
end
|
||||||
|
|
||||||
|
ffi.cdef[[
|
||||||
|
const char* ffi_select(const char *json_str, const char *path);
|
||||||
|
void *ffi_path_compile(const char *path);
|
||||||
|
const char* ffi_select_with_compiled(void *ptr, const char *json_str);
|
||||||
|
]]
|
||||||
|
|
||||||
|
local jsonpathLibPath = os.getenv("JSONPATH_LIB_PATH");
|
||||||
|
local jsonpath = ffi.load(jsonpathLibPath .. '/libjsonpath_lib.' .. ext);
|
||||||
|
|
||||||
|
function compile(path)
|
||||||
|
local compiledPath = jsonpath.ffi_path_compile(path);
|
||||||
|
return function(jsonStr)
|
||||||
|
return ffi.string(jsonpath.ffi_select_with_compiled(compiledPath, jsonStr));
|
||||||
|
end
|
||||||
|
end
|
70
src/lib.rs
70
src/lib.rs
@ -135,6 +135,8 @@ use serde_json::Value;
|
|||||||
pub use parser::Parser;
|
pub use parser::Parser;
|
||||||
pub use select::JsonPathError;
|
pub use select::JsonPathError;
|
||||||
pub use select::{Selector, SelectorMut};
|
pub use select::{Selector, SelectorMut};
|
||||||
|
use std::ffi::{CStr, CString};
|
||||||
|
use std::os::raw::{c_char, c_void};
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
mod parser;
|
mod parser;
|
||||||
@ -466,3 +468,71 @@ where
|
|||||||
let value = selector.str_path(path)?.value(value).replace_with(fun)?;
|
let value = selector.str_path(path)?.value(value).replace_with(fun)?;
|
||||||
Ok(value.take().unwrap_or(Value::Null))
|
Ok(value.take().unwrap_or(Value::Null))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn ffi_select(json_str: *const c_char, path: *const c_char) -> *const c_char {
|
||||||
|
let json_str = match unsafe { CStr::from_ptr(json_str) }.to_str() {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let path = match unsafe { CStr::from_ptr(path) }.to_str() {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match select_as_str(json_str, path) {
|
||||||
|
Ok(v) => match serde_json::to_string(&v) {
|
||||||
|
Ok(s) => {
|
||||||
|
let s = CString::new(s.as_str()).unwrap();
|
||||||
|
let p = s.as_ptr();
|
||||||
|
std::mem::forget(s);
|
||||||
|
p
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn ffi_path_compile(path: *const c_char) -> *mut c_void {
|
||||||
|
let path = match unsafe { CStr::from_ptr(path) }.to_str() {
|
||||||
|
Ok(s) => s,
|
||||||
|
Err(e) => {
|
||||||
|
panic!("{:?}", e);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Box::into_raw(Box::new(Parser::compile(path).unwrap())) as *mut c_void
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn ffi_select_with_compiled(
|
||||||
|
path_ptr: *mut c_void,
|
||||||
|
json_ptr: *const c_char,
|
||||||
|
) -> *const c_char {
|
||||||
|
let node = unsafe { Box::from_raw(path_ptr as *mut parser::Node) };
|
||||||
|
let json_str = unsafe { CStr::from_ptr(json_ptr) }.to_str().expect("invalid 'json_ptr' input");
|
||||||
|
let json = serde_json::from_str(json_str).expect(&format!("invalid json string: {}", json_str));
|
||||||
|
|
||||||
|
let mut selector = Selector::default();
|
||||||
|
let found = selector.compiled_path(&node).value(&json).select().unwrap();
|
||||||
|
std::mem::forget(node);
|
||||||
|
|
||||||
|
let result = serde_json::to_string(&found).expect(&format!("json serialize error: {:?}", found));
|
||||||
|
let result_cstring = CString::new(result.as_str()).expect(&format!("empty result: {:?}", result));
|
||||||
|
let result_ptr = result_cstring.as_ptr();
|
||||||
|
std::mem::forget(result_cstring);
|
||||||
|
result_ptr
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user