Skip to content

Commit

Permalink
Switch from SHA-1 to SHA-512
Browse files Browse the repository at this point in the history
Local benchmarking showed that the implementation of SHA-512 in the *ring* crate
is 3x faster than the implementation of SHA-1 in the `sha1` crate. I've also
noticed that 80%+ of sccache's runtime on a fully cached build is spent hashing.
With this change I noticed a decrease from 108s to 92s when building a fully
cached LLVM from the network. Not a huge win but if the network were faster
could perhaps add up!

Closes mozilla#108
  • Loading branch information
alexcrichton committed Apr 21, 2017
1 parent 47404ed commit 1597bc6
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 43 deletions.
60 changes: 55 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ number_prefix = "0.2.5"
redis = { version = "0.8.0", optional = true }
regex = "0.1.65"
retry = "0.4.0"
ring = "0.7.0"
rust-crypto = { version = "0.2.36", optional = true }
rustc-serialize = "0.3"
serde = "0.9"
serde_derive = "0.9"
serde_json = { version = "0.9.0", optional = true }
sha1 = "0.2.0"
tempdir = "0.3.4"
time = "0.1.35"
tokio-core = "0.1.6"
Expand Down
9 changes: 4 additions & 5 deletions src/compiler/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ use compiler::{Cacheable, Compiler, CompilerArguments, CompilerHasher, CompilerK
use futures::Future;
use futures_cpupool::CpuPool;
use mock_command::CommandCreatorSync;
use sha1;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::ffi::{OsStr, OsString};
use std::fmt;
use std::path::Path;
use std::process;
use util::{os_str_bytes, sha1_digest};
use util::{os_str_bytes, Digest};

use errors::*;

Expand Down Expand Up @@ -130,7 +129,7 @@ impl <I> CCompiler<I>
{
pub fn new(compiler: I, executable: String, pool: &CpuPool) -> SFuture<CCompiler<I>>
{
Box::new(sha1_digest(executable.clone(), &pool).map(move |digest| {
Box::new(Digest::file(executable.clone(), &pool).map(move |digest| {
CCompiler {
executable: executable,
executable_digest: digest,
Expand Down Expand Up @@ -271,7 +270,7 @@ pub fn hash_key(compiler_digest: &str, arguments: &str, env_vars: &[(OsString, O
preprocessor_output: &[u8]) -> String
{
// If you change any of the inputs to the hash, you should change `CACHE_VERSION`.
let mut m = sha1::Sha1::new();
let mut m = Digest::new();
m.update(compiler_digest.as_bytes());
m.update(CACHE_VERSION);
m.update(arguments.as_bytes());
Expand All @@ -285,7 +284,7 @@ pub fn hash_key(compiler_digest: &str, arguments: &str, env_vars: &[(OsString, O
}
}
m.update(preprocessor_output);
m.digest().to_string()
m.finish()
}

#[cfg(test)]
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use futures::{Future, future};
use futures_cpupool::CpuPool;
use log::LogLevel::Trace;
use mock_command::{CommandCreatorSync, RunCommand};
use sha1;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::env::consts::DLL_EXTENSION;
Expand All @@ -31,7 +30,7 @@ use std::process::{self, Stdio};
use std::slice;
use std::time::Instant;
use tempdir::TempDir;
use util::{fmt_duration_as_secs, os_str_bytes, run_input_output, sha1_digest};
use util::{fmt_duration_as_secs, os_str_bytes, run_input_output, Digest};

use errors::*;

Expand Down Expand Up @@ -137,7 +136,7 @@ fn hash_all(files: Vec<String>, pool: &CpuPool) -> SFuture<Vec<String>>
let start = Instant::now();
let count = files.len();
let pool = pool.clone();
Box::new(future::join_all(files.into_iter().map(move |f| sha1_digest(f, &pool)))
Box::new(future::join_all(files.into_iter().map(move |f| Digest::file(f, &pool)))
.map(move |hashes| {
trace!("Hashed {} files in {}", count, fmt_duration_as_secs(&start.elapsed()));
hashes
Expand Down Expand Up @@ -508,7 +507,7 @@ impl<T> CompilerHasher<T> for RustHasher
let hashes = source_hashes.join(extern_hashes);
Box::new(hashes.and_then(move |(source_hashes, extern_hashes)| -> SFuture<_> {
// If you change any of the inputs to the hash, you should change `CACHE_VERSION`.
let mut m = sha1::Sha1::new();
let mut m = Digest::new();
// Hash inputs:
// 1. A version
m.update(CACHE_VERSION);
Expand Down Expand Up @@ -572,7 +571,7 @@ impl<T> CompilerHasher<T> for RustHasher
outputs.insert(dep_info, p);
}
HashResult {
key: m.digest().to_string(),
key: m.finish(),
compilation: Box::new(RustCompilation {
executable: executable,
arguments: arguments,
Expand Down Expand Up @@ -629,7 +628,6 @@ mod test {
use ::compiler::*;
use itertools::Itertools;
use mock_command::*;
use sha1;
use std::fs::File;
use std::io::Write;
use std::sync::{Arc,Mutex};
Expand Down Expand Up @@ -842,7 +840,6 @@ bar.rs:
drop(env_logger::init());
let f = TestFixture::new();
// SHA-1 digest of an empty file.
const EMPTY_DIGEST: &'static str = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
const FAKE_DIGEST: &'static str = "abcd1234";
// We'll just use empty files for each of these.
for s in ["foo.rs", "bar.rs", "bar.rlib"].iter() {
Expand Down Expand Up @@ -873,23 +870,26 @@ bar.rs:
(OsString::from("FOO"), OsString::from("bar")),
(OsString::from("CARGO_BLAH"), OsString::from("abc"))],
&pool).wait().unwrap();
let mut m = sha1::Sha1::new();
let mut m = Digest::new();
let empty_digest = m.finish();

let mut m = Digest::new();
// Version.
m.update(CACHE_VERSION);
// sysroot shlibs digests.
m.update(FAKE_DIGEST.as_bytes());
// Arguments, with externs sorted at the end.
m.update(b"ab--externabc--externxyz");
// bar.rs (source file, from dep-info)
m.update(EMPTY_DIGEST.as_bytes());
m.update(empty_digest.as_bytes());
// foo.rs (source file, from dep-info)
m.update(EMPTY_DIGEST.as_bytes());
m.update(empty_digest.as_bytes());
// bar.rlib (extern crate, from externs)
m.update(EMPTY_DIGEST.as_bytes());
m.update(empty_digest.as_bytes());
// Env vars
m.update(b"CARGO_BLAH=abc");
m.update(b"CARGO_PKG_NAME=foo");
let digest = m.digest().to_string();
let digest = m.finish();
assert_eq!(res.key, digest);
let mut out = res.compilation.outputs().map(|(k, _)| k.to_owned()).collect::<Vec<_>>();
out.sort();
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ extern crate libc;
#[cfg(windows)]
extern crate mio_named_pipes;
extern crate number_prefix;
extern crate ring;
#[cfg(feature = "redis")]
extern crate redis;
extern crate regex;
Expand All @@ -56,7 +57,6 @@ extern crate rustc_serialize;
extern crate serde_json;
#[macro_use]
extern crate serde_derive;
extern crate sha1;
extern crate tempdir;
extern crate time;
extern crate tokio_core;
Expand Down
Loading

0 comments on commit 1597bc6

Please sign in to comment.