Skip to content
Open
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
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ harness = false
name = "test_examples_benchmark"
harness = false

[[bench]]
name = "string_alloc_benchmark"
harness = false

[dependencies.web-sys]
version = "0.3.85"
features = [
Expand Down
53 changes: 53 additions & 0 deletions benches/string_alloc_benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::hint::black_box;

use criterion::{BenchmarkId, Criterion, criterion_group, criterion_main};
use redirectionio::{RouterConfig, api::VariableValue, http::PathAndQueryWithSkipped, marker::StaticOrDynamic};

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

for &count in &[2, 5, 10, 20] {
let variables: Vec<(String, VariableValue)> = (0..count)
.map(|i| (format!("var{i}"), VariableValue::Value(format!("value{i}"))))
.collect();

let template: String = (0..count).map(|i| format!("/path/@var{i}/segment")).collect();

group.bench_with_input(BenchmarkId::from_parameter(count), &(template, variables), |b, (tpl, vars)| {
b.iter(|| StaticOrDynamic::replace(tpl.clone(), black_box(vars), false));
});
}

group.finish();
}

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

let urls: &[(&str, &str)] = &[
("/simple-path", "simple"),
("/path?a=1&b=2&c=3", "3_params"),
("/path?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10", "10_params"),
(
"/path?a=1&b=2&c=3&d=4&e=5&f=6&g=7&h=8&i=9&j=10&utm_source=google&utm_medium=cpc&utm_campaign=brand&utm_term=test&utm_content=ad1",
"10_params_5_marketing",
),
(
"/path?key=hello+world&special=%3Cscript%3E&long_value=Lorem+ipsum+dolor+sit+amet+consectetur+adipiscing+elit",
"special_chars",
),
];

let config = RouterConfig::default();

for &(url, label) in urls {
group.bench_with_input(BenchmarkId::from_parameter(label), &url, |b, &url| {
b.iter(|| PathAndQueryWithSkipped::from_config(&config, url));
});
}

group.finish();
}

criterion_group!(benches, static_or_dynamic_replace, path_and_query_with_skipped_from_config,);
criterion_main!(benches);
14 changes: 9 additions & 5 deletions src/http/query.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::fmt::Write;

use http::uri::PathAndQuery;
use linked_hash_map::LinkedHashMap;
use percent_encoding::{AsciiSet, CONTROLS, utf8_percent_encode};
Expand Down Expand Up @@ -68,10 +70,10 @@ impl PathAndQueryWithSkipped {
};

let mut new_path_and_query = path_and_query.path().to_string();
let mut skipped_query_params = "".to_string();
let mut skipped_query_params = String::new();

if let Some(query) = path_and_query.query() {
let mut query_string = "".to_string();
let mut query_string = String::new();

if config.ignore_all_query_parameters {
skipped_query_params = query.to_string();
Expand All @@ -83,15 +85,17 @@ impl PathAndQueryWithSkipped {
keys.sort();
}

let mut query_param = String::new();

for key in &keys {
let value = hash_query.get(key).unwrap();
let mut query_param = "".to_string();

query_param.push_str(&utf8_percent_encode(key, QUERY_ENCODE_SET).to_string());
query_param.clear();
let _ = write!(query_param, "{}", utf8_percent_encode(key, QUERY_ENCODE_SET));

if !value.is_empty() {
query_param.push('=');
query_param.push_str(&utf8_percent_encode(value, QUERY_ENCODE_SET).to_string());
let _ = write!(query_param, "{}", utf8_percent_encode(value, QUERY_ENCODE_SET));
}

if config.marketing_query_params.contains(key) {
Expand Down
24 changes: 12 additions & 12 deletions src/marker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod transformer;

use std::{
collections::HashMap,
fmt::Write,
sync::{Arc, RwLock},
};

Expand Down Expand Up @@ -44,15 +45,12 @@ impl Marker {

impl MarkerString {
pub fn new(str: &str, mut markers: Vec<Marker>, ignore_case: bool) -> Option<MarkerString> {
// Create regex string
let mut regex = regex::escape(str);
let mut capture = regex.clone();
let mut marker_map = HashMap::new();

// Sort markers by length
markers.sort_by(|a, b| b.name.len().cmp(&a.name.len()));

// Foreach marker replace
for marker in &markers {
let marker_regex = format!("(?:{})", marker.regex);
let marker_capture = format!("(?P<{}>{})", marker.name, marker.regex);
Expand Down Expand Up @@ -146,16 +144,18 @@ impl StaticOrDynamic {
}

pub fn replace(mut str: String, variables: &[(String, VariableValue)], use_default: bool) -> String {
let mut marker_buf = String::new();

for (name, value) in variables {
match value {
VariableValue::Value(v) => {
str = str.replace(format!("@{name}").as_str(), v.as_str());
}
VariableValue::HtmlFilter { default: Some(v), .. } if use_default => {
str = str.replace(format!("@{name}").as_str(), v.as_str());
}
_ => {}
}
let replacement = match value {
VariableValue::Value(v) => v.as_str(),
VariableValue::HtmlFilter { default: Some(v), .. } if use_default => v.as_str(),
_ => continue,
};

marker_buf.clear();
let _ = write!(marker_buf, "@{name}");
str = str.replace(marker_buf.as_str(), replacement);
}

str
Expand Down