Skip to content

Commit 1d375e6

Browse files
authored
get rid of JsonPathFinder and Boxes (#58)
* Revert "61 regex perf (#62)" This reverts commit a07c7b6. * try to get rid of JsonPathFinder and Boxes - JsonPathFinder interface does not really benefit from storing the json or path internally - trying to get rid of the Box<> that is used inside of JsonPathFinder * some clippy fixes * add benches for find slicing equal bench with reuse time: [510.30 ns 512.16 ns 514.26 ns] equal bench without reuse time: [21.436 µs 21.456 µs 21.479 µs] regex bench with reuse time: [58.875 µs 58.925 µs 58.975 µs] regex bench without reuse time: [85.324 µs 85.416 µs 85.517 µs] JsonPathInst generation time: [23.988 µs 24.019 µs 24.052 µs]
1 parent c1e3221 commit 1d375e6

File tree

13 files changed

+358
-669
lines changed

13 files changed

+358
-669
lines changed

CHANGELOG.md

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
* **`0.1.0`**
2-
* Initial implementation
3-
* **`0.1.1`**
4-
* Technical improvements
5-
* **`0.1.2`**
6-
* added a trait to obtain the result from value
7-
* added a method to get the cloned as Value
8-
* change the name of the general method*
9-
* **`0.1.4`**
10-
* add an ability to use references instead of values
11-
* fix some clippy issues
12-
* **`0.1.5`**
13-
* correct grammar for `$.[..]`
14-
* **`0.1.6`**
15-
* add logical OR and logical And to filters
16-
* fix bugs with objects in filters
17-
* add internal macros to generate path objects
18-
* **`0.2.0`**
19-
* add json path value as a result for the library
20-
* add functions (size)
21-
* change a logical operator `size` into function `size()`
22-
* **`0.2.1`**
23-
* changed the contract for length() function.
24-
* **`0.2.2`**
25-
* add ..*
26-
* **`0.2.5`**
27-
* build for tags
28-
* **`0.2.6`**
29-
* make parser mod public
30-
* **`0.3.0`**
31-
* introduce the different behaviour for empty results and non-existing result
32-
* **`0.3.2`**
33-
* make jsonpath inst cloneable.
34-
* **`0.3.3`**
35-
* fix a bug with the logical operators
36-
* **`0.3.4`**
37-
* add a result as a path
38-
* **`0.3.5`**
39-
* add `!` negation operation in filters
40-
* allow using () in filters
41-
* **`0.5`**
42-
* add config for jsonpath
43-
* add an option to add a regex cache for boosting performance
44-
* **`0.5.1`**
45-
* add double quotes for the expressions (before it was only possible to use single quotes)
46-
* add Debug on the JsonPathFinder
47-
48-
1+
- **`0.1.0`**
2+
- Initial implementation
3+
- **`0.1.1`**
4+
- Technical improvements
5+
- **`0.1.2`**
6+
- added a trait to obtain the result from value
7+
- added a method to get the cloned as Value
8+
- change the name of the general method\*
9+
- **`0.1.4`**
10+
- add an ability to use references instead of values
11+
- fix some clippy issues
12+
- **`0.1.5`**
13+
- correct grammar for `$.[..]`
14+
- **`0.1.6`**
15+
- add logical OR and logical And to filters
16+
- fix bugs with objects in filters
17+
- add internal macros to generate path objects
18+
- **`0.2.0`**
19+
- add json path value as a result for the library
20+
- add functions (size)
21+
- change a logical operator `size` into function `size()`
22+
- **`0.2.1`**
23+
- changed the contract for length() function.
24+
- **`0.2.2`**
25+
- add ..\*
26+
- **`0.2.5`**
27+
- build for tags
28+
- **`0.2.6`**
29+
- make parser mod public
30+
- **`0.3.0`**
31+
- introduce the different behaviour for empty results and non-existing result
32+
- **`0.3.2`**
33+
- make jsonpath inst cloneable.
34+
- **`0.3.3`**
35+
- fix a bug with the logical operators
36+
- **`0.3.4`**
37+
- add a result as a path
38+
- **`0.3.5`**
39+
- add `!` negation operation in filters
40+
- allow using () in filters
41+
- **`0.5`**
42+
- add config for jsonpath
43+
- add an option to add a regex cache for boosting performance
44+
- **`0.5.1`**
45+
- add double quotes for the expressions (before it was only possible to use single quotes)
46+
- add Debug on the JsonPathFinder
47+
- **`0.6`**
48+
- allow to reuse regex, that improves performance without needing an internal cache

Cargo.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "jsonpath-rust"
33
description = "The library provides the basic functionality to find the set of the data according to the filtering query."
4-
version = "0.5.1"
4+
version = "0.6.0"
55
authors = ["BorisZhguchev <[email protected]>"]
66
edition = "2018"
77
license-file = "LICENSE"
@@ -17,12 +17,15 @@ regex = "1"
1717
pest = "2.0"
1818
pest_derive = "2.0"
1919
thiserror = "1.0.50"
20-
lazy_static = "1.4"
21-
once_cell = "1.19.0"
2220

2321
[dev-dependencies]
22+
lazy_static = "1.0"
2423
criterion = "0.5.1"
2524

2625
[[bench]]
27-
name = "regex_bench"
28-
harness = false
26+
name = "regex"
27+
harness = false
28+
29+
[[bench]]
30+
name = "equal"
31+
harness = false

README.md

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -389,50 +389,7 @@ fn test() {
389389
** If the value has been modified during the search, there is no way to find a path of a new value.
390390
It can happen if we try to find a length() of array, for in stance.**
391391

392-
## Configuration
393392

394-
The JsonPath provides a wat to configure the search by using `JsonPathConfig`.
395-
396-
```rust
397-
pub fn main() {
398-
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
399-
}
400-
```
401-
402-
### Regex cache
403-
The configuration provides an ability to use a regex cache to improve the [performance](https://github.com/besok/jsonpath-rust/issues/61)
404-
405-
To instantiate the cache needs to use `RegexCache` enum with the implementation of the trait `RegexCacheInst`.
406-
Default implementation `DefaultRegexCacheInst` uses `Arc<Mutex<HashMap<String,Regex>>>`.
407-
The pair of Box<Value> or Value and config can be used:
408-
```rust
409-
pub fn main(){
410-
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
411-
let json = Box::new(json!({
412-
"author":"abcd(Rees)",
413-
}));
414-
415-
let _v = (json, cfg).path("$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]")
416-
.expect("the path is correct");
417-
418-
419-
}
420-
```
421-
or using `JsonPathFinder` :
422-
423-
```rust
424-
fn main() {
425-
let cfg = JsonPathConfig::new(RegexCache::Implemented(DefaultRegexCacheInst::default()));
426-
let finder = JsonPathFinder::from_str_with_cfg(
427-
r#"{"first":{"second":[{"active":1},{"passive":1}]}}"#,
428-
"$.first.second[?(@.active)]",
429-
cfg,
430-
).unwrap();
431-
let slice_of_data: Vec<&Value> = finder.find_slice();
432-
let js = json!({"active":1});
433-
assert_eq!(slice_of_data, vec![JsonPathValue::Slice(&js, "$.first.second[0]".to_string())]);
434-
}
435-
```
436393

437394
## The structure
438395

benches/equal.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
3+
use serde_json::json;
4+
use std::str::FromStr;
5+
6+
struct SearchData {
7+
json: serde_json::Value,
8+
path: JsonPathInst,
9+
}
10+
11+
const PATH: &'static str = "$.[?(@.author == 'abcd(Rees)')]";
12+
13+
fn equal_perf_test_with_reuse(cfg: &SearchData) {
14+
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
15+
}
16+
17+
fn equal_perf_test_without_reuse() {
18+
let json = Box::new(json!({
19+
"author":"abcd(Rees)",
20+
}));
21+
22+
let _v = json.path(PATH).expect("the path is correct");
23+
}
24+
25+
pub fn criterion_benchmark(c: &mut Criterion) {
26+
let data = SearchData {
27+
json: json!({
28+
"author":"abcd(Rees)",
29+
}),
30+
path: JsonPathInst::from_str(PATH).unwrap(),
31+
};
32+
c.bench_function("equal bench with reuse", |b| {
33+
b.iter(|| equal_perf_test_with_reuse(&data))
34+
});
35+
c.bench_function("equal bench without reuse", |b| {
36+
b.iter(|| equal_perf_test_without_reuse())
37+
});
38+
}
39+
40+
criterion_group!(benches, criterion_benchmark);
41+
criterion_main!(benches);

benches/regex.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
use criterion::{criterion_group, criterion_main, Criterion};
2+
use jsonpath_rust::{JsonPathInst, JsonPathQuery};
3+
use serde_json::json;
4+
use std::str::FromStr;
5+
6+
struct SearchData {
7+
json: serde_json::Value,
8+
path: JsonPathInst,
9+
}
10+
11+
const PATH: &'static str = "$.[?(@.author ~= '.*(?i)d\\(Rees\\)')]";
12+
13+
fn regex_perf_test_with_reuse(cfg: &SearchData) {
14+
let _v = jsonpath_rust::find(&cfg.path, &cfg.json);
15+
}
16+
17+
fn regex_perf_test_without_reuse() {
18+
let json = Box::new(json!({
19+
"author":"abcd(Rees)",
20+
}));
21+
22+
let _v = json.path(PATH).expect("the path is correct");
23+
}
24+
25+
fn json_path_inst_compiling() {
26+
let _v = JsonPathInst::from_str(PATH).unwrap();
27+
}
28+
29+
pub fn criterion_benchmark(c: &mut Criterion) {
30+
let data = SearchData {
31+
json: json!({
32+
"author":"abcd(Rees)",
33+
}),
34+
path: JsonPathInst::from_str(PATH).unwrap(),
35+
};
36+
c.bench_function("regex bench with reuse", |b| {
37+
b.iter(|| regex_perf_test_with_reuse(&data))
38+
});
39+
c.bench_function("regex bench without reuse", |b| {
40+
b.iter(|| regex_perf_test_without_reuse())
41+
});
42+
c.bench_function("JsonPathInst generation", |b| {
43+
b.iter(|| json_path_inst_compiling())
44+
});
45+
}
46+
47+
criterion_group!(benches, criterion_benchmark);
48+
criterion_main!(benches);

benches/regex_bench.rs

Lines changed: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)