Skip to content

Commit f2573fe

Browse files
committed
perf(stack-encrypt): move a document's text into its frame
json_frame copied the text into the Protected value and wiped the original. Taking the buffer moves it, so the plaintext exists once. Claude-Session: https://claude.ai/code/session_01N7bCqa3ZVJMcEE99RzVqEu
1 parent c031644 commit f2573fe

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

  • packages/stack-encrypt/src/dynamic

‎packages/stack-encrypt/src/dynamic/json.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,17 @@ pub fn read_json(value: &ValueFrame) -> Result<JsonPlaintext, JsonValueError> {
8787
}
8888

8989
/// A document as the value a `json` field opens to: its JSON text
90-
/// ([`to_text`]), in a bytes leaf. The text is written into the frame's
91-
/// protected buffer and the copy wiped.
90+
/// ([`to_text`]), in a bytes leaf. The text's buffer moves into the
91+
/// frame's protected value, so no second copy of it is made.
9292
///
9393
/// # Errors
9494
///
9595
/// [`crate::Error::Other`] if the frame writer refuses the bytes, which a
9696
/// document's text never is.
9797
pub fn json_frame(value: &Value) -> Result<ValueFrame, crate::Error> {
98-
let text = Zeroizing::new(to_text(value).map_err(|e| crate::Error::Other(Box::new(e)))?);
99-
ValueFrame::of(Protected::new(text.to_vec())).map_err(|e| crate::Error::Other(Box::new(e)))
98+
let mut text = Zeroizing::new(to_text(value).map_err(|e| crate::Error::Other(Box::new(e)))?);
99+
ValueFrame::of(Protected::new(std::mem::take(&mut *text)))
100+
.map_err(|e| crate::Error::Other(Box::new(e)))
100101
}
101102

102103
#[cfg(test)]

0 commit comments

Comments
 (0)