From d51958dbf5a66a432722e15addca0dc37c957681 Mon Sep 17 00:00:00 2001 From: Vurich Date: Tue, 18 Jul 2017 15:38:10 +0200 Subject: [PATCH] Speed up light_compute by ~9% --- Cargo.lock | 7 +++++++ ethash/Cargo.toml | 1 + ethash/src/compute.rs | 39 +++++++++++++++++++++++++++++++++------ ethash/src/lib.rs | 2 ++ 4 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63597b890..3e88b40cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -254,6 +254,11 @@ name = "crossbeam" version = "0.2.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "crunchy" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "crypt32-sys" version = "0.2.0" @@ -360,6 +365,7 @@ dependencies = [ name = "ethash" version = "1.7.0" dependencies = [ + "crunchy 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "parking_lot 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "primal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2943,6 +2949,7 @@ dependencies = [ "checksum core-foundation 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "20a6d0448d3a99d977ae4a2aa5a98d886a923e863e81ad9ff814645b6feb3bbd" "checksum core-foundation-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "05eed248dc504a5391c63794fe4fb64f46f071280afaa1b73308f3c0ce4574c5" "checksum crossbeam 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "0c5ea215664ca264da8a9d9c3be80d2eaf30923c259d03e870388eb927508f97" +"checksum crunchy 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b56bdac907d4b64254aed43964b11dd334bc46e0826a0e3add75caddfaf90175" "checksum crypt32-sys 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e34988f7e069e0b2f3bfc064295161e489b2d4e04a2e4248fb94360cdf00b4ec" "checksum ctrlc 1.1.1 (git+https://github.com/paritytech/rust-ctrlc.git)" = "" "checksum daemonize 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "271ec51b7e0bee92f0d04601422c73eb76ececf197026711c97ad25038a010cf" diff --git a/ethash/Cargo.toml b/ethash/Cargo.toml index 417dc8426..5f938be3d 100644 --- a/ethash/Cargo.toml +++ b/ethash/Cargo.toml @@ -10,6 +10,7 @@ log = "0.3" sha3 = { path = "../util/sha3" } primal = "0.2.3" parking_lot = "0.4" +crunchy = "0.1.0" [features] benches = [] \ No newline at end of file diff --git a/ethash/src/compute.rs b/ethash/src/compute.rs index 992578041..67b1ea678 100644 --- a/ethash/src/compute.rs +++ b/ethash/src/compute.rs @@ -287,12 +287,39 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) let num_full_pages = (full_size / page_size) as u32; let cache: &[Node] = &light.cache; // deref once for better performance - for i in 0..(ETHASH_ACCESSES as u32) { - let index = fnv_hash(f_mix.get_unchecked(0).as_words().get_unchecked(0) ^ i, *mix.get_unchecked(0).as_words().get_unchecked((i as usize) % MIX_WORDS)) % num_full_pages; - for n in 0..MIX_NODES { - let tmp_node = calculate_dag_item(index * MIX_NODES as u32 + n as u32, cache); - for w in 0..NODE_WORDS { - *mix.get_unchecked_mut(n).as_words_mut().get_unchecked_mut(w) = fnv_hash(*mix.get_unchecked(n).as_words().get_unchecked(w), *tmp_node.as_words().get_unchecked(w)); + debug_assert_eq!(ETHASH_ACCESSES, 64); + debug_assert_eq!(MIX_NODES, 2); + debug_assert_eq!(NODE_WORDS, 16); + + unroll! { + // ETHASH_ACCESSES + for i_usize in 0..64 { + let i = i_usize as u32; + + let index = fnv_hash( + f_mix.get_unchecked(0).as_words().get_unchecked(0) ^ i, + *mix.get_unchecked(0).as_words().get_unchecked(i_usize % MIX_WORDS) + ) % num_full_pages; + + unroll! { + // MIX_NODES + for n in 0..2 { + let tmp_node = calculate_dag_item( + index * MIX_NODES as u32 + n as u32, + cache, + ); + + unroll! { + // NODE_WORDS + for w in 0..16 { + *mix.get_unchecked_mut(n).as_words_mut().get_unchecked_mut(w) = + fnv_hash( + *mix.get_unchecked(n).as_words().get_unchecked(w), + *tmp_node.as_words().get_unchecked(w), + ); + } + } + } } } } diff --git a/ethash/src/lib.rs b/ethash/src/lib.rs index e154ac2f0..efe1950e3 100644 --- a/ethash/src/lib.rs +++ b/ethash/src/lib.rs @@ -22,6 +22,8 @@ extern crate primal; extern crate sha3; extern crate parking_lot; +#[macro_use] +extern crate crunchy; #[macro_use] extern crate log; mod compute;