Skip to content

Commit

Permalink
perf: use lazy static definittion for prelude registry (#481)
Browse files Browse the repository at this point in the history
and avoid clone
  • Loading branch information
ss2165 authored Sep 1, 2023
1 parent 2b05270 commit 1b83640
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 39 deletions.
6 changes: 3 additions & 3 deletions src/builder/build_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
types::EdgeKind,
};

use crate::extension::{prelude_registry, ExtensionRegistry, ExtensionSet};
use crate::extension::{ExtensionRegistry, ExtensionSet, PRELUDE_REGISTRY};
use crate::types::{FunctionType, Signature, Type, TypeRow};

use itertools::Itertools;
Expand Down Expand Up @@ -138,7 +138,7 @@ pub trait HugrBuilder: Container {
where
Self: Sized,
{
self.finish_hugr(&prelude_registry())
self.finish_hugr(&PRELUDE_REGISTRY)
}
}

Expand Down Expand Up @@ -739,7 +739,7 @@ pub trait DataflowHugr: HugrBuilder + Dataflow {
where
Self: Sized,
{
self.finish_hugr_with_outputs(outputs, &prelude_registry())
self.finish_hugr_with_outputs(outputs, &PRELUDE_REGISTRY)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use type_def::{TypeDef, TypeDefBound};
pub mod prelude;
pub mod validate;

pub use prelude::{prelude_registry, PRELUDE};
pub use prelude::{PRELUDE, PRELUDE_REGISTRY};

/// Extension Registries store extensions to be looked up e.g. during validation.
pub struct ExtensionRegistry(BTreeMap<SmolStr, Extension>);
Expand All @@ -39,7 +39,7 @@ impl ExtensionRegistry {
}

/// Gets the Extension with the given name
pub fn get(&self, name: &SmolStr) -> Option<&Extension> {
pub fn get(&self, name: &str) -> Option<&Extension> {
self.0.get(name)
}
}
Expand Down
22 changes: 12 additions & 10 deletions src/extension/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ use crate::{

use super::ExtensionRegistry;

/// Name of prelude extension.
pub const PRELUDE_ID: &str = "prelude";
lazy_static! {
/// Prelude extension
pub static ref PRELUDE: Extension = {
let mut prelude = Extension::new(SmolStr::new_inline("prelude"));
static ref PRELUDE_DEF: Extension = {
let mut prelude = Extension::new(SmolStr::new_inline(PRELUDE_ID));
prelude
.add_type(
SmolStr::new_inline("usize"),
Expand Down Expand Up @@ -48,22 +49,23 @@ lazy_static! {
.unwrap();
prelude
};
}
/// An extension registry containing only the prelude
pub static ref PRELUDE_REGISTRY: ExtensionRegistry = [PRELUDE_DEF.to_owned()].into();

/// Prelude extension
pub static ref PRELUDE: &'static Extension = PRELUDE_REGISTRY.get(PRELUDE_ID).unwrap();

/// An extension registry containing only the prelude
pub fn prelude_registry() -> ExtensionRegistry {
[PRELUDE.to_owned()].into()
}

pub(crate) const USIZE_CUSTOM_T: CustomType = CustomType::new_simple(
SmolStr::new_inline("usize"),
SmolStr::new_inline("prelude"),
SmolStr::new_inline(PRELUDE_ID),
TypeBound::Eq,
);

pub(crate) const QB_CUSTOM_T: CustomType = CustomType::new_simple(
SmolStr::new_inline("qubit"),
SmolStr::new_inline("prelude"),
SmolStr::new_inline(PRELUDE_ID),
TypeBound::Any,
);

Expand All @@ -85,7 +87,7 @@ pub fn new_array(typ: Type, size: u64) -> Type {

pub(crate) const ERROR_TYPE: Type = Type::new_extension(CustomType::new_simple(
smol_str::SmolStr::new_inline("error"),
smol_str::SmolStr::new_inline("prelude"),
smol_str::SmolStr::new_inline(PRELUDE_ID),
TypeBound::Eq,
));

Expand Down
4 changes: 2 additions & 2 deletions src/hugr/hugrmut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ pub(crate) mod sealed {
mod test {
use crate::{
extension::prelude::USIZE_T,
extension::prelude_registry,
extension::PRELUDE_REGISTRY,
hugr::HugrView,
macros::type_row,
ops::{self, dataflow::IOTrait, LeafOp},
Expand Down Expand Up @@ -523,6 +523,6 @@ mod test {
}

// Finish the construction and create the HUGR
builder.validate(&prelude_registry()).unwrap();
builder.validate(&PRELUDE_REGISTRY).unwrap();
}
}
4 changes: 2 additions & 2 deletions src/hugr/rewrite/insert_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod tests {
use super::*;
use crate::{
algorithm::nest_cfgs::test::build_conditional_in_loop_cfg,
extension::{prelude::QB_T, prelude_registry},
extension::{prelude::QB_T, PRELUDE_REGISTRY},
ops::handle::NodeHandle,
Hugr,
};
Expand Down Expand Up @@ -135,7 +135,7 @@ mod tests {

assert_eq!(noop, LeafOp::Noop { ty: QB_T });

h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
}

#[test]
Expand Down
16 changes: 8 additions & 8 deletions src/hugr/rewrite/outline_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use itertools::Itertools;
use thiserror::Error;

use crate::builder::{BlockBuilder, Container, Dataflow, SubContainer};
use crate::extension::prelude_registry;
use crate::extension::PRELUDE_REGISTRY;
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::hugr::rewrite::Rewrite;
use crate::hugr::{HugrMut, HugrView};
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Rewrite for OutlineCfg {
.unwrap();
let pred_wire = new_block_bldr.load_const(&predicate).unwrap();
let new_block_hugr = new_block_bldr
.finish_hugr_with_outputs(pred_wire, cfg_outputs, &prelude_registry())
.finish_hugr_with_outputs(pred_wire, cfg_outputs, &PRELUDE_REGISTRY)
.unwrap();
h.insert_hugr(outer_cfg, new_block_hugr).unwrap()
};
Expand Down Expand Up @@ -211,7 +211,7 @@ mod test {
use crate::algorithm::nest_cfgs::test::{
build_cond_then_loop_cfg, build_conditional_in_loop_cfg,
};
use crate::extension::prelude_registry;
use crate::extension::PRELUDE_REGISTRY;
use crate::ops::handle::NodeHandle;
use crate::{HugrView, Node};
use cool_asserts::assert_matches;
Expand All @@ -237,7 +237,7 @@ mod test {
// \---<---<---<---<---<--<---/
// merge is unique predecessor of tail
let merge = h.input_neighbours(tail).exactly_one().unwrap();
h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
let backup = h.clone();
let r = h.apply_rewrite(OutlineCfg::new([merge, tail]));
assert_matches!(r, Err(OutlineCfgError::MultipleExitEdges(_, _)));
Expand Down Expand Up @@ -270,10 +270,10 @@ mod test {
for n in [head, tail, merge] {
assert_eq!(depth(&h, n), 1);
}
h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
let blocks = [head, left, right, merge];
h.apply_rewrite(OutlineCfg::new(blocks)).unwrap();
h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
for n in blocks {
assert_eq!(depth(&h, n), 3);
}
Expand All @@ -300,15 +300,15 @@ mod test {
let (merge, tail) = (merge.node(), tail.node());
let head = h.output_neighbours(merge).exactly_one().unwrap();

h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
let blocks_to_move = [entry, left, right, merge];
let other_blocks = [head, tail, exit];
for &n in blocks_to_move.iter().chain(other_blocks.iter()) {
assert_eq!(depth(&h, n), 1);
}
h.apply_rewrite(OutlineCfg::new(blocks_to_move.iter().copied()))
.unwrap();
h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
let new_entry = h.children(h.root()).next().unwrap();
for n in other_blocks {
assert_eq!(depth(&h, n), 1);
Expand Down
6 changes: 3 additions & 3 deletions src/hugr/rewrite/simple_replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub(in crate::hugr::rewrite) mod test {
HugrBuilder, ModuleBuilder,
};
use crate::extension::prelude::BOOL_T;
use crate::extension::{prelude_registry, EMPTY_REG};
use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::views::HugrView;
use crate::hugr::{Hugr, Node};
use crate::ops::OpTag;
Expand Down Expand Up @@ -388,7 +388,7 @@ pub(in crate::hugr::rewrite) mod test {
// ├───┤├───┤┌─┴─┐
// ┤ H ├┤ H ├┤ X ├
// └───┘└───┘└───┘
assert_eq!(h.validate(&prelude_registry()), Ok(()));
assert_eq!(h.validate(&PRELUDE_REGISTRY), Ok(()));
}

#[rstest]
Expand Down Expand Up @@ -466,7 +466,7 @@ pub(in crate::hugr::rewrite) mod test {
// ├───┤├───┤┌───┐
// ┤ H ├┤ H ├┤ H ├
// └───┘└───┘└───┘
assert_eq!(h.validate(&prelude_registry()), Ok(()));
assert_eq!(h.validate(&PRELUDE_REGISTRY), Ok(()));
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions src/hugr/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl TryFrom<SerHugrV0> for Hugr {
pub mod test {

use super::*;
use crate::extension::{prelude_registry, EMPTY_REG};
use crate::extension::{EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::{
builder::{
Expand Down Expand Up @@ -466,12 +466,12 @@ pub mod test {
hugr.connect(new_in, 0, out, 0).unwrap();
hugr.move_before_sibling(new_in, old_in).unwrap();
hugr.remove_node(old_in).unwrap();
hugr.validate(&prelude_registry()).unwrap();
hugr.validate(&PRELUDE_REGISTRY).unwrap();

let ser = serde_json::to_vec(&hugr).unwrap();
let new_hugr: Hugr = serde_json::from_slice(&ser).unwrap();
new_hugr.validate(&EMPTY_REG).unwrap_err();
new_hugr.validate(&prelude_registry()).unwrap();
new_hugr.validate(&PRELUDE_REGISTRY).unwrap();

// Check the canonicalization works
let mut h_canon = hugr.clone();
Expand Down
12 changes: 6 additions & 6 deletions src/hugr/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,7 @@ mod test {
use super::*;
use crate::builder::{BuildError, Container, Dataflow, DataflowSubContainer, ModuleBuilder};
use crate::extension::prelude::{BOOL_T, PRELUDE, USIZE_T};
use crate::extension::{prelude_registry, Extension, ExtensionSet, TypeDefBound, EMPTY_REG};
use crate::extension::{Extension, ExtensionSet, TypeDefBound, EMPTY_REG, PRELUDE_REGISTRY};
use crate::hugr::hugrmut::sealed::HugrMutInternals;
use crate::hugr::{HugrError, HugrMut, NodeType};
use crate::ops::dataflow::IOTrait;
Expand Down Expand Up @@ -1131,7 +1131,7 @@ mod test {
let f_handle = f_builder.finish_with_outputs(f_inputs)?;
let [f_output] = f_handle.outputs_arr();
main.finish_with_outputs([f_output])?;
let handle = module_builder.hugr().validate(&prelude_registry());
let handle = module_builder.hugr().validate(&PRELUDE_REGISTRY);

assert_matches!(
handle,
Expand Down Expand Up @@ -1168,7 +1168,7 @@ mod test {
let f_handle = f_builder.finish_with_outputs(f_inputs)?;
let [f_output] = f_handle.outputs_arr();
main.finish_with_outputs([f_output])?;
let handle = module_builder.hugr().validate(&prelude_registry());
let handle = module_builder.hugr().validate(&PRELUDE_REGISTRY);
assert_matches!(
handle,
Err(ValidationError::ExtensionError(
Expand Down Expand Up @@ -1230,7 +1230,7 @@ mod test {
let [output] = builder.finish_with_outputs([])?.outputs_arr();

main.finish_with_outputs([output])?;
let handle = module_builder.hugr().validate(&prelude_registry());
let handle = module_builder.hugr().validate(&PRELUDE_REGISTRY);
assert_matches!(
handle,
Err(ValidationError::ExtensionError(
Expand Down Expand Up @@ -1268,7 +1268,7 @@ mod test {
hugr.connect(input, 0, output, 0)?;

assert_matches!(
hugr.validate(&prelude_registry()),
hugr.validate(&PRELUDE_REGISTRY),
Err(ValidationError::ExtensionError(
ExtensionError::TgtExceedsSrcExtensionsAtPort { .. }
))
Expand Down Expand Up @@ -1327,7 +1327,7 @@ mod test {
cause: SignatureError::ExtensionNotFound(PRELUDE.name.clone())
})
);
h.validate(&prelude_registry()).unwrap();
h.validate(&PRELUDE_REGISTRY).unwrap();
}

#[test]
Expand Down

0 comments on commit 1b83640

Please sign in to comment.