diff --git a/benches/sorting.rs b/benches/sorting.rs new file mode 100644 index 0000000..429d043 --- /dev/null +++ b/benches/sorting.rs @@ -0,0 +1,27 @@ +//! Benchmarking separate functions of Gutenberg + +#![feature(test)] +extern crate test; +extern crate gutenberg; +extern crate tempdir; + +use std::env; + +use tempdir::TempDir; +use gutenberg::{Site, sort_pages, SortBy}; + +fn setup_site(name: &str) -> Site { + let mut path = env::current_dir().unwrap().to_path_buf(); + path.push("benches"); + path.push(name); + let mut site = Site::new(&path, "config.toml").unwrap(); + site.load().unwrap(); + site +} + +#[bench] +fn bench_sort_pages_medium_blog(b: &mut test::Bencher) { + let mut site = setup_site("huge-blog"); + let section = site.sections.values().next().unwrap().clone(); + b.iter(|| sort_pages(section.pages.clone(), SortBy::None)); +} diff --git a/benches/unit.rs b/benches/unit.rs index 67db371..ac0f000 100644 --- a/benches/unit.rs +++ b/benches/unit.rs @@ -8,7 +8,7 @@ extern crate tempdir; use std::env; use tempdir::TempDir; -use gutenberg::Site; +use gutenberg::{Site, sort_pages}; fn setup_site(name: &str) -> Site { @@ -58,3 +58,16 @@ fn bench_render_small_section_with_pages_and_no_pagination(b: &mut test::Bencher section.meta.template = None; b.iter(|| site.render_section(§ion, true).unwrap()); } + + +#[bench] +fn bench_sort_sections_pages_medium_blog(b: &mut test::Bencher) { + let mut site = setup_site("medium-blog"); + b.iter(|| site.sort_sections_pages(None)); +} + +#[bench] +fn bench_sort_sections_pages_medium_kb(b: &mut test::Bencher) { + let mut site = setup_site("medium-kb"); + b.iter(|| site.sort_sections_pages(None)); +} diff --git a/src/content/sorting.rs b/src/content/sorting.rs index 5238d44..bfdc741 100644 --- a/src/content/sorting.rs +++ b/src/content/sorting.rs @@ -1,3 +1,5 @@ +use rayon::prelude::*; + use content::Page; #[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)] @@ -25,7 +27,7 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { cannot_be_sorted.push(page); } } - can_be_sorted.sort_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); + can_be_sorted.par_sort_unstable_by(|a, b| b.meta.date().unwrap().cmp(&a.meta.date().unwrap())); (can_be_sorted, cannot_be_sorted) }, @@ -40,9 +42,9 @@ pub fn sort_pages(pages: Vec, sort_by: SortBy) -> (Vec, Vec) { } } if sort_by == SortBy::Order { - can_be_sorted.sort_by(|a, b| b.meta.order().cmp(&a.meta.order())); + can_be_sorted.par_sort_unstable_by(|a, b| b.meta.order().cmp(&a.meta.order())); } else { - can_be_sorted.sort_by(|a, b| a.meta.order().cmp(&b.meta.order())); + can_be_sorted.par_sort_unstable_by(|a, b| a.meta.order().cmp(&b.meta.order())); } (can_be_sorted, cannot_be_sorted)