This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[ethash] remove mem::uninitialized #10861
Merged
Merged
Changes from 5 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3cf20b1
[ethash] replace mem::uninitialized with MaybeUninit
ordian 65d38f2
[ethash] replace another occurence of mem::uninitialized
ordian 5ef9344
[ethash] clean up benches
ordian f3aff09
[ethash] remove last mem::uninitialized
ordian 23acbf3
[ethash] update outdated comment
ordian 4cb0564
[ethash] compile error on big endian targets
ordian 596a04b
[ethash] extract 32 into a constant
ordian 3700590
[ethash] rename the constant to KECCAK_LEN
ordian c5b3d48
[ethash] bench quick_get_difficulty
ordian ff75d3a
[ethash] remove MaybeUninit completely
ordian 535c2f0
[ethash] replace ptr::copy_nonoverlapping with copy_from_slice
ordian 222d1b8
Merge branch 'master' into MaybeUninit
ordian d446457
[ethash] s/header_len/hash_len
ordian fa6a39c
[ethash] remove duplication in bench
ordian 7c2d729
[ethash] add a config for basic benches
ordian 4c90463
[ethash] fix a typo in bench fn name
ordian 51e94fd
[ethash] remove needless cast
ordian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -141,17 +141,22 @@ pub fn quick_get_difficulty(header_hash: &H256, nonce: u64, mix_hash: &H256, pro | |
// | ||
// This cannot be elided by the compiler as it doesn't know the implementation of | ||
// `keccak_512`. | ||
let mut buf: [u8; 64 + 32] = mem::uninitialized(); | ||
let mut buf = mem::MaybeUninit::<[u8; 64 + 32]>::uninit(); | ||
|
||
ptr::copy_nonoverlapping(header_hash.as_ptr(), buf.as_mut_ptr(), 32); | ||
ptr::copy_nonoverlapping(&nonce as *const u64 as *const u8, buf[32..].as_mut_ptr(), 8); | ||
let buf_ptr = buf.as_mut_ptr() as *mut u8; | ||
ptr::copy_nonoverlapping(header_hash.as_ptr(), buf_ptr, 32); | ||
ptr::copy_nonoverlapping(&nonce as *const u64 as *const u8, buf_ptr.offset(32), 8); | ||
|
||
keccak_512::unchecked(buf.as_mut_ptr(), 64, buf.as_ptr(), 40); | ||
ptr::copy_nonoverlapping(mix_hash.as_ptr(), buf[64..].as_mut_ptr(), 32); | ||
keccak_512::unchecked(buf_ptr, 64, buf_ptr, 40); | ||
ptr::copy_nonoverlapping(mix_hash.as_ptr(), buf_ptr.offset(64), 32); | ||
|
||
// This is initialized in `keccak_256` | ||
let mut hash: [u8; 32] = mem::uninitialized(); | ||
keccak_256::unchecked(hash.as_mut_ptr(), hash.len(), buf.as_ptr(), buf.len()); | ||
let buf = buf.assume_init(); | ||
|
||
// This is initialized in `keccak_256` below. | ||
let mut hash = mem::MaybeUninit::<[u8; 32]>::uninit(); | ||
keccak_256::unchecked(hash.as_mut_ptr() as *mut u8, 32, buf.as_ptr(), buf.len()); | ||
|
||
let hash = hash.assume_init(); | ||
|
||
hash | ||
} | ||
|
@@ -208,27 +213,27 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) | |
// We explicitly write the first 40 bytes, leaving the last 24 as uninitialized. Then | ||
// `keccak_512` reads the first 40 bytes (4th parameter) and overwrites the entire array, | ||
// leaving it fully initialized. | ||
let mut out: [u8; NODE_BYTES] = mem::uninitialized(); | ||
let mut out = mem::MaybeUninit::<[u8; NODE_BYTES]>::uninit(); | ||
let out_ptr = out.as_mut_ptr() as *mut u8; | ||
|
||
ptr::copy_nonoverlapping(header_hash.as_ptr(), out.as_mut_ptr(), header_hash.len()); | ||
ptr::copy_nonoverlapping(header_hash.as_ptr(), out_ptr, header_hash.len()); | ||
ptr::copy_nonoverlapping( | ||
&nonce as *const u64 as *const u8, | ||
out[header_hash.len()..].as_mut_ptr(), | ||
out_ptr.add(header_hash.len()), | ||
mem::size_of::<u64>(), | ||
); | ||
|
||
// compute keccak-512 hash and replicate across mix | ||
keccak_512::unchecked( | ||
out.as_mut_ptr(), | ||
out_ptr, | ||
NODE_BYTES, | ||
out.as_ptr(), | ||
out_ptr, | ||
header_hash.len() + mem::size_of::<u64>(), | ||
); | ||
|
||
Node { bytes: out } | ||
Node { bytes: out.assume_init() } | ||
}, | ||
// This is fully initialized before being read, see `let mut compress = ...` below | ||
compress_bytes: unsafe { mem::uninitialized() }, | ||
compress_bytes: [0u8; MIX_WORDS], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Amazing, must have been part of one of the LLVM upgrades since this code was written. I absolutely benchmarked it before and 0-initialising it was significantly slower. Wonder if there's anywhere else where we can remove uninit entirely now. |
||
}; | ||
|
||
let mut mix: [_; MIX_NODES] = [buf.half_mix.clone(), buf.half_mix.clone()]; | ||
|
@@ -277,9 +282,9 @@ fn hash_compute(light: &Light, full_size: usize, header_hash: &H256, nonce: u64) | |
let mix_words: [u32; MIX_WORDS] = unsafe { mem::transmute(mix) }; | ||
|
||
{ | ||
// This is an uninitialized buffer to begin with, but we iterate precisely `compress.len()` | ||
// times and set each index, leaving the array fully initialized. THIS ONLY WORKS ON LITTLE- | ||
// ENDIAN MACHINES. See a future PR to make this and the rest of the code work correctly on | ||
// We iterate precisely `compress.len()` times and set each index, | ||
// leaving the array fully initialized. THIS ONLY WORKS ON LITTLE-ENDIAN MACHINES. | ||
ordian marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// See a future PR to make this and the rest of the code work correctly on | ||
// big-endian arches like mips. | ||
let compress: &mut [u32; MIX_WORDS / 4] = | ||
unsafe { make_const_array!(MIX_WORDS / 4, &mut buf.compress_bytes) }; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you extract 32 to a constant so you don't have to ensure that the
32
in the definition ofhash
and the32
in theunchecked
call are the same? Before you could do.len
but that's now impossible because ofMaybeUninit
.