Skip to content

Commit

Permalink
refactor(merge_in): Improve performance (#15)
Browse files Browse the repository at this point in the history
* refactor(merge_in): Improve performance
* refactor(cicd): fix semantic release
  • Loading branch information
jmfiaschi authored Sep 9, 2023
1 parent af54495 commit 344fd31
Show file tree
Hide file tree
Showing 5 changed files with 236 additions and 179 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ jobs:
sudo npm install @semantic-release/git -g
sudo npm install @semantic-release/github -g
sudo npm install @semantic-release/exec -g
cargo install semantic-release-rust --version 1.0.0-alpha.8
- name: Run Semantic Release
run: semantic-release
env:
Expand Down
7 changes: 3 additions & 4 deletions .releaserc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
[
"@semantic-release/exec",
{
"verifyConditionsCmd": "semantic-release-rust verify-conditions",
"prepareCmd": "semantic-release-rust prepare ${nextRelease.version}",
"publishCmd": "semantic-release-rust publish"
"prepareCmd": "cargo set-version ${nextRelease.version} && cargo package --no-verify --allow-dirty",
"publishCmd": "cargo publish --no-verify --allow-dirty"
}
],
[
Expand All @@ -37,4 +36,4 @@
],
"@semantic-release/github"
]
}
}
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ readme = "README.md"
categories = ["encoding"]

[dependencies]
serde_json = "1.0"
serde_json = { version="1.0", default-features = false }

[dev-dependencies]
criterion = "0.3"
criterion = "0.5"

[[bench]]
name = "bench"
Expand Down
95 changes: 29 additions & 66 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,63 @@ use serde_json::*;
fn merge(c: &mut Criterion) {
let mut group = c.benchmark_group("merge");

let json_value: Value = Value::String("my_string".to_string());
let mut value: Value = Value::Null;
let value_to_merge = value.clone();
group.bench_function("null", |b| {
b.iter(|| black_box(json_value.clone().merge(Value::Null)))
b.iter(|| black_box(value.merge(&value_to_merge)))
});

let mut value: Value = Value::String("My String".to_string());
let value_to_merge = value.clone();
group.bench_function("string", |b| {
b.iter(|| {
black_box(
json_value
.clone()
.merge(Value::String("my string B".to_string())),
)
})
b.iter(|| black_box(value.merge(&value_to_merge)))
});

let json_value: Value = serde_json::from_str(r#"["a","b"]"#).unwrap();
let json_value_to_merge: Value = serde_json::from_str(r#"["c","d"]"#).unwrap();
let mut value: Value = serde_json::from_str(r#"["a","b"]"#).unwrap();
let value_to_merge: Value = value.clone();
group.bench_function("array", |b| {
b.iter(|| {
black_box({
json_value.clone().merge(json_value_to_merge.clone());
})
})
b.iter(|| black_box(value.merge(&value_to_merge)))
});
let json_value: Value = serde_json::from_str(r#"{"a":"b"}"#).unwrap();
let json_value_to_merge: Value = serde_json::from_str(r#"{"c":"d"}"#).unwrap();

let mut value: Value = serde_json::from_str(r#"{"a":"b"}"#).unwrap();
let value_to_merge: Value = value.clone();
group.bench_function("object", |b| {
b.iter(|| {
black_box({
json_value.clone().merge(json_value_to_merge.clone());
})
})
b.iter(|| black_box(value.merge(&value_to_merge)))
});
group.finish();
}

fn merge_in(c: &mut Criterion) {
let mut group = c.benchmark_group("merge_in");

let json_value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let mut value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let value_to_merge: Value = Value::Null;
group.bench_function("null", |b| {
b.iter(|| black_box(json_value.clone().merge_in("/field", Value::Null)))
b.iter(|| black_box(value.merge_in("/field", &value_to_merge).unwrap()))
});

let json_value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let mut value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let value_to_merge: Value = Value::String("new value".to_string());
group.bench_function("string.", |b| {
b.iter(|| {
black_box(
json_value
.clone()
.merge_in("/field", Value::String("new value".to_string())),
)
})
b.iter(|| black_box(value.merge_in("/field", &value_to_merge).unwrap()))
});

let json_value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let json_value_to_merge: Value = serde_json::from_str(r#"{"new_field":"new_value"}"#).unwrap();
let mut value: Value = serde_json::from_str(r#"{"field":"value"}"#).unwrap();
let value_to_merge: Value = serde_json::from_str(r#"{"new_field":"new_value"}"#).unwrap();
group.bench_function("object", |b| {
b.iter(|| {
black_box({
json_value
.clone()
.merge_in("/field", json_value_to_merge.clone())
.unwrap();
})
})
b.iter(|| black_box(value.merge_in("/field", &value_to_merge).unwrap()))
});

let json_value: Value = serde_json::from_str(r#"["value"]"#).unwrap();
let json_value_to_merge: Value = serde_json::from_str(r#"["new_value"]"#).unwrap();
let mut value: Value = serde_json::from_str(r#"["value"]"#).unwrap();
let value_to_merge: Value = serde_json::from_str(r#"["new_value"]"#).unwrap();
group.bench_function("array", |b| {
b.iter(|| {
black_box({
json_value
.clone()
.merge_in("/0", json_value_to_merge.clone())
.unwrap();
})
})
b.iter(|| black_box(value.merge_in("/0", &value_to_merge).unwrap()))
});

let json_value: Value = serde_json::from_str(r#"{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut"}}}}}"#).unwrap();
let json_value_to_merge: Value = serde_json::from_str(r#"{"new_field":"new_value"}"#).unwrap();
let mut value: Value = serde_json::from_str(r#"{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut","child":{"batters":{"batter":[{"id":"1001","type":"Regular"},{"id":"1002","type":"Chocolate"},{"id":"1003","type":"Blueberry"},{"id":"1004","type":"Devil's Food"}]},"id":"0001","name":"Cake","ppu":0.55,"topping":[{"id":"5001","type":"None"},{"id":"5002","type":"Glazed"},{"id":"5005","type":"Sugar"},{"id":"5007","type":"Powdered Sugar"},{"id":"5006","type":"Chocolate with Sprinkles"},{"id":"5003","type":"Chocolate"},{"id":"5004","type":"Maple"}],"type":"donut"}}}}}"#).unwrap();
let value_to_merge: Value = serde_json::from_str(r#"{"new_field":"new_value"}"#).unwrap();
group.bench_function("deeper_object", |b| {
b.iter(|| {
black_box({
json_value
.clone()
.merge_in(
"/child/child/child/batters/batter",
json_value_to_merge.clone(),
)
.unwrap();
})
})
b.iter(|| black_box(value.merge_in("/child/child/child/batters/batter",&value_to_merge).unwrap()))
});

group.finish();
Expand Down
Loading

0 comments on commit 344fd31

Please sign in to comment.