From a3dc5f95aa06b26a056f67cdc5e8438e80e8394c Mon Sep 17 00:00:00 2001 From: Michael Woerister Date: Fri, 26 Aug 2016 16:50:24 -0400 Subject: [PATCH] incr.comp.: Make path's of session directories slightly shorter. By using "s-" instead of "sess-" as a prefix and encoding numbers as base36 instead of base16. --- src/librustc_incremental/persist/fs.rs | 113 ++++++++++++++++--------- 1 file changed, 75 insertions(+), 38 deletions(-) diff --git a/src/librustc_incremental/persist/fs.rs b/src/librustc_incremental/persist/fs.rs index 9e4a16fd43c..809e1324c1e 100644 --- a/src/librustc_incremental/persist/fs.rs +++ b/src/librustc_incremental/persist/fs.rs @@ -45,7 +45,7 @@ //! that are consistent with the state of the source code it was compiled //! from, with no need to change them ever again. At this point, the compiler //! finalizes and "publishes" its private session directory by renaming it -//! from "sess-{timestamp}-{random}-working" to "sess-{timestamp}-{SVH}". +//! from "s-{timestamp}-{random}-working" to "s-{timestamp}-{SVH}". //! 6. At this point the "old" session directory that we copied our data from //! at the beginning of the session has become obsolete because we have just //! published a more current version. Thus the compiler will delete it. @@ -201,7 +201,7 @@ pub fn prepare_session_directory(tcx: TyCtxt) -> Result { loop { // Generate a session directory of the form: // - // {incr-comp-dir}/{crate-name-and-disambiguator}/sess-{timestamp}-{random}-working + // {incr-comp-dir}/{crate-name-and-disambiguator}/s-{timestamp}-{random}-working let session_dir = generate_session_dir_path(&crate_dir); debug!("session-dir: {}", session_dir.display()); @@ -265,7 +265,7 @@ pub fn prepare_session_directory(tcx: TyCtxt) -> Result { /// This function finalizes and thus 'publishes' the session directory by -/// renaming it to `sess-{timestamp}-{svh}` and releasing the file lock. +/// renaming it to `s-{timestamp}-{svh}` and releasing the file lock. /// If there have been compilation errors, however, this function will just /// delete the presumably invalid session directory. pub fn finalize_session_directory(sess: &Session, svh: Svh) { @@ -302,7 +302,7 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) { .to_string_lossy(); assert_no_characters_lost(&old_sub_dir_name); - // Keep the 'sess-{timestamp}-{random-number}' prefix, but replace the + // Keep the 's-{timestamp}-{random-number}' prefix, but replace the // '-working' part with the SVH of the crate let dash_indices: Vec<_> = old_sub_dir_name.match_indices("-") .map(|(idx, _)| idx) @@ -313,11 +313,11 @@ pub fn finalize_session_directory(sess: &Session, svh: Svh) { incr_comp_session_dir.display()) } - // State: "sess-{timestamp}-{random-number}-" + // State: "s-{timestamp}-{random-number}-" let mut new_sub_dir_name = String::from(&old_sub_dir_name[.. dash_indices[2] + 1]); // Append the svh - new_sub_dir_name.push_str(&svh.to_string()); + new_sub_dir_name.push_str(&encode_base_36(svh.as_u64())); // Create the full path let new_path = incr_comp_session_dir.parent().unwrap().join(new_sub_dir_name); @@ -405,14 +405,16 @@ fn copy_files(target_dir: &Path, } /// Generate unique directory path of the form: -/// {crate_dir}/sess-{timestamp}-{random-number}-working +/// {crate_dir}/s-{timestamp}-{random-number}-working fn generate_session_dir_path(crate_dir: &Path) -> PathBuf { let timestamp = timestamp_to_string(SystemTime::now()); debug!("generate_session_dir_path: timestamp = {}", timestamp); let random_number = thread_rng().next_u32(); debug!("generate_session_dir_path: random_number = {}", random_number); - let directory_name = format!("sess-{}-{:x}-working", timestamp, random_number); + let directory_name = format!("s-{}-{}-working", + timestamp, + encode_base_36(random_number as u64)); debug!("generate_session_dir_path: directory_name = {}", directory_name); let directory_path = crate_dir.join(directory_name); debug!("generate_session_dir_path: directory_path = {}", directory_path.display()); @@ -517,12 +519,12 @@ fn is_finalized(directory_name: &str) -> bool { } fn is_session_directory(directory_name: &str) -> bool { - directory_name.starts_with("sess-") && + directory_name.starts_with("s-") && !directory_name.ends_with(LOCK_FILE_EXT) } fn is_session_directory_lock_file(file_name: &str) -> bool { - file_name.starts_with("sess-") && file_name.ends_with(LOCK_FILE_EXT) + file_name.starts_with("s-") && file_name.ends_with(LOCK_FILE_EXT) } fn extract_timestamp_from_session_dir(directory_name: &str) @@ -541,15 +543,31 @@ fn extract_timestamp_from_session_dir(directory_name: &str) string_to_timestamp(&directory_name[dash_indices[0]+1 .. dash_indices[1]]) } +const BASE_36: &'static [u8] = b"0123456789abcdefghijklmnopqrstuvwxyz"; + +fn encode_base_36(mut n: u64) -> String { + let mut s = Vec::with_capacity(13); + loop { + s.push(BASE_36[(n % 36) as usize]); + n /= 36; + + if n == 0 { + break; + } + } + s.reverse(); + String::from_utf8(s).unwrap() +} + fn timestamp_to_string(timestamp: SystemTime) -> String { let duration = timestamp.duration_since(UNIX_EPOCH).unwrap(); let micros = duration.as_secs() * 1_000_000 + (duration.subsec_nanos() as u64) / 1000; - format!("{:x}", micros) + encode_base_36(micros) } fn string_to_timestamp(s: &str) -> Result { - let micros_since_unix_epoch = u64::from_str_radix(s, 16); + let micros_since_unix_epoch = u64::from_str_radix(s, 36); if micros_since_unix_epoch.is_err() { return Err(()) @@ -591,7 +609,8 @@ pub fn find_metadata_hashes_for(tcx: TyCtxt, cnum: ast::CrateNum) -> Option