diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2024-10-24 15:48:48 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-24 19:48:48 +0000 |
commit | eedf243b5ea98d22649bb0445444719a2fc12c59 (patch) | |
tree | 8f31d76e6223c195afb848f42d1ec7b9115bc7e1 /cli/util | |
parent | ea641897c92e3975dd102b31c1419720df358d12 (diff) |
perf(compile): pass module source data from binary directly to v8 (#26494)
This changes denort to pass a static reference of the moude source bytes found in the binary to v8 instead of copying it.
Diffstat (limited to 'cli/util')
-rw-r--r-- | cli/util/text_encoding.rs | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/cli/util/text_encoding.rs b/cli/util/text_encoding.rs index 0b7601cb9..df72cc2be 100644 --- a/cli/util/text_encoding.rs +++ b/cli/util/text_encoding.rs @@ -103,6 +103,21 @@ pub fn arc_str_to_bytes(arc_str: Arc<str>) -> Arc<[u8]> { unsafe { Arc::from_raw(raw as *const [u8]) } } +/// Converts an `Arc<u8>` to an `Arc<str>` if able. +#[allow(dead_code)] +pub fn arc_u8_to_arc_str( + arc_u8: Arc<[u8]>, +) -> Result<Arc<str>, std::str::Utf8Error> { + // Check that the string is valid UTF-8. + std::str::from_utf8(&arc_u8)?; + // SAFETY: the string is valid UTF-8, and the layout Arc<[u8]> is the same as + // Arc<str>. This is proven by the From<Arc<str>> impl for Arc<[u8]> from the + // standard library. + Ok(unsafe { + std::mem::transmute::<std::sync::Arc<[u8]>, std::sync::Arc<str>>(arc_u8) + }) +} + #[cfg(test)] mod tests { use std::sync::Arc; |