Skip to content

Commit 29cd7a8

Browse files
abbraclaude
andauthored
x509/certificate: cache public_key getter result (#14443)
Store the public key object in a PyOnceLock so that repeated accesses avoid re-parsing the SubjectPublicKeyInfo DER bytes. Signed-off-by: Alexander Bokovoy <abokovoy@redhat.com> Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent e69b1e0 commit 29cd7a8

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

src/rust/src/pkcs7.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ where
770770
cached_extensions: pyo3::sync::PyOnceLock::new(),
771771
cached_issuer: pyo3::sync::PyOnceLock::new(),
772772
cached_subject: pyo3::sync::PyOnceLock::new(),
773+
cached_public_key: pyo3::sync::PyOnceLock::new(),
773774
},
774775
)?)?;
775776

src/rust/src/x509/certificate.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub(crate) struct Certificate {
4343
pub(crate) cached_extensions: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
4444
pub(crate) cached_issuer: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
4545
pub(crate) cached_subject: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
46+
pub(crate) cached_public_key: pyo3::sync::PyOnceLock<pyo3::Py<pyo3::PyAny>>,
4647
}
4748

4849
#[pyo3::pymethods]
@@ -80,10 +81,17 @@ impl Certificate {
8081
&self,
8182
py: pyo3::Python<'p>,
8283
) -> CryptographyResult<pyo3::Bound<'p, pyo3::PyAny>> {
83-
keys::load_der_public_key_bytes(
84-
py,
85-
self.raw.borrow_dependent().tbs_cert.spki.tlv().full_data(),
86-
)
84+
Ok(self
85+
.cached_public_key
86+
.get_or_try_init(py, || {
87+
keys::load_der_public_key_bytes(
88+
py,
89+
self.raw.borrow_dependent().tbs_cert.spki.tlv().full_data(),
90+
)
91+
.map(|v| v.unbind())
92+
})?
93+
.bind(py)
94+
.clone())
8795
}
8896

8997
#[getter]
@@ -459,6 +467,7 @@ pub(crate) fn load_der_x509_certificate(
459467
cached_extensions: pyo3::sync::PyOnceLock::new(),
460468
cached_issuer: pyo3::sync::PyOnceLock::new(),
461469
cached_subject: pyo3::sync::PyOnceLock::new(),
470+
cached_public_key: pyo3::sync::PyOnceLock::new(),
462471
})
463472
}
464473

src/rust/src/x509/ocsp_resp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ impl OCSPResponse {
267267
cached_extensions: pyo3::sync::PyOnceLock::new(),
268268
cached_issuer: pyo3::sync::PyOnceLock::new(),
269269
cached_subject: pyo3::sync::PyOnceLock::new(),
270+
cached_public_key: pyo3::sync::PyOnceLock::new(),
270271
},
271272
)?)?;
272273
}

0 commit comments

Comments
 (0)