Skip to content

Commit 3b26002

Browse files
authored
Use Arc instead of Box for internal expression nodes in SymbolExpr (#14660)
This commit updates the internal construction of the SymbolExpr struct use a reference counting pointer instead of a regular pointer to a heap allocated object. The way the binary tree gets constructed internally using a normal pointer results in a lot of a copies of elements on the tree. This creates a large performance overhead for the symbol expr as we end up copying and freeing elements on the binary tree a large amount. This was the root cause of the regression reported in #14653 as the expression that gets generated by the transpiler internally ends up adding a lot of elements to global phase expression and that results in a lot of memory allocation and deallocation overhead. By using a reference counting pointer instead of creating and freeing copies of the expressions on the tree we instead only keep a single copy and just increment or decrement the reference count. Fixes #14653
1 parent f59779d commit 3b26002

File tree

4 files changed

+89
-79
lines changed

4 files changed

+89
-79
lines changed

crates/circuit/src/parameter_expression.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use num_complex::Complex64;
2121
use std::collections::hash_map::DefaultHasher;
2222
use std::fmt;
2323
use std::hash::{Hash, Hasher};
24+
use std::sync::Arc;
2425

2526
use pyo3::prelude::*;
2627
use pyo3::IntoPyObjectExt;
@@ -84,7 +85,7 @@ impl ParameterExpression {
8485
.replace("__end_sympy_replace__", "$");
8586

8687
ParameterExpression {
87-
expr: SymbolExpr::Symbol(Box::new(name)),
88+
expr: SymbolExpr::Symbol(Arc::new(name)),
8889
}
8990
}
9091

0 commit comments

Comments
 (0)