From 5d19387a438af8cb2aaf5d54841544ff18540709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=C3=A1=C5=A1=20Fiala?= Date: Mon, 15 Jan 2024 15:36:00 +0100 Subject: [PATCH] Use non-zero ints to allow Option optimisations --- smt-log-parser/src/error.rs | 3 ++- smt-log-parser/src/items.rs | 10 +++++----- smt-log-parser/src/parsers/z3/inst.rs | 8 +++++--- smt-log-parser/src/parsers/z3/z3parser.rs | 4 ++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/smt-log-parser/src/error.rs b/smt-log-parser/src/error.rs index 37635616..4fdde903 100644 --- a/smt-log-parser/src/error.rs +++ b/smt-log-parser/src/error.rs @@ -1,6 +1,6 @@ use std::{collections::TryReserveError, num::ParseIntError}; -use crate::items::{TermId, TermIdx, StackIdx, ENodeIdx, BlameKind}; +use crate::items::{TermId, TermIdx, StackIdx, ENodeIdx, BlameKind, Fingerprint}; pub type Result = std::result::Result; pub type FResult = std::result::Result; @@ -44,6 +44,7 @@ pub enum Error { // Fingerprint InvalidFingerprint(ParseIntError), + UnknownFingerprint(Fingerprint), // Enode UnknownEnode(TermIdx), diff --git a/smt-log-parser/src/items.rs b/smt-log-parser/src/items.rs index 60249ad4..d7069941 100644 --- a/smt-log-parser/src/items.rs +++ b/smt-log-parser/src/items.rs @@ -2,7 +2,7 @@ use fxhash::FxHashMap; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use std::fmt; -use std::num::NonZeroU32; +use std::num::{NonZeroU32, NonZeroUsize}; use crate::{Result, Error}; pub type StringTable = lasso::Rodeo; @@ -12,17 +12,17 @@ pub type IString = lasso::Spur; macro_rules! idx { ($struct:ident, $prefix:tt) => { #[derive( - Clone, Copy, Default, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Hash, + Clone, Copy, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Hash, )] - pub struct $struct(usize); + pub struct $struct(NonZeroUsize); impl From for $struct { fn from(value: usize) -> Self { - Self(value) + Self(NonZeroUsize::new(value.checked_add(1).unwrap()).unwrap()) } } impl From<$struct> for usize { fn from(value: $struct) -> Self { - value.0 + value.0.get() - 1 } } impl fmt::Debug for $struct { diff --git a/smt-log-parser/src/parsers/z3/inst.rs b/smt-log-parser/src/parsers/z3/inst.rs index 2754bc49..3e550a29 100644 --- a/smt-log-parser/src/parsers/z3/inst.rs +++ b/smt-log-parser/src/parsers/z3/inst.rs @@ -25,12 +25,14 @@ impl Insts { Ok(idx) } - pub fn new_inst(&mut self, fingerprint: Fingerprint, mut inst: Instantiation) -> Result { - let (match_idx, inst_idx) = self + pub fn get_match(&self, fingerprint: Fingerprint) -> Option { + self.fingerprint_to_match.get(&fingerprint).map(|(idx, _)| *idx) + } + pub fn new_inst(&mut self, fingerprint: Fingerprint, inst: Instantiation) -> Result { + let (_, inst_idx) = self .fingerprint_to_match .get_mut(&fingerprint) .expect(&format!("{:x}", fingerprint.0)); - inst.match_ = *match_idx; self.insts.raw.try_reserve(1)?; let idx = self.insts.push_and_get_key(inst); debug_assert!(inst_idx.is_none(), "duplicate fingerprint"); diff --git a/smt-log-parser/src/parsers/z3/z3parser.rs b/smt-log-parser/src/parsers/z3/z3parser.rs index 64f3f548..b55be69c 100644 --- a/smt-log-parser/src/parsers/z3/z3parser.rs +++ b/smt-log-parser/src/parsers/z3/z3parser.rs @@ -503,9 +503,9 @@ impl Z3LogParser for Z3Parser { Self::expect_completed(proof)?; let z3_generation = Self::parse_z3_generation(&mut l)?; + let match_ = self.insts.get_match(fingerprint).ok_or(Error::UnknownFingerprint(fingerprint))?; let inst = Instantiation { - // Will be filled in by `new_inst` - match_: MatchIdx::default(), + match_, fingerprint, proof_id, z3_generation,