summaryrefslogtreecommitdiff
path: root/serde_v8/benches/de.rs
blob: 23b1161c78858d7619d5c5580fc86d3779aba0b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
use bencher::{benchmark_group, benchmark_main, Bencher};

use serde::Deserialize;

use serde_v8::utils::{js_exec, v8_do};
use serde_v8::ByteString;

#[derive(Debug, Deserialize, PartialEq)]
struct MathOp {
  arg1: u64,
  arg2: u64,
  operator: Option<String>,
}

fn dedo(
  code: &str,
  f: impl FnOnce(&mut v8::HandleScope, v8::Local<v8::Value>),
) {
  v8_do(|| {
    let isolate = &mut v8::Isolate::new(v8::CreateParams::default());
    let handle_scope = &mut v8::HandleScope::new(isolate);
    let context = v8::Context::new(handle_scope);
    let scope = &mut v8::ContextScope::new(handle_scope, context);
    let v = js_exec(scope, code);

    f(scope, v);
  })
}

fn dedo_json(code: &str, f: impl FnOnce(String)) {
  let code = format!("JSON.stringify({})", code);
  dedo(&code[..], |scope, v| {
    let s: String = serde_v8::from_v8(scope, v).unwrap();
    f(s);
  })
}

fn de_struct_v8(b: &mut Bencher) {
  dedo("({arg1: 10, arg2: 123 })", |scope, obj| {
    let mut total = 0;
    b.iter(move || {
      let op: MathOp = serde_v8::from_v8(scope, obj).unwrap();
      total = total + op.arg1 + op.arg2;
    });
  });
}

fn de_struct_v8_opt(b: &mut Bencher) {
  dedo("({arg1: 10, arg2: 123 })", |scope, v| {
    let k_arg1 = v8::String::new(scope, "arg1").unwrap().into();
    let k_arg2 = v8::String::new(scope, "arg2").unwrap().into();
    let obj = v8::Local::<v8::Object>::try_from(v).unwrap();
    let mut total = 0;
    b.iter(move || {
      let v_arg1 = obj.get(scope, k_arg1).unwrap();
      let v_arg2 = obj.get(scope, k_arg2).unwrap();
      let op = MathOp {
        arg1: serde_v8::from_v8(scope, v_arg1).unwrap(),
        arg2: serde_v8::from_v8(scope, v_arg2).unwrap(),
        operator: None,
      };
      total = total + op.arg1 + op.arg2;
    });
  });
}

fn de_struct_json(b: &mut Bencher) {
  dedo_json("({arg1: 10, arg2: 123 })", |s| {
    let mut total = 0;
    b.iter(move || {
      let op: MathOp = serde_json::from_str(&s).unwrap();
      total = total + op.arg1 + op.arg2;
    });
  });
}

fn de_struct_json_deopt(b: &mut Bencher) {
  // JSON.stringify() in loop (semi-simulating ABI loop)
  dedo("({arg1: 10, arg2: 123 })", |scope, obj| {
    let mut total = 0;
    b.iter(move || {
      let mut scope = v8::HandleScope::new(scope);
      let s = v8::json::stringify(&mut scope, obj).unwrap();
      let rs = s.to_rust_string_lossy(&mut scope);
      let op: MathOp = serde_json::from_str(&rs).unwrap();
      total = total + op.arg1 + op.arg2;
    });
  });
}

macro_rules! dualbench {
  ($v8_fn:ident, $json_fn:ident, $src:expr, $t:ty) => {
    fn $v8_fn(b: &mut Bencher) {
      dedo($src, |scope, v| {
        b.iter(move || {
          let _: $t = serde_v8::from_v8(scope, v).unwrap();
        });
      });
    }

    fn $json_fn(b: &mut Bencher) {
      dedo_json($src, |s| {
        b.iter(move || {
          let _: $t = serde_json::from_str(&s).unwrap();
        });
      });
    }
  };
}

dualbench!(de_bool_v8, de_bool_json, "true", bool);
dualbench!(de_int_v8, de_int_json, "12345", u32);
dualbench!(
  de_array_v8,
  de_array_json,
  "[1,2,3,4,5,6,7,8,9,10]",
  Vec<u32>
);
dualbench!(de_str_v8, de_str_json, "'hello world'", String);
dualbench!(de_tuple_v8, de_tuple_json, "[1,false]", (u8, bool));

fn de_tuple_v8_opt(b: &mut Bencher) {
  dedo("[1,false]", |scope, obj| {
    let arr = v8::Local::<v8::Array>::try_from(obj).unwrap();
    let obj = v8::Local::<v8::Object>::from(arr);

    b.iter(move || {
      let v1 = obj.get_index(scope, 0).unwrap();
      let v2 = obj.get_index(scope, 1).unwrap();
      let _: (u8, bool) = (
        serde_v8::from_v8(scope, v1).unwrap(),
        serde_v8::from_v8(scope, v2).unwrap(),
      );
    });
  });
}

fn de_bstr_v8_12_b(b: &mut Bencher) {
  dedo(r#""hello world\n""#, |scope, v| {
    b.iter(move || {
      let _: ByteString = serde_v8::from_v8(scope, v).unwrap();
    });
  });
}

fn de_bstr_v8_1024_b(b: &mut Bencher) {
  dedo(
    r#""hello world\n".repeat(1e2).slice(0, 1024)"#,
    |scope, v| {
      b.iter(move || {
        let _: ByteString = serde_v8::from_v8(scope, v).unwrap();
      });
    },
  );
}

benchmark_group!(
  benches,
  de_struct_v8,
  de_struct_v8_opt,
  de_struct_json,
  de_struct_json_deopt,
  de_bool_v8,
  de_bool_json,
  de_int_v8,
  de_int_json,
  de_array_v8,
  de_array_json,
  de_str_v8,
  de_str_json,
  de_tuple_v8,
  de_tuple_json,
  de_tuple_v8_opt,
  de_bstr_v8_12_b,
  de_bstr_v8_1024_b,
);

benchmark_main!(benches);