Skip to content

Commit feb0a1b

Browse files
committed
perf: dedup req.cookie() calls in setting() to reduce header parses
1 parent 76decf7 commit feb0a1b

1 file changed

Lines changed: 44 additions & 50 deletions

File tree

src/utils.rs

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -913,70 +913,64 @@ pub fn setting(req: &Request<Body>, name: &str) -> String {
913913
// Parse a cookie value from request
914914

915915
// If this was called with "subscriptions" and the "subscriptions" cookie has a value
916-
if name == "subscriptions" && req.cookie("subscriptions").is_some() {
917-
// Create subscriptions string
918-
let mut subscriptions = String::new();
916+
if name == "subscriptions" {
917+
if let Some(base) = req.cookie("subscriptions") {
918+
// Create subscriptions string
919+
let mut subscriptions = String::from(base.value());
919920

920-
// Default subscriptions cookie
921-
if req.cookie("subscriptions").is_some() {
922-
subscriptions.push_str(req.cookie("subscriptions").unwrap().value());
923-
}
921+
// Start with first numbered subscription cookie
922+
let mut subscriptions_number = 1;
924923

925-
// Start with first numbered subscription cookie
926-
let mut subscriptions_number = 1;
924+
// While whatever subscriptionsNUMBER cookie we're looking at has a value
925+
while let Some(cookie) = req.cookie(&format!("subscriptions{subscriptions_number}")) {
926+
// Push whatever subscriptionsNUMBER cookie we're looking at into the subscriptions string
927+
subscriptions.push_str(cookie.value());
927928

928-
// While whatever subscriptionsNUMBER cookie we're looking at has a value
929-
while req.cookie(&format!("subscriptions{subscriptions_number}")).is_some() {
930-
// Push whatever subscriptionsNUMBER cookie we're looking at into the subscriptions string
931-
subscriptions.push_str(req.cookie(&format!("subscriptions{subscriptions_number}")).unwrap().value());
929+
// Increment subscription cookie number
930+
subscriptions_number += 1;
931+
}
932932

933-
// Increment subscription cookie number
934-
subscriptions_number += 1;
933+
// Return the subscriptions cookies as one large string
934+
return subscriptions;
935935
}
936-
937-
// Return the subscriptions cookies as one large string
938-
subscriptions
939936
}
937+
940938
// If this was called with "filters" and the "filters" cookie has a value
941-
else if name == "filters" && req.cookie("filters").is_some() {
942-
// Create filters string
943-
let mut filters = String::new();
939+
if name == "filters" {
940+
if let Some(base) = req.cookie("filters") {
941+
// Create filters string
942+
let mut filters = String::from(base.value());
944943

945-
// Default filters cookie
946-
if req.cookie("filters").is_some() {
947-
filters.push_str(req.cookie("filters").unwrap().value());
948-
}
944+
// Start with first numbered filters cookie
945+
let mut filters_number = 1;
949946

950-
// Start with first numbered filters cookie
951-
let mut filters_number = 1;
947+
// While whatever filtersNUMBER cookie we're looking at has a value
948+
while let Some(cookie) = req.cookie(&format!("filters{filters_number}")) {
949+
// Push whatever filtersNUMBER cookie we're looking at into the filters string
950+
filters.push_str(cookie.value());
952951

953-
// While whatever filtersNUMBER cookie we're looking at has a value
954-
while req.cookie(&format!("filters{filters_number}")).is_some() {
955-
// Push whatever filtersNUMBER cookie we're looking at into the filters string
956-
filters.push_str(req.cookie(&format!("filters{filters_number}")).unwrap().value());
952+
// Increment filters cookie number
953+
filters_number += 1;
954+
}
957955

958-
// Increment filters cookie number
959-
filters_number += 1;
956+
// Return the filters cookies as one large string
957+
return filters;
960958
}
961-
962-
// Return the filters cookies as one large string
963-
filters
964959
}
960+
965961
// The above two still come to this if there was no existing value
966-
else {
967-
req
968-
.cookie(name)
969-
.unwrap_or_else(|| {
970-
// If there is no cookie for this setting, try receiving a default from the config
971-
if let Some(default) = get_setting(&format!("REDLIB_DEFAULT_{}", name.to_uppercase())) {
972-
Cookie::new(name, default)
973-
} else {
974-
Cookie::from(name)
975-
}
976-
})
977-
.value()
978-
.to_string()
979-
}
962+
req
963+
.cookie(name)
964+
.unwrap_or_else(|| {
965+
// If there is no cookie for this setting, try receiving a default from the config
966+
if let Some(default) = get_setting(&format!("REDLIB_DEFAULT_{}", name.to_uppercase())) {
967+
Cookie::new(name, default)
968+
} else {
969+
Cookie::from(name)
970+
}
971+
})
972+
.value()
973+
.to_string()
980974
}
981975

982976
/// Retrieve the value of a setting by name or the default value

0 commit comments

Comments
 (0)