@@ -13,6 +13,7 @@ struct Shared {
1313 prefix : String ,
1414 done : AtomicBool ,
1515 counter : AtomicU32 ,
16+ mutex : Mutex < Exclusive > ,
1617}
1718
1819struct Exclusive {
@@ -25,24 +26,24 @@ pub fn parse(input: &str) -> Vec<u32> {
2526 prefix : input. trim ( ) . to_owned ( ) ,
2627 done : AtomicBool :: new ( false ) ,
2728 counter : AtomicU32 :: new ( 1000 ) ,
29+ mutex : Mutex :: new ( Exclusive { found : vec ! [ ] , mask : 0 } ) ,
2830 } ;
29- let mutex = Mutex :: new ( Exclusive { found : vec ! [ ] , mask : 0 } ) ;
3031
3132 // Handle the first 999 numbers specially as the number of digits varies.
3233 for n in 1 ..1000 {
3334 let ( mut buffer, size) = format_string ( & shared. prefix , n) ;
34- check_hash ( & mut buffer, size, n, & shared, & mutex ) ;
35+ check_hash ( & mut buffer, size, n, & shared) ;
3536 }
3637
3738 // Use as many cores as possible to parallelize the remaining search.
3839 spawn ( || {
3940 #[ cfg( not( feature = "simd" ) ) ]
40- worker ( & shared, & mutex ) ;
41+ worker ( & shared) ;
4142 #[ cfg( feature = "simd" ) ]
42- simd:: worker ( & shared, & mutex ) ;
43+ simd:: worker ( & shared) ;
4344 } ) ;
4445
45- let mut found = mutex. into_inner ( ) . unwrap ( ) . found ;
46+ let mut found = shared . mutex . into_inner ( ) . unwrap ( ) . found ;
4647 found. sort_unstable ( ) ;
4748 found. iter ( ) . map ( |& ( _, n) | n) . collect ( )
4849}
@@ -79,11 +80,11 @@ fn format_string(prefix: &str, n: u32) -> ([u8; 64], usize) {
7980 ( buffer, size)
8081}
8182
82- fn check_hash ( buffer : & mut [ u8 ] , size : usize , n : u32 , shared : & Shared , mutex : & Mutex < Exclusive > ) {
83+ fn check_hash ( buffer : & mut [ u8 ] , size : usize , n : u32 , shared : & Shared ) {
8384 let ( result, ..) = hash ( buffer, size) ;
8485
8586 if result & 0xfffff000 == 0 {
86- let mut exclusive = mutex. lock ( ) . unwrap ( ) ;
87+ let mut exclusive = shared . mutex . lock ( ) . unwrap ( ) ;
8788
8889 exclusive. found . push ( ( n, result) ) ;
8990 exclusive. mask |= 1 << ( result >> 8 ) ;
@@ -95,7 +96,7 @@ fn check_hash(buffer: &mut [u8], size: usize, n: u32, shared: &Shared, mutex: &M
9596}
9697
9798#[ cfg( not( feature = "simd" ) ) ]
98- fn worker ( shared : & Shared , mutex : & Mutex < Exclusive > ) {
99+ fn worker ( shared : & Shared ) {
99100 while !shared. done . load ( Ordering :: Relaxed ) {
100101 let offset = shared. counter . fetch_add ( 1000 , Ordering :: Relaxed ) ;
101102 let ( mut buffer, size) = format_string ( & shared. prefix , offset) ;
@@ -106,7 +107,7 @@ fn worker(shared: &Shared, mutex: &Mutex<Exclusive>) {
106107 buffer[ size - 2 ] = b'0' + ( ( n / 10 ) % 10 ) as u8 ;
107108 buffer[ size - 1 ] = b'0' + ( n % 10 ) as u8 ;
108109
109- check_hash ( & mut buffer, size, offset + n, shared, mutex ) ;
110+ check_hash ( & mut buffer, size, offset + n, shared) ;
110111 }
111112 }
112113}
@@ -124,7 +125,6 @@ mod simd {
124125 start : u32 ,
125126 offset : u32 ,
126127 shared : & Shared ,
127- mutex : & Mutex < Exclusive > ,
128128 ) where
129129 LaneCount < N > : SupportedLaneCount ,
130130 {
@@ -140,7 +140,7 @@ mod simd {
140140
141141 for i in 0 ..N {
142142 if result[ i] & 0xfffff000 == 0 {
143- let mut exclusive = mutex. lock ( ) . unwrap ( ) ;
143+ let mut exclusive = shared . mutex . lock ( ) . unwrap ( ) ;
144144
145145 exclusive. found . push ( ( start + offset + i as u32 , result[ i] ) ) ;
146146 exclusive. mask |= 1 << ( result[ i] >> 8 ) ;
@@ -152,17 +152,17 @@ mod simd {
152152 }
153153 }
154154
155- pub ( super ) fn worker ( shared : & Shared , mutex : & Mutex < Exclusive > ) {
155+ pub ( super ) fn worker ( shared : & Shared ) {
156156 while !shared. done . load ( Ordering :: Relaxed ) {
157157 let start = shared. counter . fetch_add ( 1000 , Ordering :: Relaxed ) ;
158158 let ( prefix, size) = format_string ( & shared. prefix , start) ;
159159 let mut buffers = [ prefix; 32 ] ;
160160
161161 for offset in ( 0 ..992 ) . step_by ( 32 ) {
162- check_hash_simd :: < 32 > ( & mut buffers, size, start, offset, shared, mutex ) ;
162+ check_hash_simd :: < 32 > ( & mut buffers, size, start, offset, shared) ;
163163 }
164164
165- check_hash_simd :: < 8 > ( & mut buffers, size, start, 992 , shared, mutex ) ;
165+ check_hash_simd :: < 8 > ( & mut buffers, size, start, 992 , shared) ;
166166 }
167167 }
168168}
0 commit comments