Skip to content

Commit 030a100

Browse files
committed
perf: avoid full URL parse and HashMap alloc in param()
1 parent 2c7c6c0 commit 030a100

1 file changed

Lines changed: 5 additions & 10 deletions

File tree

src/utils.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rust_embed::RustEmbed;
1717
use serde::{Deserialize, Deserializer, Serialize, Serializer};
1818
use serde_json::Value;
1919
use serde_json_path::{JsonPath, JsonPathExt};
20-
use std::collections::{HashMap, HashSet};
20+
use std::collections::HashSet;
2121
use std::env;
2222
use std::io::{Read, Write};
2323
use std::string::ToString;
@@ -890,15 +890,10 @@ pub async fn parse_post(post: &Value) -> Post {
890890

891891
/// Grab a query parameter from a url
892892
pub fn param(path: &str, value: &str) -> Option<String> {
893-
Some(
894-
Url::parse(format!("https://libredd.it/{path}").as_str())
895-
.ok()?
896-
.query_pairs()
897-
.into_owned()
898-
.collect::<HashMap<_, _>>()
899-
.get(value)?
900-
.clone(),
901-
)
893+
let query = path.splitn(2, '?').nth(1).unwrap_or_default();
894+
url::form_urlencoded::parse(query.as_bytes())
895+
.find(|(k, _)| k == value)
896+
.map(|(_, v)| v.into_owned())
902897
}
903898

904899
/// Retrieve the value of a setting by name

0 commit comments

Comments
 (0)