Skip to content

Commit

Permalink
Use let-else syntax more (#1148)
Browse files Browse the repository at this point in the history
* refactor: avoid distant else, convert to `and_then`

- Simplified return flow in `InversePoseidonCache` `get` method in `hash.rs` using `and_then`.
- Refactored variable allocation within `from_ptr` method in `memoset/env.rs`.
- Made minor clean-up and formatting improvements to improve readability across multiple files.

* refactor: convert divergent if .. let to let .. else

* refactor: Refactor if let .. else into let .. else where relevant in store.rs

- Simplify code logic by replacing `if let` syntax with `let ... else`.

* refactor: transform match on option to let-else where relevant
  • Loading branch information
huitseeker authored Feb 19, 2024
1 parent f2d70f4 commit afa34a2
Show file tree
Hide file tree
Showing 6 changed files with 214 additions and 249 deletions.
11 changes: 5 additions & 6 deletions lurk-metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,12 @@ impl ThreadMetricsSinkHandle {

/// Initialize the thread-local metrics sink by registering it with the global sink
fn init() -> ThreadMetricsSinkHandle {
if let Some(global_sink) = GLOBAL_SINK.get() {
let me = Arc::new(Mutex::new(ThreadMetricsSink::default()));
global_sink.threads.lock().unwrap().push(Arc::clone(&me));
ThreadMetricsSinkHandle { inner: me }
} else {
let Some(global_sink) = GLOBAL_SINK.get() else {
panic!("global metrics sink must be installed first");
}
};
let me = Arc::new(Mutex::new(ThreadMetricsSink::default()));
global_sink.threads.lock().unwrap().push(Arc::clone(&me));
ThreadMetricsSinkHandle { inner: me }
}
}

Expand Down
24 changes: 9 additions & 15 deletions src/coroutine/memoset/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,16 @@ impl<F: LurkField> CircuitQuery<F> for EnvCircuitQuery<F> {

fn from_ptr<CS: ConstraintSystem<F>>(cs: &mut CS, s: &Store<F>, ptr: &Ptr) -> Option<Self> {
let query = EnvQuery::from_ptr(s, ptr);
if let Some(q) = query {
match q {
EnvQuery::Lookup(var, env) => {
let allocated_var = AllocatedNum::alloc_infallible(ns!(cs, "var"), || {
*s.hash_ptr(&var).value()
});
let allocated_env = AllocatedNum::alloc_infallible(ns!(cs, "env"), || {
*s.hash_ptr(&env).value()
});
Some(Self::Lookup(allocated_var, allocated_env))
}
_ => unreachable!(),
query.and_then(|q| match q {
EnvQuery::Lookup(var, env) => {
let allocated_var =
AllocatedNum::alloc_infallible(ns!(cs, "var"), || *s.hash_ptr(&var).value());
let allocated_env =
AllocatedNum::alloc_infallible(ns!(cs, "env"), || *s.hash_ptr(&env).value());
Some(Self::Lookup(allocated_var, allocated_env))
}
} else {
None
}
_ => unreachable!(),
})
}

fn dummy_from_index<CS: ConstraintSystem<F>>(cs: &mut CS, s: &Store<F>, index: usize) -> Self {
Expand Down
8 changes: 2 additions & 6 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,10 @@ impl<F: LurkField> InversePoseidonCache<F> {
macro_rules! get {
($name:ident, $n: expr) => {{
let preimage = self.$name.get(key);
if let Some(p) = preimage {
assert_eq!(ARITY, $n);
preimage.and_then(|p | { assert_eq!(ARITY, $n);
// SAFETY: we are just teaching the compiler that the slice has size, ARITY, which is guaranteed by
// the assertion above.
Some(unsafe { std::mem::transmute::<&[F; $n], &[F; ARITY]>(p) })
} else {
None
}
Some(unsafe { std::mem::transmute::<&[F; $n], &[F; ARITY]>(p) }) })
}};
}

Expand Down
14 changes: 6 additions & 8 deletions src/lem/multiframe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,12 @@ impl<'a, F: LurkField, C: Coprocessor<F>> MultiFrame<'a, F, C> {
inner_frames.push(frames[current_frame_idx].clone());
chunk_start_idx = current_frame_idx + 1;

if let Some(next_frame) = frames.get(chunk_start_idx) {
next_pc = next_frame.pc;
if next_pc != 0 {
// incompatible `pc` incoming
break;
}
} else {
// not enough frames
let Some(next_frame) = frames.get(chunk_start_idx) else {
break;
};
next_pc = next_frame.pc;
if next_pc != 0 {
// incompatible `pc` incoming
break;
}
}
Expand Down
Loading

1 comment on commit afa34a2

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
32 vCPUs
125 GB RAM
Workflow run: https://github.com/lurk-lab/lurk-rs/actions/runs/7953072176

Benchmark Results

LEM Fibonacci Prove - rc = 100

ref=f2d70f4f7c295cde6f07ae391f601a0d49c3965e ref=afa34a24d8209b1f1a92e453e37892c98ca2df42
num-100 1.45 s (✅ 1.00x) 1.45 s (✅ 1.00x faster)
num-200 2.77 s (✅ 1.00x) 2.77 s (✅ 1.00x faster)

LEM Fibonacci Prove - rc = 600

ref=f2d70f4f7c295cde6f07ae391f601a0d49c3965e ref=afa34a24d8209b1f1a92e453e37892c98ca2df42
num-100 1.84 s (✅ 1.00x) 1.85 s (✅ 1.01x slower)
num-200 3.04 s (✅ 1.00x) 3.04 s (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.