Skip to content

Commit

Permalink
Use Arc instead of Rc in GenericFoo structs (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh authored Sep 16, 2024
1 parent d2248cd commit 322eb04
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
20 changes: 10 additions & 10 deletions crates/starknet-os-types/src/casm_contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::OnceCell;
use std::rc::Rc;
use std::sync::Arc;

use serde::{Deserialize, Deserializer, Serialize, Serializer};

Expand All @@ -16,9 +16,9 @@ pub type BlockifierCasmClass = blockifier::execution::contract_class::ContractCl
/// Fields are boxed in an RC for cheap cloning.
#[derive(Debug, Clone)]
pub struct GenericCasmContractClass {
blockifier_contract_class: OnceCell<Rc<BlockifierCasmClass>>,
cairo_lang_contract_class: OnceCell<Rc<CairoLangCasmClass>>,
serialized_class: OnceCell<Rc<Vec<u8>>>,
blockifier_contract_class: OnceCell<Arc<BlockifierCasmClass>>,
cairo_lang_contract_class: OnceCell<Arc<CairoLangCasmClass>>,
serialized_class: OnceCell<Arc<Vec<u8>>>,
class_hash: OnceCell<GenericClassHash>,
}

Expand All @@ -40,7 +40,7 @@ impl GenericCasmContractClass {
Self {
blockifier_contract_class: OnceCell::new(),
cairo_lang_contract_class: OnceCell::new(),
serialized_class: OnceCell::from(Rc::new(serialized_class)),
serialized_class: OnceCell::from(Arc::new(serialized_class)),
class_hash: OnceCell::new(),
}
}
Expand All @@ -62,7 +62,7 @@ impl GenericCasmContractClass {
if let Some(serialized_class) = &self.serialized_class.get() {
let cairo_lang_class = cairo_lang_contract_class_from_bytes(serialized_class)?;
self.cairo_lang_contract_class
.set(Rc::new(cairo_lang_class.clone()))
.set(Arc::new(cairo_lang_class.clone()))
.expect("cairo-lang class is already set");
return blockifier_contract_class_from_cairo_lang_class(cairo_lang_class);
}
Expand All @@ -71,13 +71,13 @@ impl GenericCasmContractClass {
}
pub fn get_cairo_lang_contract_class(&self) -> Result<&CairoLangCasmClass, ContractClassError> {
self.cairo_lang_contract_class
.get_or_try_init(|| self.build_cairo_lang_class().map(Rc::new))
.get_or_try_init(|| self.build_cairo_lang_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

pub fn get_blockifier_contract_class(&self) -> Result<&BlockifierCasmClass, ContractClassError> {
self.blockifier_contract_class
.get_or_try_init(|| self.build_blockifier_class().map(Rc::new))
.get_or_try_init(|| self.build_blockifier_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

Expand Down Expand Up @@ -130,7 +130,7 @@ impl From<CairoLangCasmClass> for GenericCasmContractClass {
fn from(cairo_lang_class: CairoLangCasmClass) -> Self {
Self {
blockifier_contract_class: Default::default(),
cairo_lang_contract_class: OnceCell::from(Rc::new(cairo_lang_class)),
cairo_lang_contract_class: OnceCell::from(Arc::new(cairo_lang_class)),
serialized_class: Default::default(),
class_hash: Default::default(),
}
Expand All @@ -140,7 +140,7 @@ impl From<CairoLangCasmClass> for GenericCasmContractClass {
impl From<BlockifierCasmClass> for GenericCasmContractClass {
fn from(blockifier_class: BlockifierCasmClass) -> Self {
Self {
blockifier_contract_class: OnceCell::from(Rc::new(blockifier_class)),
blockifier_contract_class: OnceCell::from(Arc::new(blockifier_class)),
cairo_lang_contract_class: Default::default(),
serialized_class: Default::default(),
class_hash: Default::default(),
Expand Down
18 changes: 9 additions & 9 deletions crates/starknet-os-types/src/deprecated_compiled_class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::OnceCell;
use std::rc::Rc;
use std::sync::Arc;

use pathfinder_gateway_types::class_hash::compute_class_hash;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
Expand All @@ -20,9 +20,9 @@ pub type BlockifierDeprecatedClass = blockifier::execution::contract_class::Cont
/// Fields are boxed in an RC for cheap cloning.
#[derive(Debug, Clone)]
pub struct GenericDeprecatedCompiledClass {
blockifier_contract_class: OnceCell<Rc<BlockifierDeprecatedClass>>,
starknet_api_contract_class: OnceCell<Rc<StarknetApiDeprecatedClass>>,
starknet_core_contract_class: OnceCell<Rc<StarknetCoreDeprecatedClass>>,
blockifier_contract_class: OnceCell<Arc<BlockifierDeprecatedClass>>,
starknet_api_contract_class: OnceCell<Arc<StarknetApiDeprecatedClass>>,
starknet_core_contract_class: OnceCell<Arc<StarknetCoreDeprecatedClass>>,
serialized_class: OnceCell<Vec<u8>>,
class_hash: OnceCell<GenericClassHash>,
}
Expand Down Expand Up @@ -56,13 +56,13 @@ impl GenericDeprecatedCompiledClass {

pub fn get_starknet_api_contract_class(&self) -> Result<&StarknetApiDeprecatedClass, ContractClassError> {
self.starknet_api_contract_class
.get_or_try_init(|| self.build_starknet_api_class().map(Rc::new))
.get_or_try_init(|| self.build_starknet_api_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

pub fn get_blockifier_contract_class(&self) -> Result<&BlockifierDeprecatedClass, ContractClassError> {
self.blockifier_contract_class
.get_or_try_init(|| self.build_blockifier_class().map(Rc::new))
.get_or_try_init(|| self.build_blockifier_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

Expand Down Expand Up @@ -128,7 +128,7 @@ impl From<StarknetApiDeprecatedClass> for GenericDeprecatedCompiledClass {
fn from(starknet_api_class: StarknetApiDeprecatedClass) -> Self {
Self {
blockifier_contract_class: Default::default(),
starknet_api_contract_class: OnceCell::from(Rc::new(starknet_api_class)),
starknet_api_contract_class: OnceCell::from(Arc::new(starknet_api_class)),
starknet_core_contract_class: Default::default(),
serialized_class: Default::default(),
class_hash: Default::default(),
Expand All @@ -151,7 +151,7 @@ impl TryFrom<CompressedStarknetCoreDeprecatedClass> for GenericDeprecatedCompile
impl From<BlockifierDeprecatedClass> for GenericDeprecatedCompiledClass {
fn from(blockifier_class: BlockifierDeprecatedClass) -> Self {
Self {
blockifier_contract_class: OnceCell::from(Rc::new(blockifier_class)),
blockifier_contract_class: OnceCell::from(Arc::new(blockifier_class)),
starknet_api_contract_class: Default::default(),
starknet_core_contract_class: Default::default(),
serialized_class: Default::default(),
Expand All @@ -165,7 +165,7 @@ impl From<StarknetCoreDeprecatedClass> for GenericDeprecatedCompiledClass {
Self {
blockifier_contract_class: Default::default(),
starknet_api_contract_class: Default::default(),
starknet_core_contract_class: OnceCell::from(Rc::new(starknet_core_class)),
starknet_core_contract_class: OnceCell::from(Arc::new(starknet_core_class)),
serialized_class: Default::default(),
class_hash: Default::default(),
}
Expand Down
16 changes: 8 additions & 8 deletions crates/starknet-os-types/src/sierra_contract_class.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::OnceCell;
use std::rc::Rc;
use std::sync::Arc;

use cairo_vm::Felt252;
use pathfinder_gateway_types::class_hash::compute_class_hash;
Expand All @@ -20,11 +20,11 @@ pub type StarknetCoreSierraContractClass = starknet_core::types::FlattenedSierra
/// contract class types in Starknet and provides utility methods.
/// Operations are implemented as lazily as possible, i.e. we only convert
/// between different types if strictly necessary.
/// Fields are boxed in an RC for cheap cloning.
/// Fields are boxed in an Arc for cheap cloning.
#[derive(Debug, Clone)]
pub struct GenericSierraContractClass {
cairo_lang_contract_class: OnceCell<Rc<CairoLangSierraContractClass>>,
starknet_core_contract_class: OnceCell<Rc<StarknetCoreSierraContractClass>>,
cairo_lang_contract_class: OnceCell<Arc<CairoLangSierraContractClass>>,
starknet_core_contract_class: OnceCell<Arc<StarknetCoreSierraContractClass>>,
serialized_class: OnceCell<Vec<u8>>,
class_hash: OnceCell<GenericClassHash>,
}
Expand Down Expand Up @@ -58,13 +58,13 @@ impl GenericSierraContractClass {
}
pub fn get_cairo_lang_contract_class(&self) -> Result<&CairoLangSierraContractClass, ContractClassError> {
self.cairo_lang_contract_class
.get_or_try_init(|| self.build_cairo_lang_class().map(Rc::new))
.get_or_try_init(|| self.build_cairo_lang_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

pub fn get_starknet_core_contract_class(&self) -> Result<&StarknetCoreSierraContractClass, ContractClassError> {
self.starknet_core_contract_class
.get_or_try_init(|| self.build_starknet_core_class().map(Rc::new))
.get_or_try_init(|| self.build_starknet_core_class().map(Arc::new))
.map(|boxed| boxed.as_ref())
}

Expand Down Expand Up @@ -194,7 +194,7 @@ impl<'de> Deserialize<'de> for GenericSierraContractClass {
impl From<CairoLangSierraContractClass> for GenericSierraContractClass {
fn from(cairo_lang_class: CairoLangSierraContractClass) -> Self {
Self {
cairo_lang_contract_class: OnceCell::from(Rc::new(cairo_lang_class)),
cairo_lang_contract_class: OnceCell::from(Arc::new(cairo_lang_class)),
starknet_core_contract_class: Default::default(),
serialized_class: Default::default(),
class_hash: Default::default(),
Expand All @@ -206,7 +206,7 @@ impl From<StarknetCoreSierraContractClass> for GenericSierraContractClass {
fn from(starknet_core_class: StarknetCoreSierraContractClass) -> Self {
Self {
cairo_lang_contract_class: Default::default(),
starknet_core_contract_class: OnceCell::from(Rc::new(starknet_core_class)),
starknet_core_contract_class: OnceCell::from(Arc::new(starknet_core_class)),
serialized_class: Default::default(),
class_hash: Default::default(),
}
Expand Down

0 comments on commit 322eb04

Please sign in to comment.