From 01eb4ffeb384199f692352077d460015e7c6a48e Mon Sep 17 00:00:00 2001 From: T6 Date: Fri, 23 Feb 2024 17:35:36 -0500 Subject: [PATCH] optimize op2 with immediate num rhs (#63) --- src/run.rs | 22 +++++++++++++++------- src/util.rs | 7 +++++++ 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/run.rs b/src/run.rs index 0a0f0cda..15e9426f 100644 --- a/src/run.rs +++ b/src/run.rs @@ -206,7 +206,7 @@ impl Port { /// Accesses the tag of this port; this is valid for all ports. #[inline(always)] pub fn tag(&self) -> Tag { - unsafe { ((self.0 & 0x7) as u8).try_into().unwrap_unchecked() } + unsafe { Tag::from_unchecked((self.0 & 0x7) as u8) } } /// Accesses the label of this port; this is valid for all non-`Num` ports. @@ -225,7 +225,7 @@ impl Port { /// ports. #[inline(always)] pub fn op(&self) -> Op { - unsafe { self.lab().try_into().unwrap_unchecked() } + unsafe { Op::from_unchecked(self.lab()) } } /// Accesses the numeric value of this port; this is valid for [`Num`] ports. @@ -1704,11 +1704,19 @@ impl<'a, M: Mode> Net<'a, M> { trace!(self.tracer, a, b); self.rwts.oper += 1; let a = a.consume_node(); - let x = self.create_node(Op1, a.lab); - trace!(self.tracer, x.p0); - self.link_port_port(x.p1, b); - self.link_wire_port(a.p2, x.p2); - self.link_wire_port(a.p1, x.p0); + let a1 = a.p1.load_target(); + if a1.tag() == Num { + // skip creating the Op1 node if it will instantly interact + self.rwts.oper += 1; + let out = unsafe { Op::from_unchecked(a.lab) }.op(b.num(), a1.num()); + self.link_wire_port(a.p2, Port::new_num(out)); + } else { + let x = self.create_node(Op1, a.lab); + trace!(self.tracer, x.p0); + self.link_port_port(x.p1, b); + self.link_wire_port(a.p2, x.p2); + self.link_wire_port(a.p1, x.p0); + } } /// Interacts a number and a unary numeric operation node. diff --git a/src/util.rs b/src/util.rs index 5e986878..61f5d989 100644 --- a/src/util.rs +++ b/src/util.rs @@ -48,6 +48,13 @@ macro_rules! bi_enum { } } + impl $Ty { + #[allow(unused)] + pub unsafe fn from_unchecked(value: $uN) -> $Ty { + Self::try_from(value).unwrap_unchecked() + } + } + impl From<$Ty> for $uN { fn from(value: $Ty) -> Self { value as Self } }