Skip to content

Commit

Permalink
nostr: remove Arc<T> from OnceCell<T> in Event and Tag
Browse files Browse the repository at this point in the history
This change eliminates the unnecessary use of `Arc` around `OnceCell` in the `Event` and `Tag` structs. This simplification leads to more efficient memory usage.

Ref rust-nostr#522

Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Aug 5, 2024
1 parent 2088be8 commit 8923e67
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
* nostr: update `PartialEvent` methods ([Yuki Kishimoto])
* nostr: change `EventBuilder::award_badge` fingerprint ([Yuki Kishimoto])
* nostr: add NIP-50 support to `Filter::match_event` method ([Yuki Kishimoto])
* nostr: remove `Arc<T>` from `OnceCell<T>` in `Event` and `Tag` ([Yuki Kishimoto])
* pool: take mutex ownership instead of clone in `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])
Expand Down
8 changes: 3 additions & 5 deletions crates/nostr/src/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use alloc::collections::{BTreeMap, BTreeSet};
use alloc::string::{String, ToString};
#[cfg(feature = "std")]
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::fmt;
Expand Down Expand Up @@ -137,7 +135,7 @@ pub struct Event {
deser_order: Vec<EventKey>,
/// Tags indexes
#[cfg(feature = "std")]
tags_indexes: Arc<OnceCell<TagsIndexes>>,
tags_indexes: OnceCell<TagsIndexes>,
}

impl fmt::Debug for Event {
Expand Down Expand Up @@ -215,7 +213,7 @@ impl Event {
},
deser_order: Vec::new(),
#[cfg(feature = "std")]
tags_indexes: Arc::new(OnceCell::new()),
tags_indexes: OnceCell::new(),
}
}

Expand Down Expand Up @@ -653,7 +651,7 @@ impl<'de> Deserialize<'de> for Event {
inner: serde_json::from_value(value).map_err(serde::de::Error::custom)?,
deser_order,
#[cfg(feature = "std")]
tags_indexes: Arc::new(OnceCell::new()),
tags_indexes: OnceCell::new(),
})
}
}
Expand Down
12 changes: 4 additions & 8 deletions crates/nostr/src/event/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! Tag
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::cmp::Ordering;
use core::hash::{Hash, Hasher};
Expand Down Expand Up @@ -37,7 +36,7 @@ use crate::{ImageDimensions, PublicKey, SingleLetterTag, Timestamp, UncheckedUrl
#[derive(Debug, Clone)]
pub struct Tag {
buf: Vec<String>,
standardized: Arc<OnceCell<Option<TagStandard>>>,
standardized: OnceCell<Option<TagStandard>>,
}

impl PartialEq for Tag {
Expand Down Expand Up @@ -71,15 +70,15 @@ impl Tag {
fn new(buf: Vec<String>, standardized: Option<TagStandard>) -> Self {
Self {
buf,
standardized: Arc::new(OnceCell::from(standardized)),
standardized: OnceCell::from(standardized),
}
}

#[inline]
fn new_with_empty_cell(buf: Vec<String>) -> Self {
Self {
buf,
standardized: Arc::new(OnceCell::new()),
standardized: OnceCell::new(),
}
}

Expand Down Expand Up @@ -146,10 +145,7 @@ impl Tag {

/// Consume tag and get standardized tag
pub fn to_standardized(self) -> Option<TagStandard> {
// TODO: replace with `Arc::unwrap_or_clone(self.standardized)` when MSRV will be >= 1.76.0
let standardized: OnceCell<Option<TagStandard>> =
Arc::try_unwrap(self.standardized).unwrap_or_else(|arc| (*arc).clone());
match standardized.into_inner() {
match self.standardized.into_inner() {
Some(inner) => inner,
None => TagStandard::parse(&self.buf).ok(),
}
Expand Down

0 comments on commit 8923e67

Please sign in to comment.