Skip to content

Commit

Permalink
perf: reduce clones in render process (part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
delan committed Oct 6, 2024
1 parent 68ec578 commit 99b9d2a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
32 changes: 16 additions & 16 deletions src/command/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
interesting_output_paths.extend(result.interesting_output_paths);
for (tag, threads) in result.threads_by_interesting_tag {
threads_by_interesting_tag
.entry(tag.clone())
.entry(tag)
.or_insert(vec![])
.extend(threads);
}
Expand All @@ -145,18 +145,30 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
// author step: generate atom feeds.
let atom_feed_path = collections.write_atom_feed("index", &SitePath::ROOT, &now)?;
interesting_output_paths.insert(atom_feed_path);
for (tag, threads) in threads_by_interesting_tag.clone().into_iter() {

// generate /tagged/<tag>.feed.xml and /tagged/<tag>.html.
for (tag, threads) in threads_by_interesting_tag {
let atom_feed_path = SitePath::TAGGED.join(&format!("{tag}.feed.xml"))?;
writeln!(
File::create(&atom_feed_path)?,
"{}",
AtomFeedTemplate::render(
threads,
&threads,
format!("{} — {tag}", SETTINGS.site_title),
now.clone()
)?
)?;
interesting_output_paths.insert(atom_feed_path);
let threads_content = render_cached_threads_content(&threads_content_cache, threads);
let threads_page = ThreadsPageTemplate::render(
threads_content,
format!("#{tag} — {}", SETTINGS.site_title),
Some(SitePath::TAGGED.join(&format!("{tag}.feed.xml"))?),
)?;
// TODO: move this logic into path module and check for slashes
let threads_page_path = SitePath::TAGGED.join(&format!("{tag}.html"))?;
writeln!(File::create(&threads_page_path)?, "{}", threads_page)?;
interesting_output_paths.insert(threads_page_path);
}

let mut tags = tags.into_iter().collect::<Vec<_>>();
Expand All @@ -182,18 +194,6 @@ pub fn render<'posts>(post_paths: Vec<PostsPath>) -> eyre::Result<()> {
interesting_output_paths.insert(threads_page_path);
}
}
for (tag, threads) in threads_by_interesting_tag.into_iter() {
let threads_content = render_cached_threads_content(&threads_content_cache, threads);
let threads_page = ThreadsPageTemplate::render(
threads_content,
format!("#{tag} — {}", SETTINGS.site_title),
Some(SitePath::TAGGED.join(&format!("{tag}.feed.xml"))?),
)?;
// TODO: move this logic into path module and check for slashes
let threads_page_path = SitePath::TAGGED.join(&format!("{tag}.html"))?;
writeln!(File::create(&threads_page_path)?, "{}", threads_page)?;
interesting_output_paths.insert(threads_page_path);
}

let interesting_output_paths = interesting_output_paths
.into_iter()
Expand Down Expand Up @@ -459,7 +459,7 @@ impl Collection {
writeln!(
File::create(atom_feed_path)?,
"{}",
AtomFeedTemplate::render(threads, SETTINGS.site_title.clone(), now.to_owned())?
AtomFeedTemplate::render(&threads, SETTINGS.site_title.clone(), now.to_owned())?
)?;

Ok(())
Expand Down
8 changes: 4 additions & 4 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ pub struct ThreadOrPostHeaderTemplate {

#[derive(Clone, Debug, Template)]
#[template(path = "feed.xml")]
pub struct AtomFeedTemplate {
threads: Vec<Thread>,
pub struct AtomFeedTemplate<'threads> {
threads: &'threads [Thread],
feed_title: String,
updated: String,
}
Expand Down Expand Up @@ -109,9 +109,9 @@ impl ThreadOrPostHeaderTemplate {
}
}

impl AtomFeedTemplate {
impl<'threads> AtomFeedTemplate<'threads> {
pub fn render(
threads: Vec<Thread>,
threads: &'threads [Thread],
feed_title: String,
updated: String,
) -> eyre::Result<String> {
Expand Down

0 comments on commit 99b9d2a

Please sign in to comment.