Skip to content

Commit

Permalink
Reduce allocations for empty cell
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexagon committed Aug 15, 2023
1 parent b75e1ab commit ab6aefa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/cell/cell_impl/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ impl From<Rc<DynCell>> for Cell {

impl CellFamily for Cell {
fn empty_cell() -> Cell {
Cell(Rc::new(EmptyOrdinaryCell))
thread_local! {
static EMPTY_CELL: Cell = Cell(Rc::new(EmptyOrdinaryCell));
}
EMPTY_CELL.with(Cell::clone)
}

fn empty_cell_ref() -> &'static DynCell {
Expand Down
7 changes: 6 additions & 1 deletion src/cell/cell_impl/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ impl From<Arc<DynCell>> for Cell {

impl CellFamily for Cell {
fn empty_cell() -> Cell {
Cell(Arc::new(EmptyOrdinaryCell))
use once_cell::sync::OnceCell;

static EMPTY_CELL: OnceCell<Cell> = OnceCell::new();
EMPTY_CELL
.get_or_init(|| Cell(Arc::new(EmptyOrdinaryCell)))
.clone()
}

fn empty_cell_ref() -> &'static DynCell {
Expand Down
5 changes: 3 additions & 2 deletions src/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::ops::{BitOr, BitOrAssign};
use std::str::FromStr;

use crate::error::{Error, ParseHashBytesError};
use crate::util::{decode_base64_slice, Bitstring};
use crate::util::Bitstring;

pub use self::builder::{CellBuilder, CellRefsBuilder, Store};
pub use self::cell_impl::{StaticCell, VirtualCellWrapper};
Expand Down Expand Up @@ -662,8 +662,9 @@ impl FromStr for HashBytes {
return Err(ParseHashBytesError::InvalidHex(e));
}
}
#[cfg(feature = "base64")]
44 => {
if let Err(e) = decode_base64_slice(s, &mut result.0) {
if let Err(e) = crate::util::decode_base64_slice(s, &mut result.0) {
return Err(ParseHashBytesError::InvalidBase64(e));
}
}
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub enum ParseIntError {
#[derive(Debug, Clone, thiserror::Error)]
pub enum ParseHashBytesError {
/// Failed to parse base64 encoded bytes.
#[cfg(feature = "base64")]
#[error("invalid base64 string")]
InvalidBase64(#[from] base64::DecodeSliceError),
/// Failed to parse hex encoded bytes.
Expand Down
2 changes: 2 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub(crate) fn decode_base64<T: AsRef<[u8]>>(data: T) -> Result<Vec<u8>, base64::
}

#[cfg(any(feature = "base64", test))]
#[allow(unused)]
#[inline]
pub(crate) fn decode_base64_slice<T: AsRef<[u8]>>(
data: T,
Expand Down Expand Up @@ -254,6 +255,7 @@ impl std::fmt::Binary for Bitstring<'_> {
}
}

#[allow(unused)]
pub(crate) fn debug_tuple_field1_finish(
f: &mut std::fmt::Formatter<'_>,
name: &str,
Expand Down

0 comments on commit ab6aefa

Please sign in to comment.