Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify initialization of array of elements that are not Copy #28

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions crates/circuit/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ pub fn populate_std_gate_map(py: Python, rs_gate: StandardGate, py_gate: PyObjec
match STDGATE_PYTHON_GATES.get_mut() {
Some(gate_map) => gate_map,
None => {
// A fixed size array is initialized like this because using the `[T; 5]` syntax
// requires T to be `Copy`. But `PyObject` isn't Copy so therefore Option<PyObject>
// as T isn't Copy. To avoid that we just list out None STANDARD_GATE_SIZE times
let array: [Option<PyObject>; STANDARD_GATE_SIZE] = [
None, None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None, None,
];
let array: [Option<PyObject>; STANDARD_GATE_SIZE] = core::array::from_fn(|_| None);
STDGATE_PYTHON_GATES.set(py, array).unwrap();
STDGATE_PYTHON_GATES.get_mut().unwrap()
}
Expand All @@ -124,18 +118,8 @@ pub fn populate_std_gate_map(py: Python, rs_gate: StandardGate, py_gate: PyObjec

#[inline]
pub fn get_std_gate_class(py: Python, rs_gate: StandardGate) -> PyResult<PyObject> {
let gate_map = unsafe {
STDGATE_PYTHON_GATES.get_or_init(py, || {
// A fixed size array is initialized like this because using the `[T; 5]` syntax
// requires T to be `Copy`. But `PyObject` isn't Copy so therefore Option<PyObject>
// as T isn't Copy. To avoid that we just list out None STANDARD_GATE_SIZE times
let array: [Option<PyObject>; STANDARD_GATE_SIZE] = [
None, None, None, None, None, None, None, None, None, None, None, None, None, None,
None, None, None, None,
];
array
})
};
let array: [Option<PyObject>; STANDARD_GATE_SIZE] = core::array::from_fn(|_| None);
let gate_map = unsafe { STDGATE_PYTHON_GATES.get_or_init(py, || array) };
Copy link
Owner

Choose a reason for hiding this comment

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

Shouldn't we initialize array inside the closure as part of get_or_init? Otherwise we'll be creating the array of None every time this function is called even though we don't need it.

Copy link
Author

Choose a reason for hiding this comment

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

Oh yeah it is called frequently. I thought about this but somehow ignored it.

Copy link
Author

Choose a reason for hiding this comment

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

Done in 0bce4d4

let gate = &gate_map[rs_gate as usize];
let populate = gate.is_none();
let out_gate = match gate {
Expand Down