From 1879d74fac95ca682a97d62b425b801629bed394 Mon Sep 17 00:00:00 2001 From: Alain Emilia Anna Zscheile Date: Wed, 11 Dec 2024 00:24:57 +0100 Subject: [PATCH] refactor(specctra/design): avoid double-lookup into net_outs --- src/specctra/design.rs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/specctra/design.rs b/src/specctra/design.rs index 952bffd..3590392 100644 --- a/src/specctra/design.rs +++ b/src/specctra/design.rs @@ -3,7 +3,7 @@ //! exporting the session file use geo::{point, Point, Rotate}; -use std::collections::HashMap; +use std::collections::{hash_map::Entry as HashMapEntry, HashMap}; use crate::{ board::{mesadata::AccessMesadata, Board}, @@ -136,26 +136,23 @@ impl SpecctraDesign { }, }; - if let Some(net) = net_outs.get_mut(&net) { - net.wire.push(wire); - } else { - net_outs.insert( - net, - structure::NetOut { - name: mesadata - .net_netname(net) - .ok_or_else(|| { - std::io::Error::new( - std::io::ErrorKind::InvalidData, - format!("tried to reference invalid net ID {}", net), - ) - })? - .to_owned(), - wire: vec![wire], - via: Vec::new(), - }, - ); - } + let net_out = match net_outs.entry(net) { + HashMapEntry::Occupied(occ) => occ.into_mut(), + HashMapEntry::Vacant(vac) => vac.insert(structure::NetOut { + name: mesadata + .net_netname(net) + .ok_or_else(|| { + std::io::Error::new( + std::io::ErrorKind::InvalidData, + format!("tried to reference invalid net ID {}", net), + ) + })? + .to_owned(), + wire: Vec::new(), + via: Vec::new(), + }), + }; + net_out.wire.push(wire); } }