2023-03-27 07:28:42 +03:00
|
|
|
#![cfg_attr(not(feature = "serde-impl"), allow(unused))]
|
|
|
|
|
2019-11-29 17:46:06 +03:00
|
|
|
use ast_node::ast_node;
|
2022-04-03 18:56:29 +03:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
2019-11-29 17:46:06 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use swc_common::{Span, DUMMY_SP};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SerdeStr {
|
|
|
|
span: Span,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ast_node("String")]
|
|
|
|
pub struct Str {
|
|
|
|
span: Span,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SerdeNum {
|
|
|
|
span: Span,
|
|
|
|
value: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ast_node("Number")]
|
|
|
|
pub struct Num {
|
|
|
|
span: Span,
|
|
|
|
value: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum Serde {
|
|
|
|
Number(SerdeNum),
|
|
|
|
String(SerdeStr),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[ast_node]
|
|
|
|
pub enum AstNode {
|
|
|
|
#[tag("Number")]
|
|
|
|
Number(Num),
|
|
|
|
#[tag("String")]
|
|
|
|
String(Str),
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
fn bench_serde(c: &mut Criterion) {
|
2019-11-29 17:46:06 +03:00
|
|
|
let src = Serde::String(SerdeStr {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
value: String::from("perf-diff"),
|
|
|
|
});
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
c.bench_function("serialization of serde", |b| {
|
|
|
|
b.iter(|| black_box(serde_json::to_string(&src).unwrap()));
|
2019-11-29 17:46:06 +03:00
|
|
|
});
|
2022-04-03 18:56:29 +03:00
|
|
|
c.bench_function("deserialization of serde", |b| {
|
|
|
|
let src = serde_json::to_string(&Serde::String(SerdeStr {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
value: String::from("perf-diff"),
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
println!("{}", src);
|
|
|
|
|
|
|
|
b.iter(|| black_box(serde_json::to_string(&src).unwrap()));
|
2019-11-29 17:46:06 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
fn bench_ast_node(c: &mut Criterion) {
|
2023-03-27 07:28:42 +03:00
|
|
|
#[cfg(feature = "serde-impl")]
|
2022-04-03 18:56:29 +03:00
|
|
|
c.bench_function("serialization of ast node", |b| {
|
|
|
|
let src = AstNode::String(Str {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
value: String::from("perf-diff"),
|
|
|
|
});
|
2019-11-29 17:46:06 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
b.iter(|| black_box(serde_json::to_string(&src).unwrap()));
|
|
|
|
});
|
2023-03-27 07:28:42 +03:00
|
|
|
#[cfg(feature = "serde-impl")]
|
2022-04-03 18:56:29 +03:00
|
|
|
c.bench_function("deserialization of ast node", |b| {
|
|
|
|
let src = serde_json::to_string(&AstNode::String(Str {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
value: String::from("perf-diff"),
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
println!("{}", src);
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
let t: AstNode = serde_json::from_str(&src).unwrap();
|
|
|
|
|
|
|
|
black_box(t);
|
|
|
|
});
|
2019-11-29 17:46:06 +03:00
|
|
|
});
|
|
|
|
}
|
2022-04-03 18:56:29 +03:00
|
|
|
|
|
|
|
criterion_group!(benches, bench_ast_node, bench_serde);
|
|
|
|
criterion_main!(benches);
|