Rust library to generate static websites
5
fork

Configure Feed

Select the types of activity you want to include in your feed.

perf: try to clean up in a separate thread (#20)

* perf: try to clean up in a separate thread

* chore: gitignore benchmarks

* fix: make cleaning optional

* refactor: rename option

authored by

Erika and committed by
GitHub
785519be 0db640a3

+34 -2
+3
.gitignore
··· 4 4 5 5 .DS_Store 6 6 project.tar 7 + 8 + flamegraph.svg 9 + profile.json.gz
+25 -2
crates/maudit/src/build.rs
··· 1 1 use std::{ 2 + env, 2 3 fs::{self, remove_dir_all, File}, 3 4 io::{self, Write}, 4 5 path::{Path, PathBuf}, 5 6 str::FromStr, 6 - time::SystemTime, 7 + time::{SystemTime, UNIX_EPOCH}, 7 8 }; 8 9 9 10 use crate::{ ··· 55 56 let tmp_dir = dist_dir.join("_tmp"); 56 57 let static_dir = PathBuf::from_str(&options.static_dir)?; 57 58 58 - let _ = fs::remove_dir_all(&dist_dir); 59 + let old_dist_tmp_dir = if options.clean_output_dir { 60 + let duration = SystemTime::now().duration_since(UNIX_EPOCH).unwrap(); 61 + let num = (duration.as_secs() + duration.subsec_nanos() as u64) % 100000; 62 + let new_dir_for_old_dist = env::temp_dir().join(format!("maudit_old_dist_{}", num)); 63 + let _ = fs::rename(&dist_dir, &new_dir_for_old_dist); 64 + Some(new_dir_for_old_dist) 65 + } else { 66 + None 67 + }; 68 + 69 + let should_clear_dist = options.clean_output_dir; 70 + let clean_up_handle = tokio::spawn(async move { 71 + if should_clear_dist { 72 + let _ = fs::remove_dir_all(old_dist_tmp_dir.unwrap()); 73 + } 74 + }); 75 + 59 76 fs::create_dir_all(&dist_dir)?; 60 77 fs::create_dir_all(&assets_dir)?; 61 78 ··· 103 120 let mut build_pages_styles: FxHashSet<assets::Style> = FxHashSet::default(); 104 121 105 122 let mut page_count = 0; 123 + 124 + // TODO: This is fully serial. Parallelizing it is trivial with Rayon and stuff, but it doesn't necessarily make it 125 + // faster in all cases, making it sometimes even slower due to the overhead. It'd be great to investigate and benchmark 126 + // this. 106 127 for route in routes { 107 128 let params_def = extract_params_from_raw_route(&route.route_raw()); 108 129 let route_type = get_route_type_from_route_params(&params_def); ··· 313 334 314 335 info!(target: "SKIP_FORMAT", "{}", ""); 315 336 info!(target: "build", "{}", format!("Build completed in {}", format_elapsed_time(build_start.elapsed(), &section_format_options).unwrap()).bold()); 337 + 338 + clean_up_handle.await.unwrap(); 316 339 317 340 Ok(build_metadata) 318 341 }
+6
crates/maudit/src/build/options.rs
··· 30 30 /// assets_dir: "_assets".to_string(), 31 31 /// static_dir: "static".to_string(), 32 32 /// tailwind_binary_path: "./node_modules/.bin/tailwindcss".to_string(), 33 + /// ..Default::default() 33 34 /// }, 34 35 /// ) 35 36 /// } ··· 39 40 pub assets_dir: String, 40 41 pub static_dir: String, 41 42 pub tailwind_binary_path: String, 43 + /// Whether to clean the output directory before building. 44 + /// 45 + /// At the speed Maudit operates at, not cleaning the output directory may offer a significant performance improvement at the cost of potentially serving stale content. 46 + pub clean_output_dir: bool, 42 47 } 43 48 44 49 /// Provides default values for [`crate::coronate()`]. Designed to work for most projects. ··· 64 69 assets_dir: "_maudit".to_string(), 65 70 static_dir: "static".to_string(), 66 71 tailwind_binary_path: "./node_modules/.bin/tailwindcss".to_string(), 72 + clean_output_dir: true, 67 73 } 68 74 } 69 75 }