Skip to content

Commit d2a5d90

Browse files
committed
refactoring done default
1 parent 9a35357 commit d2a5d90

File tree

20 files changed

+1513
-1172
lines changed

20 files changed

+1513
-1172
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
.vscode
33
!.idea/runConfigurations/
44
/target/
5-
Cargo.lock
5+
Cargo.lock
6+
callgrind.out.*

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ name = "jsonpath_lib"
3232
path = "src/lib.rs"
3333

3434
[profile.release]
35-
#debug = true
35+
debug = true
3636
#lto = false

benches/bench.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
#![feature(test)]
2+
extern crate bencher;
3+
extern crate indexmap;
24
extern crate jsonpath_lib as jsonpath;
35
extern crate serde;
46
extern crate serde_json;
57
extern crate test;
6-
extern crate bencher;
78

89
use std::io::Read;
910

11+
use serde::Serialize;
1012
use serde::Deserialize;
1113
use serde_json::Value;
1214

1315
use self::test::Bencher;
14-
use jsonpath::ref_value::model::RefValue;
15-
use serde::ser::Serialize;
16+
use jsonpath::ref_value::model::{RefValue, RefValueWrapper};
17+
use jsonpath::ref_value::ser::RefValueSerializer;
1618

1719
fn read_json(path: &str) -> String {
1820
let mut f = std::fs::File::open(path).unwrap();
@@ -107,22 +109,55 @@ fn bench_select_as(b: &mut Bencher) {
107109
}
108110

109111
#[bench]
110-
fn bench_serde_ser(b: &mut Bencher) {
112+
fn refval_de(b: &mut Bencher) {
113+
let json = get_json();
114+
b.iter(move || {
115+
for _ in 1..100 {
116+
let _ = RefValue::deserialize(&json).unwrap();
117+
}
118+
});
119+
}
120+
121+
#[bench]
122+
fn refval_se(b: &mut Bencher) {
123+
let json = get_json();
124+
b.iter(move || {
125+
for _ in 1..100 {
126+
let _ = &json.serialize(RefValueSerializer).unwrap();
127+
}
128+
});
129+
}
130+
131+
#[bench]
132+
fn refval_refcopy(b: &mut Bencher) {
133+
use std::ops::Deref;
134+
111135
let json = get_json();
136+
let ref_json: RefValue = json.serialize(RefValueSerializer).unwrap();
137+
let store = ref_json.get("store".to_string()).unwrap();
138+
let book = store.get("book".to_string()).unwrap();
112139

113140
b.iter(move || {
114141
for _ in 1..100 {
115-
let _: RefValue = json.serialize(jsonpath::ref_value::ser::Serializer).unwrap().into();
142+
if let RefValue::Array(vec) = book.deref() {
143+
let _: Vec<RefValueWrapper> = vec.iter().map(|v| v.clone()).collect();
144+
}
116145
}
117146
});
118147
}
119148

120149
#[bench]
121-
fn bench_serde_de(b: &mut Bencher) {
122-
let json_string = get_string();
123-
let json_str = json_string.as_str();
150+
fn refval_copy(b: &mut Bencher) {
151+
152+
let json = get_json();
153+
let store = json.get("store".to_string()).unwrap();
154+
let book = store.get("book".to_string()).unwrap();
124155

125-
b.iter(move || for _ in 1..100 {
126-
let _: RefValue = serde_json::from_str(json_str).unwrap();
156+
b.iter(move || {
157+
for _ in 1..100 {
158+
if let Value::Array(vec) = book {
159+
let _: Vec<Value> = vec.iter().map(|v| v.clone()).collect();
160+
}
161+
}
127162
});
128163
}

benches/bench_example.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#![feature(test)]
2+
3+
extern crate bencher;
4+
extern crate indexmap;
5+
extern crate jsonpath_lib as jsonpath;
6+
extern crate serde;
7+
extern crate serde_json;
8+
extern crate test;
9+
10+
use std::io::Read;
11+
12+
use serde_json::Value;
13+
14+
use self::test::Bencher;
15+
16+
fn read_json(path: &str) -> String {
17+
let mut f = std::fs::File::open(path).unwrap();
18+
let mut contents = String::new();
19+
f.read_to_string(&mut contents).unwrap();
20+
contents
21+
}
22+
23+
fn get_string() -> String {
24+
read_json("./benches/example.json")
25+
}
26+
27+
fn get_json() -> Value {
28+
let string = get_string();
29+
serde_json::from_str(string.as_str()).unwrap()
30+
}
31+
32+
fn get_path(i: usize) -> &'static str {
33+
let paths = vec![
34+
"$.store.book[*].author", //0
35+
"$..author", //1
36+
"$.store.*", //2
37+
"$.store..price", //3
38+
"$..book[2]", //4
39+
"$..book[-2]", //5
40+
"$..book[0,1]", //6
41+
"$..book[:2]", //7
42+
"$..book[1:2]", //8
43+
"$..book[-2:]", //9
44+
"$..book[2:]", //10
45+
"$..book[?(@.isbn)]", //11
46+
"$.store.book[?(@.price < 10)]", //12
47+
"$..*", //13
48+
"$..book[ ?( (@.price < 13 || $.store.bicycle.price < @.price) && @.price <=10 ) ]" //14
49+
];
50+
paths[i]
51+
}
52+
53+
macro_rules! example {
54+
($name:ident, $i:expr) => {
55+
#[bench]
56+
fn $name(b: &mut Bencher) {
57+
let json = get_json();
58+
b.iter(move || {
59+
for _ in 1..100 {
60+
let _ = jsonpath::select(&json, get_path($i));
61+
}
62+
});
63+
}
64+
};
65+
}
66+
67+
example!(example0, 0);
68+
example!(example1, 1);
69+
example!(example2, 2);
70+
example!(example3, 3);
71+
example!(example4, 4);
72+
example!(example5, 5);
73+
example!(example6, 6);
74+
example!(example7, 7);
75+
example!(example8, 8);
76+
example!(example9, 9);
77+
example!(example10, 10);
78+
example!(example11, 11);
79+
example!(example12, 12);
80+
example!(example13, 13);
81+
example!(example14, 14);

profiling.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
valgrind \
6+
--tool=callgrind \
7+
--dump-instr=yes \
8+
--collect-jumps=yes \
9+
--simulate-cache=yes $1 -- $2

src/filter/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod cmp;
22
mod term;
33
pub mod value_filter;
4-
pub mod value_wrapper;
4+
pub mod value_manager;
5+
#[deprecated(since = "0.1.14", note = "Please use the value_manager module instead")]
6+
pub use self::value_manager as value_wrapper;

src/filter/term.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
use super::cmp::*;
22
use super::value_filter::ValueFilterKey;
3-
use super::value_wrapper::*;
3+
use super::value_manager::*;
4+
use std::cell::RefCell;
5+
use select::path_map::PathMap;
6+
use std::sync::Arc;
47

58
#[derive(Debug)]
69
pub enum TermContext {
710
Constants(ExprTerm),
8-
Json(Option<ValueFilterKey>, ValueWrapper),
11+
Json(Option<ValueFilterKey>, ValueManager),
912
}
1013

1114
impl TermContext {
12-
fn cmp<F: PrivCmp + IntoType>(&self, other: &TermContext, cmp_fn: F, default: bool) -> TermContext {
15+
fn cmp<F: PrivCmp + IntoType>(&mut self, other: &mut TermContext, cmp_fn: F, default: bool) -> TermContext {
1316
match self {
1417
TermContext::Constants(et) => {
1518
match other {
1619
TermContext::Constants(oet) => {
1720
trace!("const-const");
1821
TermContext::Constants(ExprTerm::Bool(et.cmp(oet, cmp_fn, default)))
1922
}
20-
TermContext::Json(key, v) => {
23+
TermContext::Json(ref mut key, ref mut v) => {
2124
trace!("const-json");
22-
TermContext::Json(None, v.take_with(key, et, cmp_fn, true))
25+
TermContext::Json(None, v.get_compare_with(key, et, cmp_fn, true))
2326
}
2427
}
2528
}
@@ -35,24 +38,24 @@ impl TermContext {
3538
}
3639
}
3740

38-
let c = v.into_term(key);
39-
let oc = ov.into_term(key_other);
41+
let mut c = v.into_term(key);
42+
let mut oc = ov.into_term(key_other);
4043
if is_json(&c) && is_json(&oc) {
4144
v.cmp(&ov, cmp_fn.into_type())
4245
} else {
43-
c.cmp(&oc, cmp_fn, default)
46+
c.cmp(&mut oc, cmp_fn, default)
4447
}
4548
}
4649
TermContext::Constants(et) => {
4750
trace!("json-const");
48-
TermContext::Json(None, v.take_with(key, et, cmp_fn, false))
51+
TermContext::Json(None, v.get_compare_with(key, et, cmp_fn, false))
4952
}
5053
}
5154
}
5255
}
5356
}
5457

55-
fn cmp_cond(&self, other: &TermContext, cmp_cond_type: CmpCondType) -> TermContext {
58+
fn cmp_cond(&self, other: &TermContext, cmp_cond_type: CmpCondType, path_map: Arc<RefCell<PathMap>>) -> TermContext {
5659
match self {
5760
TermContext::Constants(et) => {
5861
match other {
@@ -67,7 +70,7 @@ impl TermContext {
6770
}
6871
}
6972
TermContext::Json(_, v) => {
70-
TermContext::Json(None, ValueWrapper::new(v.get_val().clone(), false))
73+
TermContext::Json(None, ValueManager::new(v.get_val().clone(), false, path_map))
7174
}
7275
}
7376
}
@@ -80,49 +83,49 @@ impl TermContext {
8083
}
8184
}
8285
_ => {
83-
TermContext::Json(None, ValueWrapper::new(v.get_val().clone(), false))
86+
TermContext::Json(None, ValueManager::new(v.get_val().clone(), false, path_map))
8487
}
8588
}
8689
}
8790
}
8891
}
8992

90-
pub fn eq(&self, other: &TermContext) -> TermContext {
93+
pub fn eq(&mut self, other: &mut TermContext) -> TermContext {
9194
trace!("eq");
9295
self.cmp(other, CmpEq, false)
9396
}
9497

95-
pub fn ne(&self, other: &TermContext) -> TermContext {
98+
pub fn ne(&mut self, other: &mut TermContext) -> TermContext {
9699
trace!("ne");
97100
self.cmp(other, CmpNe, true)
98101
}
99102

100-
pub fn gt(&self, other: &TermContext) -> TermContext {
103+
pub fn gt(&mut self, other: &mut TermContext) -> TermContext {
101104
trace!("gt");
102105
self.cmp(other, CmpGt, false)
103106
}
104107

105-
pub fn ge(&self, other: &TermContext) -> TermContext {
108+
pub fn ge(&mut self, other: &mut TermContext) -> TermContext {
106109
trace!("ge");
107110
self.cmp(other, CmpGe, false)
108111
}
109112

110-
pub fn lt(&self, other: &TermContext) -> TermContext {
113+
pub fn lt(&mut self, other: &mut TermContext) -> TermContext {
111114
trace!("lt");
112115
self.cmp(other, CmpLt, false)
113116
}
114117

115-
pub fn le(&self, other: &TermContext) -> TermContext {
118+
pub fn le(&mut self, other: &mut TermContext) -> TermContext {
116119
trace!("le");
117120
self.cmp(other, CmpLe, false)
118121
}
119122

120-
pub fn and(&self, other: &TermContext) -> TermContext {
121-
self.cmp_cond(other, CmpCondType::And)
123+
pub fn and(&mut self, other: &mut TermContext, path_map: Arc<RefCell<PathMap>>) -> TermContext {
124+
self.cmp_cond(other, CmpCondType::And, path_map)
122125
}
123126

124-
pub fn or(&self, other: &TermContext) -> TermContext {
125-
self.cmp_cond(other, CmpCondType::Or)
127+
pub fn or(&mut self, other: &mut TermContext, path_map: Arc<RefCell<PathMap>>) -> TermContext {
128+
self.cmp_cond(other, CmpCondType::Or, path_map)
126129
}
127130
}
128131

0 commit comments

Comments
 (0)