radicle/identity/doc: LazyLock for PayloadId

The functions `PayloadId::{project,canonical_refs}` have two
weaknesses:
 1. They always execute the checks in `from_str` for the same
    `str` over and over again.
 2. They do not return a reference, thus, callers that only
    need a reference make a copy that they then discard.

For improved performance, switch to `LazyLocks`. Keep the less efficient
functions, but deprecate them.
This commit is contained in:
Lorenz Leutgeb
2026-08-30 17:37:09 +02:00
parent 138404df6f
commit fba60164f0
6 changed files with 40 additions and 32 deletions
+1 -1
View File
@@ -2112,7 +2112,7 @@ fn test_fetch_emits_canonical_ref_update_partial_glob() {
});
raw.payload.insert(
radicle::identity::doc::PayloadId::canonical_refs(),
radicle::identity::doc::PayloadId::canonical_refs().clone(),
radicle::identity::doc::Payload::from(crefs),
);
})
+3 -2
View File
@@ -2596,7 +2596,8 @@ mod test {
// Make a change to the description and sign it.
let desc = prj.description().to_owned() + "!";
let prj = prj.update(None, desc, None).unwrap();
doc.payload.insert(PayloadId::project(), prj.clone().into());
doc.payload
.insert(PayloadId::project().clone(), prj.clone().into());
identity
.update(
cob::Title::new("Update description").unwrap(),
@@ -2634,7 +2635,7 @@ mod test {
// Update description again with signatures by Eve and Bob.
let desc = prj.description().to_owned() + "?";
let prj = prj.update(None, desc, None).unwrap();
doc.payload.insert(PayloadId::project(), prj.into());
doc.payload.insert(PayloadId::project().clone(), prj.into());
let revision = bob_identity
.update(
cob::Title::new("Update description again").unwrap(),
@@ -434,7 +434,7 @@ impl Harness {
let prj = doc.project().unwrap().unwrap();
let desc = format!("{} {}", msg, self.revisions.len());
let prj = prj.update(None, desc, None).unwrap();
doc.payload.insert(PayloadId::project(), prj.into());
doc.payload.insert(PayloadId::project().clone(), prj.into());
}
fn did_for(&self, actor: &Actor) -> Did {
+3 -3
View File
@@ -3194,7 +3194,7 @@ mod test {
});
let crefs = serde_json::json!({ "rules": rules });
raw_doc.payload.insert(
identity::doc::PayloadId::canonical_refs(),
identity::doc::PayloadId::canonical_refs().clone(),
identity::doc::Payload::from(crefs),
);
@@ -3276,7 +3276,7 @@ mod test {
"rules": rules
});
raw_doc.payload.insert(
identity::doc::PayloadId::canonical_refs(),
identity::doc::PayloadId::canonical_refs().clone(),
identity::doc::Payload::from(crefs),
);
@@ -3335,7 +3335,7 @@ mod test {
"rules": rules
});
raw_doc.payload.insert(
identity::doc::PayloadId::canonical_refs(),
identity::doc::PayloadId::canonical_refs().clone(),
identity::doc::Payload::from(crefs),
);
+22 -19
View File
@@ -227,22 +227,21 @@ impl FromStr for PayloadId {
}
}
static CANONICAL_REFS: LazyLock<PayloadId> = LazyLock::new(|| PayloadId::new("xyz.radicle.crefs"));
static PROJECT: LazyLock<PayloadId> = LazyLock::new(|| PayloadId::new("xyz.radicle.project"));
impl PayloadId {
/// Project payload type.
pub fn project() -> Self {
Self(
// SAFETY: We know this is valid.
TypeName::from_str("xyz.radicle.project")
.expect("PayloadId::project: type name is valid"),
)
pub fn canonical_refs() -> &'static Self {
&CANONICAL_REFS
}
pub fn canonical_refs() -> Self {
Self(
// SAFETY: We know this is valid.
TypeName::from_str("xyz.radicle.crefs")
.expect("PayloadId::canonical_refs: type name is valid"),
)
pub fn project() -> &'static Self {
&PROJECT
}
#[inline]
fn new(type_name: &'static str) -> Self {
Self::from_str(type_name).expect("type_name is valid")
}
}
@@ -303,13 +302,13 @@ pub trait GetPayload {
/// Get the [`Project`] by deserializing from the payload.
fn project(&self) -> Option<Result<Project, PayloadError>> {
self.load_payload(&PayloadId::project())
self.load_payload(PayloadId::project())
}
/// Retrieve the [`RawCanonicalRefs`] by deserializing from the payload
/// (if present).
fn raw_canonical_refs(&self) -> Option<Result<RawCanonicalRefs, PayloadError>> {
self.load_payload(&PayloadId::canonical_refs())
self.load_payload(PayloadId::canonical_refs())
}
}
@@ -454,7 +453,7 @@ impl RawDoc {
Self {
version: IDENTITY_VERSION,
payload: BTreeMap::from_iter([(PayloadId::project(), Payload::from(project))]),
payload: BTreeMap::from_iter([(PayloadId::project().clone(), Payload::from(project))]),
delegates,
threshold,
visibility,
@@ -714,7 +713,7 @@ impl Doc {
Self {
version: IDENTITY_VERSION,
payload: BTreeMap::from_iter([(PayloadId::project(), Payload::from(project))]),
payload: BTreeMap::from_iter([(PayloadId::project().clone(), Payload::from(project))]),
delegates: Delegates(NonEmpty::new(delegate)),
threshold: Threshold(NonZeroUsize::MIN),
visibility,
@@ -809,7 +808,11 @@ impl Doc {
///
/// [`RawCanonicalRefs`]: super::crefs::RawCanonicalRefs
pub fn canonical_refs(&self) -> Result<CanonicalRefs, CanonicalRefsError> {
let mut raw_crefs = self.raw_canonical_refs().transpose().map_err(CanonicalRefsError::Payload)?.unwrap_or_default();
let mut raw_crefs = self
.raw_canonical_refs()
.transpose()
.map_err(CanonicalRefsError::Payload)?
.unwrap_or_default();
let resolve = &mut || self.delegates.clone();
// Determine where `HEAD` comes from. The `resolve_head()` result
@@ -1209,7 +1212,7 @@ mod test {
// `IDENTITY_VERSION`.
let doc = serde_json::from_str::<RawDoc>(&v1.to_string()).unwrap();
let payload = [(
PayloadId::project(),
PayloadId::project().clone(),
Payload {
value: json!({
"name": "heartwood",
+10 -6
View File
@@ -202,7 +202,7 @@ pub fn verify(raw: RawDoc) -> Result<Doc, error::DocVerification> {
Some(Ok(project)) => Some(project),
Some(Err(PayloadError::Json(e))) => {
return Err(error::DocVerification::PayloadError {
id: PayloadId::project(),
id: PayloadId::project().clone(),
err: e.to_string(),
});
}
@@ -214,7 +214,11 @@ pub fn verify(raw: RawDoc) -> Result<Doc, error::DocVerification> {
// (This rule must be synthesized!)
// 2. … symbolic reference with the name `HEAD`.
// (This reference must be synthesized!)
match raw.raw_canonical_refs().transpose().map(|rcrefs| rcrefs.zip(project)) {
match raw
.raw_canonical_refs()
.transpose()
.map(|rcrefs| rcrefs.zip(project))
{
Ok(Some((crefs, project))) => {
let default = project.default_branch_qualified().to_owned();
let matches = crefs
@@ -239,7 +243,7 @@ pub fn verify(raw: RawDoc) -> Result<Doc, error::DocVerification> {
// Verify that the canonical references payload is valid
if let Err(e) = proposal.canonical_refs() {
return Err(error::DocVerification::PayloadError {
id: PayloadId::canonical_refs(),
id: PayloadId::canonical_refs().clone(),
err: e.to_string(),
});
}
@@ -296,7 +300,7 @@ mod test {
let raw = super::payload(
raw,
[PayloadUpsert {
id: PayloadId::canonical_refs(),
id: PayloadId::canonical_refs().clone(),
key: "rules".to_string(),
value: json!({
"refs/tags/*": {
@@ -319,7 +323,7 @@ mod test {
let raw = super::payload(
raw,
[PayloadUpsert {
id: PayloadId::canonical_refs(),
id: PayloadId::canonical_refs().clone(),
key: "rules".to_string(),
value: json!({
"refs/tags/*": {
@@ -351,7 +355,7 @@ mod test {
let raw = super::payload(
raw,
[PayloadUpsert {
id: PayloadId::canonical_refs(),
id: PayloadId::canonical_refs().clone(),
key: "rules".to_string(),
value: json!({
"refs/tags/*": {