Skip to content

Reduce number of String allocations #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@
All notable changes to this project will be documented in this file.
This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

### Changed

- [#3](https://github.com/sunsided/query-string-builder/pull/3):
The functions now change inputs that implement `ToString` rather than requiring `Into<String>`.
This allows for any `Display` types to be used directly.

## [0.4.0] - 2023-07-08

### Added
Expand Down
7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ license = "EUPL-1.2"

[dependencies]
percent-encoding = { version = "2.3.0", default-features = false, features = ["std"] }

[dev-dependencies]
criterion = "0.5.1"

[[bench]]
name = "bench"
harness = false
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ use query_string_builder::QueryString;
fn main() {
let qs = QueryString::new()
.with_value("q", "apple")
.with_value("tasty", true)
.with_opt_value("color", None::<String>)
.with_opt_value("category", Some("fruits and vegetables?"));

assert_eq!(
format!("https://example.com/{qs}"),
"https://example.com/?q=apple&category=fruits%20and%20vegetables?"
"https://example.com/?q=apple&tasty=true&category=fruits%20and%20vegetables?&tasty=true"
);
}
```
57 changes: 57 additions & 0 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use criterion::{Criterion, criterion_group, criterion_main};

use query_string_builder::QueryString;

pub fn criterion_benchmark(c: &mut Criterion) {
// `with_value` method benchmark
c.bench_function("with_value", |b| {
b.iter(|| {
let qs = QueryString::new()
.with_value("q", "apple???")
.with_value("category", "fruits and vegetables");
format!("{qs}")
})
});

c.bench_function("with_value_more", |b| {
b.iter(|| {
let qs = QueryString::new()
.with_value("q", "apple???")
.with_value("category", "fruits and vegetables")
.with_value("tasty", true)
.with_value("weight", 99.9);
format!("{qs}")
})
});

// `with_opt_value` method benchmark
c.bench_function("with_opt_value", |b| {
b.iter(|| {
let qs = QueryString::new()
.with_value("q", "celery")
.with_opt_value("taste", None::<String>)
.with_opt_value("category", Some("fruits and vegetables"))
.with_opt_value("tasty", Some(true))
.with_opt_value("weight", Some(99.9));
format!("{qs}")
})
});

// Full test including creating, pushing and appending
c.bench_function("push_opt_and_append", |b| {
b.iter(|| {
let mut qs = QueryString::new();
qs.push("a", "apple");
qs.push_opt("b", None::<String>);
qs.push_opt("c", Some("🍎 apple"));

let more = QueryString::new().with_value("q", "pear");
let qs = qs.append_into(more);

format!("{qs}")
})
});
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);
Loading