Skip to content

Commit

Permalink
refactor: simplify usage cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Jun 7, 2024
1 parent 3f6ec66 commit 41cfd24
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 55 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ base64 = { version = "0.21.0", optional = true }
bitflags = "2.3"
bytes = { version = "1.4", optional = true }
crc32c = "0.6"
dashmap = { version = "5.4", optional = true }
ed25519-dalek = { version = "2.0", optional = true }
everscale-crypto = { version = "0.2", features = ["tl-proto"], optional = true }
hex = "0.4"
Expand Down Expand Up @@ -54,7 +55,7 @@ serde_json = "1"

[features]
default = ["base64", "serde", "models", "sync"]
sync = []
sync = ["dep:dashmap"]
stats = []
serde = ["dep:serde", "base64"]
rand = ["dep:rand"]
Expand Down
7 changes: 7 additions & 0 deletions src/cell/cell_impl/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ use crate::util::TryAsMut;
#[repr(transparent)]
pub struct Cell(Rc<DynCell>);

impl Default for Cell {
#[inline]
fn default() -> Self {
Cell::empty_cell()
}
}

impl std::ops::Deref for Cell {
type Target = DynCell;

Expand Down
62 changes: 11 additions & 51 deletions src/cell/usage_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,6 @@ impl UsageTreeWithSubtrees {
}
}

struct VisitedCell {
include: bool,
_cell: Cell,
}

#[cfg(not(feature = "sync"))]
use self::rc::{SharedState, UsageCell, UsageTreeState};

Expand Down Expand Up @@ -164,12 +159,12 @@ impl CellImpl for UsageCell {
mod rc {
use std::rc::Rc;

use super::{UsageTreeMode, VisitedCell};
use super::UsageTreeMode;
use crate::cell::{Cell, DynCell, HashBytes};

pub type SharedState = Rc<UsageTreeState>;

type VisitedCells = std::cell::RefCell<ahash::HashMap<HashBytes, VisitedCell>>;
type VisitedCells = std::cell::RefCell<ahash::HashSet<HashBytes>>;

pub struct UsageTreeState {
mode: UsageTreeMode,
Expand All @@ -194,31 +189,14 @@ mod rc {

#[inline]
pub fn insert(&self, cell: &Cell, ctx: UsageTreeMode) {
let repr_hash = cell.repr_hash();
let include = self.mode == ctx;

let mut visited = self.visited.borrow_mut();

if let Some(visited) = visited.get_mut(repr_hash) {
visited.include |= include;
} else {
visited.insert(
*repr_hash,
VisitedCell {
include,
_cell: cell.clone(),
},
);
if self.mode == ctx {
self.visited.borrow_mut().insert(*cell.repr_hash());
}
}

#[inline]
pub fn contains(&self, repr_hash: &HashBytes) -> bool {
if let Some(cell) = self.visited.borrow().get(repr_hash) {
cell.include
} else {
false
}
self.visited.borrow().contains(repr_hash)
}
}

Expand Down Expand Up @@ -256,14 +234,14 @@ mod rc {

#[cfg(feature = "sync")]
mod sync {
use std::sync::{Arc, Mutex};
use std::sync::Arc;

use super::{UsageTreeMode, VisitedCell};
use super::UsageTreeMode;
use crate::cell::{Cell, DynCell, HashBytes};

pub type SharedState = Arc<UsageTreeState>;

type VisitedCells = Mutex<ahash::HashMap<HashBytes, VisitedCell>>;
type VisitedCells = dashmap::DashSet<HashBytes, ahash::RandomState>;

pub struct UsageTreeState {
mode: UsageTreeMode,
Expand All @@ -288,32 +266,14 @@ mod sync {

#[inline]
pub fn insert(&self, cell: &Cell, ctx: UsageTreeMode) {
let repr_hash = cell.repr_hash();
let include = self.mode == ctx;

let mut visited = self.visited.lock().expect("lock failed");

if let Some(visited) = visited.get_mut(repr_hash) {
visited.include |= include;
} else {
visited.insert(
*repr_hash,
VisitedCell {
include,
_cell: cell.clone(),
},
);
if self.mode == ctx {
self.visited.insert(*cell.repr_hash());
}
}

#[inline]
pub fn contains(&self, repr_hash: &HashBytes) -> bool {
let visited = self.visited.lock().expect("lock failed");
if let Some(cell) = visited.get(repr_hash) {
cell.include
} else {
false
}
self.visited.contains(repr_hash)
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/models/block/block_extra.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[cfg(feature = "sync")]
use std::sync::OnceLock;

use crate::cell::*;
Expand Down Expand Up @@ -34,6 +35,7 @@ pub struct BlockExtra {
pub shard_block_refs: ShardBlockRefs,
}

#[cfg(feature = "sync")]
impl Default for BlockExtra {
fn default() -> Self {
Self {
Expand All @@ -55,18 +57,21 @@ impl BlockExtra {
const TAG_V2: u32 = 0x4a33f6fc;

/// Returns a static reference to an empty inbound message description.
#[cfg(feature = "sync")]
pub fn empty_in_msg_descr() -> &'static Lazy<InMsgDescr> {
static IN_MSG_DESCR: OnceLock<Lazy<InMsgDescr>> = OnceLock::new();
IN_MSG_DESCR.get_or_init(|| Lazy::new(&AugDict::new()).unwrap())
}

/// Returns a static reference to an empty outbound message description.
#[cfg(feature = "sync")]
pub fn empty_out_msg_descr() -> &'static Lazy<OutMsgDescr> {
static OUT_MSG_DESCR: OnceLock<Lazy<OutMsgDescr>> = OnceLock::new();
OUT_MSG_DESCR.get_or_init(|| Lazy::new(&AugDict::new()).unwrap())
}

/// Returns a static reference to an empty account blocks.
#[cfg(feature = "sync")]
pub fn empty_account_blocks() -> &'static Lazy<AccountBlocks> {
static ACCOUNT_BLOCKS: OnceLock<Lazy<AccountBlocks>> = OnceLock::new();
ACCOUNT_BLOCKS.get_or_init(|| Lazy::new(&AugDict::new()).unwrap())
Expand Down
3 changes: 3 additions & 0 deletions src/models/block/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Block models.

#[cfg(feature = "sync")]
use std::sync::OnceLock;

use crate::cell::*;
Expand Down Expand Up @@ -206,6 +207,7 @@ pub struct BlockInfo {
pub prev_vert_ref: Option<Lazy<BlockRef>>,
}

#[cfg(feature = "sync")]
impl Default for BlockInfo {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -494,6 +496,7 @@ pub enum PrevBlockRef {

impl PrevBlockRef {
/// Returns a static reference to an empty single reference.
#[cfg(feature = "sync")]
pub fn empty_single_ref() -> &'static Cell {
static CELL: OnceLock<Cell> = OnceLock::new();
CELL.get_or_init(|| {
Expand Down
4 changes: 2 additions & 2 deletions src/models/config/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ fn validator_subset() {
prev_total_weight: 0,
},
ValidatorDescription {
public_key: "4a766a1664c2ea41cfb6451c0bff37e23592344ffb36d939e12c556008f15106"
public_key: "dad309ba273ef26449a9824a31136e0cfcb904e0b37f99123b5ad8cc61ff3c82"
.parse()
.unwrap(),
weight: 1,
Expand All @@ -409,7 +409,7 @@ fn validator_subset() {
prev_total_weight: 0,
},
];
let expected_hash_short = 2248643272;
let expected_hash_short = 730838627;

assert_eq!(subset, (expected_list, expected_hash_short));
}
Expand Down
5 changes: 4 additions & 1 deletion src/models/shard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Shard state models.

#[cfg(feature = "sync")]
use std::sync::OnceLock;

use crate::cell::*;
Expand Down Expand Up @@ -116,6 +117,7 @@ pub struct ShardStateUnsplit {
pub shard_block_refs: Option<ShardBlockRefs>,
}

#[cfg(feature = "sync")]
impl Default for ShardStateUnsplit {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -153,13 +155,14 @@ impl ShardStateUnsplit {
const TAG_V2: u32 = 0x9023aeee;

/// Returns a static reference to the empty processed up to info.
#[cfg(feature = "tycho")]
#[cfg(all(feature = "sync", feature = "tycho"))]
pub fn empty_processed_upto_info() -> &'static Lazy<ProcessedUptoInfo> {
static PROCESSED_UPTO_INFO: OnceLock<Lazy<ProcessedUptoInfo>> = OnceLock::new();
PROCESSED_UPTO_INFO.get_or_init(|| Lazy::new(&ProcessedUptoInfo::default()).unwrap())
}

/// Returns a static reference to the empty shard accounts.
#[cfg(feature = "sync")]
pub fn empty_shard_accounts() -> &'static Lazy<ShardAccounts> {
static SHARD_ACCOUNTS: OnceLock<Lazy<ShardAccounts>> = OnceLock::new();
SHARD_ACCOUNTS.get_or_init(|| Lazy::new(&ShardAccounts::new()).unwrap())
Expand Down

0 comments on commit 41cfd24

Please sign in to comment.