@@ -5,21 +5,19 @@ macro_rules! generate_hashcons_rc {
5
5
use fxhash:: FxHashMap as HashMap ;
6
6
7
7
use log:: trace;
8
- use std:: borrow:: Borrow ;
9
8
use std:: cell:: { Cell , RefCell } ;
10
9
use std:: rc:: Rc ;
10
+ use std:: sync:: atomic:: AtomicU64 ;
11
11
use std:: thread_local;
12
12
use $crate:: Id ;
13
13
14
14
#[ allow( dead_code) ]
15
15
struct NodeData {
16
16
op: $Op,
17
+ hash: AtomicU64 ,
17
18
cs: Box <[ Node ] >,
18
19
}
19
20
20
- #[ allow( dead_code) ]
21
- struct NodeDataRef <' a, Q : Borrow <[ Node ] >>( & ' a $Op, & ' a Q ) ;
22
-
23
21
#[ derive( Clone ) ]
24
22
pub struct Node {
25
23
data: Rc <NodeData >,
@@ -161,6 +159,7 @@ macro_rules! generate_hashcons_rc {
161
159
let mut table = self . table. borrow_mut( ) ;
162
160
let data = Rc :: new( NodeData {
163
161
op: op. clone( ) ,
162
+ hash: Default :: default ( ) ,
164
163
cs: children. into( ) ,
165
164
} ) ;
166
165
@@ -299,37 +298,45 @@ macro_rules! generate_hashcons_rc {
299
298
}
300
299
301
300
mod hash {
302
- use super :: { Node , NodeData , NodeDataRef , Weak } ;
303
- use std :: borrow :: Borrow ;
301
+ use super :: { Node , NodeData , Weak } ;
302
+ use fxhash :: FxHasher ;
304
303
use std:: hash:: { Hash , Hasher } ;
304
+ use std:: sync:: atomic:: Ordering :: SeqCst ;
305
305
306
306
impl Hash for Node {
307
+ #[ inline]
307
308
fn hash<H : Hasher >( & self , state: & mut H ) {
308
309
self . id. hash( state)
309
310
}
310
311
}
311
312
312
313
impl Hash for Weak {
314
+ #[ inline]
313
315
fn hash<H : Hasher >( & self , state: & mut H ) {
314
316
self . id. hash( state)
315
317
}
316
318
}
317
319
318
- impl Hash for NodeData {
319
- fn hash<H : Hasher >( & self , state: & mut H ) {
320
- self . op. hash( state) ;
320
+ impl NodeData {
321
+ fn rehash( & self ) -> u64 {
322
+ let mut hasher = FxHasher :: default ( ) ;
323
+ self . op. hash( & mut hasher) ;
321
324
for c in self . cs. iter( ) {
322
- c. hash( state ) ;
325
+ c. hash( & mut hasher ) ;
323
326
}
327
+ let current_hash = hasher. finish( ) ;
328
+ self . hash. store( current_hash, SeqCst ) ;
329
+ current_hash
324
330
}
325
331
}
326
-
327
- impl < ' a , Q : Borrow < [ Node ] >> Hash for NodeDataRef < ' a , Q > {
332
+ impl Hash for NodeData {
333
+ # [ inline ]
328
334
fn hash<H : Hasher >( & self , state: & mut H ) {
329
- self . 0 . hash( state ) ;
330
- for c in self . 1 . borrow ( ) . iter ( ) {
331
- c . hash ( state ) ;
335
+ let mut current_hash : u64 = self . hash. load ( SeqCst ) ;
336
+ if current_hash == 0 {
337
+ current_hash = self . rehash ( ) ;
332
338
}
339
+ state. write_u64( current_hash) ;
333
340
}
334
341
}
335
342
}
0 commit comments