Skip to content

Commit 2cdc019

Browse files
authored
Merge updates needed for SHA with lookups. (#196)
This is highly unoptimized, for now.
1 parent aa318e5 commit 2cdc019

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+2300
-330
lines changed

circ_fields/src/lib.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -371,14 +371,22 @@ impl PartialOrd for FieldV {
371371
impl Ord for FieldV {
372372
#[inline]
373373
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
374-
self.full_cow().cmp(&other.full_cow())
374+
if self.is_full() || other.is_full() {
375+
self.full_cow().cmp(&other.full_cow())
376+
} else {
377+
self.inline_i64().cmp(&other.inline_i64())
378+
}
375379
}
376380
}
377381

378382
impl PartialEq for FieldV {
379383
#[inline]
380384
fn eq(&self, other: &Self) -> bool {
381-
self.full_cow().eq(&other.full_cow())
385+
if self.is_full() || other.is_full() {
386+
self.full_cow().eq(&other.full_cow())
387+
} else {
388+
self.inline_i64().eq(&other.inline_i64())
389+
}
382390
}
383391
}
384392

circ_hc/src/hashconsing/example_u8.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl crate::Table<u8> for Table {
4949
"hashconsing"
5050
}
5151

52-
fn for_each(f: impl FnMut(&u8, &[Self::Node])) {
52+
fn for_each(_f: impl FnMut(&u8, &[Self::Node])) {
5353
panic!()
5454
}
5555

circ_hc/src/hashconsing/macro_.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ macro_rules! generate_hashcons_hashconsing {
5252
"hashconsing"
5353
}
5454

55-
fn for_each(f: impl FnMut(&$Op, &[Self::Node])) {
55+
fn for_each(_f: impl FnMut(&$Op, &[Self::Node])) {
5656
panic!()
5757
}
5858

circ_hc/src/hashconsing/template.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl crate::Table<TemplateOp> for Table {
5050
"hashconsing"
5151
}
5252

53-
fn for_each(f: impl FnMut(&TemplateOp, &[Self::Node])) {
53+
fn for_each(_f: impl FnMut(&TemplateOp, &[Self::Node])) {
5454
panic!()
5555
}
5656

circ_hc/src/rc/example_u8.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@ use fxhash::FxHashMap as HashMap;
33

44
use crate::Id;
55
use log::trace;
6-
use std::borrow::Borrow;
76
use std::cell::{Cell, RefCell};
87
use std::rc::Rc;
8+
use std::sync::atomic::AtomicU64;
99
use std::thread_local;
1010

1111
#[allow(dead_code)]
1212
struct NodeData {
1313
op: u8,
14+
hash: AtomicU64,
1415
cs: Box<[Node]>,
1516
}
1617

17-
#[allow(dead_code)]
18-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a u8, &'a Q);
19-
2018
#[derive(Clone)]
2119
pub struct Node {
2220
data: Rc<NodeData>,
@@ -158,6 +156,7 @@ impl Manager {
158156
let mut table = self.table.borrow_mut();
159157
let data = Rc::new(NodeData {
160158
op: op.clone(),
159+
hash: Default::default(),
161160
cs: children.into(),
162161
});
163162

@@ -218,10 +217,10 @@ impl Manager {
218217
let duration = start.elapsed();
219218
self.in_gc.set(false);
220219
trace!(
221-
"GC: {} terms -> {} terms in {} us",
220+
"GC: {} terms -> {} terms in {} ns",
222221
collected,
223222
new_size,
224-
duration.as_micros()
223+
duration.as_nanos()
225224
);
226225
collected
227226
} else {
@@ -296,37 +295,45 @@ impl crate::Weak<u8> for Weak {
296295
}
297296

298297
mod hash {
299-
use super::{Node, NodeData, NodeDataRef, Weak};
300-
use std::borrow::Borrow;
298+
use super::{Node, NodeData, Weak};
299+
use fxhash::FxHasher;
301300
use std::hash::{Hash, Hasher};
301+
use std::sync::atomic::Ordering::SeqCst;
302302

303303
impl Hash for Node {
304+
#[inline]
304305
fn hash<H: Hasher>(&self, state: &mut H) {
305306
self.id.hash(state)
306307
}
307308
}
308309

309310
impl Hash for Weak {
311+
#[inline]
310312
fn hash<H: Hasher>(&self, state: &mut H) {
311313
self.id.hash(state)
312314
}
313315
}
314316

315-
impl Hash for NodeData {
316-
fn hash<H: Hasher>(&self, state: &mut H) {
317-
self.op.hash(state);
317+
impl NodeData {
318+
fn rehash(&self) -> u64 {
319+
let mut hasher = FxHasher::default();
320+
self.op.hash(&mut hasher);
318321
for c in self.cs.iter() {
319-
c.hash(state);
322+
c.hash(&mut hasher);
320323
}
324+
let current_hash = hasher.finish();
325+
self.hash.store(current_hash, SeqCst);
326+
current_hash
321327
}
322328
}
323-
324-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
329+
impl Hash for NodeData {
330+
#[inline]
325331
fn hash<H: Hasher>(&self, state: &mut H) {
326-
self.0.hash(state);
327-
for c in self.1.borrow().iter() {
328-
c.hash(state);
332+
let mut current_hash: u64 = self.hash.load(SeqCst);
333+
if current_hash == 0 {
334+
current_hash = self.rehash();
329335
}
336+
state.write_u64(current_hash);
330337
}
331338
}
332339
}

circ_hc/src/rc/macro_.rs

+24-17
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,19 @@ macro_rules! generate_hashcons_rc {
55
use fxhash::FxHashMap as HashMap;
66

77
use log::trace;
8-
use std::borrow::Borrow;
98
use std::cell::{Cell, RefCell};
109
use std::rc::Rc;
10+
use std::sync::atomic::AtomicU64;
1111
use std::thread_local;
1212
use $crate::Id;
1313

1414
#[allow(dead_code)]
1515
struct NodeData {
1616
op: $Op,
17+
hash: AtomicU64,
1718
cs: Box<[Node]>,
1819
}
1920

20-
#[allow(dead_code)]
21-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a $Op, &'a Q);
22-
2321
#[derive(Clone)]
2422
pub struct Node {
2523
data: Rc<NodeData>,
@@ -161,6 +159,7 @@ macro_rules! generate_hashcons_rc {
161159
let mut table = self.table.borrow_mut();
162160
let data = Rc::new(NodeData {
163161
op: op.clone(),
162+
hash: Default::default(),
164163
cs: children.into(),
165164
});
166165

@@ -221,10 +220,10 @@ macro_rules! generate_hashcons_rc {
221220
let duration = start.elapsed();
222221
self.in_gc.set(false);
223222
trace!(
224-
"GC: {} terms -> {} terms in {} us",
223+
"GC: {} terms -> {} terms in {} ns",
225224
collected,
226225
new_size,
227-
duration.as_micros()
226+
duration.as_nanos()
228227
);
229228
collected
230229
} else {
@@ -299,37 +298,45 @@ macro_rules! generate_hashcons_rc {
299298
}
300299

301300
mod hash {
302-
use super::{Node, NodeData, NodeDataRef, Weak};
303-
use std::borrow::Borrow;
301+
use super::{Node, NodeData, Weak};
302+
use fxhash::FxHasher;
304303
use std::hash::{Hash, Hasher};
304+
use std::sync::atomic::Ordering::SeqCst;
305305

306306
impl Hash for Node {
307+
#[inline]
307308
fn hash<H: Hasher>(&self, state: &mut H) {
308309
self.id.hash(state)
309310
}
310311
}
311312

312313
impl Hash for Weak {
314+
#[inline]
313315
fn hash<H: Hasher>(&self, state: &mut H) {
314316
self.id.hash(state)
315317
}
316318
}
317319

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);
321324
for c in self.cs.iter() {
322-
c.hash(state);
325+
c.hash(&mut hasher);
323326
}
327+
let current_hash = hasher.finish();
328+
self.hash.store(current_hash, SeqCst);
329+
current_hash
324330
}
325331
}
326-
327-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
332+
impl Hash for NodeData {
333+
#[inline]
328334
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();
332338
}
339+
state.write_u64(current_hash);
333340
}
334341
}
335342
}

circ_hc/src/rc/template.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,19 @@ use fxhash::FxHashMap as HashMap;
22

33
use crate::Id;
44
use log::trace;
5-
use std::borrow::Borrow;
65
use std::cell::{Cell, RefCell};
76
use std::net::SocketAddrV6 as TemplateOp;
87
use std::rc::Rc;
8+
use std::sync::atomic::AtomicU64;
99
use std::thread_local;
1010

1111
#[allow(dead_code)]
1212
struct NodeData {
1313
op: TemplateOp,
14+
hash: AtomicU64,
1415
cs: Box<[Node]>,
1516
}
1617

17-
#[allow(dead_code)]
18-
struct NodeDataRef<'a, Q: Borrow<[Node]>>(&'a TemplateOp, &'a Q);
19-
2018
#[derive(Clone)]
2119
pub struct Node {
2220
data: Rc<NodeData>,
@@ -158,6 +156,7 @@ impl Manager {
158156
let mut table = self.table.borrow_mut();
159157
let data = Rc::new(NodeData {
160158
op: op.clone(),
159+
hash: Default::default(),
161160
cs: children.into(),
162161
});
163162

@@ -218,10 +217,10 @@ impl Manager {
218217
let duration = start.elapsed();
219218
self.in_gc.set(false);
220219
trace!(
221-
"GC: {} terms -> {} terms in {} us",
220+
"GC: {} terms -> {} terms in {} ns",
222221
collected,
223222
new_size,
224-
duration.as_micros()
223+
duration.as_nanos()
225224
);
226225
collected
227226
} else {
@@ -296,37 +295,46 @@ impl crate::Weak<TemplateOp> for Weak {
296295
}
297296

298297
mod hash {
299-
use super::{Node, NodeData, NodeDataRef, Weak};
300-
use std::borrow::Borrow;
298+
use super::{Node, NodeData, Weak};
299+
use fxhash::FxHasher;
301300
use std::hash::{Hash, Hasher};
301+
use std::sync::atomic::Ordering::SeqCst;
302302

303303
impl Hash for Node {
304+
#[inline]
304305
fn hash<H: Hasher>(&self, state: &mut H) {
305306
self.id.hash(state)
306307
}
307308
}
308309

309310
impl Hash for Weak {
311+
#[inline]
310312
fn hash<H: Hasher>(&self, state: &mut H) {
311313
self.id.hash(state)
312314
}
313315
}
314316

315-
impl Hash for NodeData {
316-
fn hash<H: Hasher>(&self, state: &mut H) {
317-
self.op.hash(state);
317+
impl NodeData {
318+
fn rehash(&self) -> u64 {
319+
let mut hasher = FxHasher::default();
320+
self.op.hash(&mut hasher);
318321
for c in self.cs.iter() {
319-
c.hash(state);
322+
c.hash(&mut hasher);
320323
}
324+
let current_hash = hasher.finish();
325+
self.hash.store(current_hash, SeqCst);
326+
current_hash
321327
}
322328
}
323329

324-
impl<'a, Q: Borrow<[Node]>> Hash for NodeDataRef<'a, Q> {
330+
impl Hash for NodeData {
331+
#[inline]
325332
fn hash<H: Hasher>(&self, state: &mut H) {
326-
self.0.hash(state);
327-
for c in self.1.borrow().iter() {
328-
c.hash(state);
333+
let mut current_hash: u64 = self.hash.load(SeqCst);
334+
if current_hash == 0 {
335+
current_hash = self.rehash();
329336
}
337+
state.write_u64(current_hash);
330338
}
331339
}
332340
}

circ_opt/src/lib.rs

+9
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,14 @@ pub struct IrOpt {
194194
default_value = "true"
195195
)]
196196
pub fits_in_bits_ip: bool,
197+
/// Time operator evaluations
198+
#[arg(
199+
long = "ir-time-eval-ops",
200+
env = "IR_TIME_EVAL_OPS",
201+
action = ArgAction::Set,
202+
default_value = "false"
203+
)]
204+
pub time_eval_ops: bool,
197205
}
198206

199207
impl Default for IrOpt {
@@ -202,6 +210,7 @@ impl Default for IrOpt {
202210
field_to_bv: Default::default(),
203211
frequent_gc: Default::default(),
204212
fits_in_bits_ip: true,
213+
time_eval_ops: false,
205214
}
206215
}
207216
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(set_default_modulus 52435875175126190479447740508185965837690552500527637822603658699938581184513
22
(let (
33
(x #f6)
4-
(return #f6)
4+
(return #f0)
55
) false ; ignored
66
))
77

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory contains a SHA256 implementation by Anna Woo.

0 commit comments

Comments
 (0)