Skip to content

Commit 8923e67

Browse files
committed
nostr: remove Arc<T> from OnceCell<T> in Event and Tag
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 #522 Signed-off-by: Yuki Kishimoto <[email protected]>
1 parent 2088be8 commit 8923e67

File tree

3 files changed

+8
-13
lines changed

3 files changed

+8
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* nostr: update `PartialEvent` methods ([Yuki Kishimoto])
3535
* nostr: change `EventBuilder::award_badge` fingerprint ([Yuki Kishimoto])
3636
* nostr: add NIP-50 support to `Filter::match_event` method ([Yuki Kishimoto])
37+
* nostr: remove `Arc<T>` from `OnceCell<T>` in `Event` and `Tag` ([Yuki Kishimoto])
3738
* pool: take mutex ownership instead of clone in `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
3839
* pool: remove IDs collection from `InternalRelayPool::get_events_from` ([Yuki Kishimoto])
3940
* pool: better checks before perform queries or send messages to relays ([Yuki Kishimoto])

crates/nostr/src/event/mod.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
88
use alloc::collections::{BTreeMap, BTreeSet};
99
use alloc::string::{String, ToString};
10-
#[cfg(feature = "std")]
11-
use alloc::sync::Arc;
1210
use alloc::vec::Vec;
1311
use core::cmp::Ordering;
1412
use core::fmt;
@@ -137,7 +135,7 @@ pub struct Event {
137135
deser_order: Vec<EventKey>,
138136
/// Tags indexes
139137
#[cfg(feature = "std")]
140-
tags_indexes: Arc<OnceCell<TagsIndexes>>,
138+
tags_indexes: OnceCell<TagsIndexes>,
141139
}
142140

143141
impl fmt::Debug for Event {
@@ -215,7 +213,7 @@ impl Event {
215213
},
216214
deser_order: Vec::new(),
217215
#[cfg(feature = "std")]
218-
tags_indexes: Arc::new(OnceCell::new()),
216+
tags_indexes: OnceCell::new(),
219217
}
220218
}
221219

@@ -653,7 +651,7 @@ impl<'de> Deserialize<'de> for Event {
653651
inner: serde_json::from_value(value).map_err(serde::de::Error::custom)?,
654652
deser_order,
655653
#[cfg(feature = "std")]
656-
tags_indexes: Arc::new(OnceCell::new()),
654+
tags_indexes: OnceCell::new(),
657655
})
658656
}
659657
}

crates/nostr/src/event/tag/mod.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
//! Tag
66
77
use alloc::string::{String, ToString};
8-
use alloc::sync::Arc;
98
use alloc::vec::Vec;
109
use core::cmp::Ordering;
1110
use core::hash::{Hash, Hasher};
@@ -37,7 +36,7 @@ use crate::{ImageDimensions, PublicKey, SingleLetterTag, Timestamp, UncheckedUrl
3736
#[derive(Debug, Clone)]
3837
pub struct Tag {
3938
buf: Vec<String>,
40-
standardized: Arc<OnceCell<Option<TagStandard>>>,
39+
standardized: OnceCell<Option<TagStandard>>,
4140
}
4241

4342
impl PartialEq for Tag {
@@ -71,15 +70,15 @@ impl Tag {
7170
fn new(buf: Vec<String>, standardized: Option<TagStandard>) -> Self {
7271
Self {
7372
buf,
74-
standardized: Arc::new(OnceCell::from(standardized)),
73+
standardized: OnceCell::from(standardized),
7574
}
7675
}
7776

7877
#[inline]
7978
fn new_with_empty_cell(buf: Vec<String>) -> Self {
8079
Self {
8180
buf,
82-
standardized: Arc::new(OnceCell::new()),
81+
standardized: OnceCell::new(),
8382
}
8483
}
8584

@@ -146,10 +145,7 @@ impl Tag {
146145

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

0 commit comments

Comments
 (0)