Skip to content

Commit ffdcb21

Browse files
committed
perf(post): parse cookies once and clone Preferences into each comment
1 parent 6e5db6f commit ffdcb21

2 files changed

Lines changed: 11 additions & 11 deletions

File tree

src/post.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
8585
let prefs = Preferences::new(&req);
8686
let filters: HashSet<String> = prefs.filters.iter().cloned().collect();
8787
let comments = match query.as_str() {
88-
"" => parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &filters, &req),
89-
_ => query_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &filters, &query, &req),
88+
"" => parse_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &filters, &prefs),
89+
_ => query_comments(&response[1], &post.permalink, &post.author.name, highlighted_comment, &filters, &query, &prefs),
9090
};
9191

9292
// Use the Post and Comment structs to generate a website to show users
@@ -115,7 +115,7 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
115115

116116
// COMMENTS
117117

118-
fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str, filters: &HashSet<String>, req: &Request<Body>) -> Vec<Comment> {
118+
fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str, highlighted_comment: &str, filters: &HashSet<String>, prefs: &Preferences) -> Vec<Comment> {
119119
// Parse the comment JSON into a Vector of Comments
120120
let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned);
121121

@@ -125,11 +125,11 @@ fn parse_comments(json: &serde_json::Value, post_link: &str, post_author: &str,
125125
.map(|comment| {
126126
let data = &comment["data"];
127127
let replies: Vec<Comment> = if data["replies"].is_object() {
128-
parse_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, req)
128+
parse_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, prefs)
129129
} else {
130130
Vec::new()
131131
};
132-
build_comment(&comment, data, replies, post_link, post_author, highlighted_comment, filters, req)
132+
build_comment(&comment, data, replies, post_link, post_author, highlighted_comment, filters, prefs)
133133
})
134134
.collect()
135135
}
@@ -141,7 +141,7 @@ fn query_comments(
141141
highlighted_comment: &str,
142142
filters: &HashSet<String>,
143143
query: &str,
144-
req: &Request<Body>,
144+
prefs: &Preferences,
145145
) -> Vec<Comment> {
146146
let comments = json["data"]["children"].as_array().map_or(Vec::new(), std::borrow::ToOwned::to_owned);
147147
let mut results = Vec::new();
@@ -151,10 +151,10 @@ fn query_comments(
151151

152152
// If this comment contains replies, handle those too
153153
if data["replies"].is_object() {
154-
results.append(&mut query_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, query, req));
154+
results.append(&mut query_comments(&data["replies"], post_link, post_author, highlighted_comment, filters, query, prefs));
155155
}
156156

157-
let c = build_comment(&comment, data, Vec::new(), post_link, post_author, highlighted_comment, filters, req);
157+
let c = build_comment(&comment, data, Vec::new(), post_link, post_author, highlighted_comment, filters, prefs);
158158
if c.body.to_lowercase().contains(&query.to_lowercase()) {
159159
results.push(c);
160160
}
@@ -171,7 +171,7 @@ fn build_comment(
171171
post_author: &str,
172172
highlighted_comment: &str,
173173
filters: &HashSet<String>,
174-
req: &Request<Body>,
174+
prefs: &Preferences,
175175
) -> Comment {
176176
let id = val(comment, "id");
177177
let comment_author = val(comment, "author");
@@ -255,6 +255,6 @@ fn build_comment(
255255
collapsed,
256256
is_filtered,
257257
more_count,
258-
prefs: Preferences::new(req),
258+
prefs: prefs.clone(),
259259
}
260260
}

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ pub struct Params {
614614
pub before: Option<String>,
615615
}
616616

617-
#[derive(Default, Serialize, Deserialize, Debug, PartialEq, Eq)]
617+
#[derive(Default, Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
618618
#[revisioned(revision = 1)]
619619
pub struct Preferences {
620620
#[revision(start = 1)]

0 commit comments

Comments
 (0)