Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: use Arc<[u8]> instead of Arc<Vec<u8>> #139

Merged
merged 1 commit into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 311,7 @@ pub async fn build_eszip(
eszip.add_import_map(
ModuleKind::Json,
import_map_specifier.to_string(),
Arc::new(import_map_content.as_bytes().to_vec()),
Arc::from(import_map_content),
)
}
Ok(Uint8Array::from(eszip.into_bytes().as_slice()))
Expand Down
2 changes: 1 addition & 1 deletion src/examples/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 69,7 @@ async fn main() {
eszip.add_import_map(
eszip::ModuleKind::Json,
import_map_specifier.to_string(),
Arc::new(import_map_content.as_bytes().to_vec()),
Arc::from(import_map_content),
)
}
for specifier in eszip.specifiers() {
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 103,7 @@ pub enum ModuleInner {

impl Module {
/// Get source code of the module.
pub async fn source(&self) -> Option<Arc<Vec<u8>>> {
pub async fn source(&self) -> Option<Arc<[u8]>> {
match &self.inner {
ModuleInner::V1(eszip_v1) => eszip_v1.get_module_source(&self.specifier),
ModuleInner::V2(eszip_v2) => {
Expand All @@ -116,7 116,7 @@ impl Module {
/// the subsequent calls to `take_source()` will return `None`.
/// For V1, this will take the entire module and returns the source code. We don't need
/// to preserve module metadata for V1.
pub async fn take_source(&self) -> Option<Arc<Vec<u8>>> {
pub async fn take_source(&self) -> Option<Arc<[u8]>> {
match &self.inner {
ModuleInner::V1(eszip_v1) => eszip_v1.take(&self.specifier),
ModuleInner::V2(eszip_v2) => {
Expand All @@ -126,7 126,7 @@ impl Module {
}

/// Get source map of the module.
pub async fn source_map(&self) -> Option<Arc<Vec<u8>>> {
pub async fn source_map(&self) -> Option<Arc<[u8]>> {
match &self.inner {
ModuleInner::V1(_) => None,
ModuleInner::V2(eszip) => {
Expand All @@ -137,7 137,7 @@ impl Module {

/// Take source map of the module. This will remove the source map from memory and
/// the subsequent calls to `take_source_map()` will return `None`.
pub async fn take_source_map(&self) -> Option<Arc<Vec<u8>>> {
pub async fn take_source_map(&self) -> Option<Arc<[u8]>> {
match &self.inner {
ModuleInner::V1(_) => None,
ModuleInner::V2(eszip) => {
Expand Down
17 changes: 7 additions & 10 deletions src/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 77,21 @@ impl EszipV1 {
}

/// Get source code of the module.
pub(crate) fn get_module_source(
&self,
specifier: &str,
) -> Option<Arc<Vec<u8>>> {
pub(crate) fn get_module_source(&self, specifier: &str) -> Option<Arc<[u8]>> {
let specifier = &Url::parse(specifier).ok()?;
let modules = self.modules.lock().unwrap();
let module = modules.get(specifier).unwrap();
match module {
ModuleInfo::Redirect(_) => panic!("Redirects should be resolved"),
ModuleInfo::Source(module) => {
let source = module.transpiled.as_ref().unwrap_or(&module.source);
Some(Arc::new(source.clone().into_bytes()))
Some(source.clone().into())
}
}
}

/// Removes the module from the modules map and returns the source code.
pub(crate) fn take(&self, specifier: &str) -> Option<Arc<Vec<u8>>> {
pub(crate) fn take(&self, specifier: &str) -> Option<Arc<[u8]>> {
let specifier = &Url::parse(specifier).ok()?;
let mut modules = self.modules.lock().unwrap();
// Note: we don't have a need to preserve the module in the map for v1, so we can
Expand All @@ -105,7 102,7 @@ impl EszipV1 {
ModuleInfo::Redirect(_) => panic!("Redirects should be resolved"),
ModuleInfo::Source(module_source) => {
let source = module_source.transpiled.unwrap_or(module_source.source);
Some(Arc::new(source.into_bytes()))
Some(source.into())
}
}
}
Expand All @@ -119,8 116,8 @@ pub enum ModuleInfo {

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct ModuleSource {
pub source: String,
pub transpiled: Option<String>,
pub source: Arc<str>,
pub transpiled: Option<Arc<str>>,
pub content_type: Option<String>,
pub deps: Vec<Url>,
}
Expand All @@ -145,7 142,7 @@ mod tests {
}
crate::ModuleInner::V2(_) => unreachable!(),
};
assert_eq!(*bytes, b"addEventListener(\"fetch\", (event)=>{\n event.respondWith(new Response(\"Hello World\", {\n headers: {\n \"content-type\": \"text/plain\"\n }\n }));\n});\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxodHRwczovL2dpc3QuZ2l0aHVidXNlcmNvbnRlbnQuY29tL2x1Y2FjYXNvbmF0by9mM2UyMTQwNTMyMjI1OWNhNGVkMTU1NzIyMzkwZmRhMi9yYXcvZTI1YWNiNDliNjgxZThlMWRhNWEyYTMzNzQ0YjdhMzZkNTM4NzEyZC9oZWxsby5qcz4iXSwic291cmNlc0NvbnRlbnQiOlsiYWRkRXZlbnRMaXN0ZW5lcihcImZldGNoXCIsIChldmVudCkgPT4ge1xuICBldmVudC5yZXNwb25kV2l0aChuZXcgUmVzcG9uc2UoXCJIZWxsbyBXb3JsZFwiLCB7XG4gICAgaGVhZGVyczogeyBcImNvbnRlbnQtdHlwZVwiOiBcInRleHQvcGxhaW5cIiB9LFxuICB9KSk7XG59KTsiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsZ0JBQUEsRUFBQSxLQUFBLElBQUEsS0FBQTtBQUNBLFNBQUEsQ0FBQSxXQUFBLEtBQUEsUUFBQSxFQUFBLFdBQUE7QUFDQSxlQUFBO2FBQUEsWUFBQSxJQUFBLFVBQUEifQ==");
assert_eq!(&*bytes, b"addEventListener(\"fetch\", (event)=>{\n event.respondWith(new Response(\"Hello World\", {\n headers: {\n \"content-type\": \"text/plain\"\n }\n }));\n});\n//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIjxodHRwczovL2dpc3QuZ2l0aHVidXNlcmNvbnRlbnQuY29tL2x1Y2FjYXNvbmF0by9mM2UyMTQwNTMyMjI1OWNhNGVkMTU1NzIyMzkwZmRhMi9yYXcvZTI1YWNiNDliNjgxZThlMWRhNWEyYTMzNzQ0YjdhMzZkNTM4NzEyZC9oZWxsby5qcz4iXSwic291cmNlc0NvbnRlbnQiOlsiYWRkRXZlbnRMaXN0ZW5lcihcImZldGNoXCIsIChldmVudCkgPT4ge1xuICBldmVudC5yZXNwb25kV2l0aChuZXcgUmVzcG9uc2UoXCJIZWxsbyBXb3JsZFwiLCB7XG4gICAgaGVhZGVyczogeyBcImNvbnRlbnQtdHlwZVwiOiBcInRleHQvcGxhaW5cIiB9LFxuICB9KSk7XG59KTsiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IkFBQUEsZ0JBQUEsRUFBQSxLQUFBLElBQUEsS0FBQTtBQUNBLFNBQUEsQ0FBQSxXQUFBLEtBQUEsUUFBQSxFQUFBLFdBQUE7QUFDQSxlQUFBO2FBQUEsWUFBQSxJQUFBLFVBQUEifQ==");
}

#[tokio::test]
Expand Down
49 changes: 25 additions & 24 deletions src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 47,7 @@ impl EszipV2Modules {
pub(crate) async fn get_module_source<'a>(
&'a self,
specifier: &str,
) -> Option<Arc<Vec<u8>>> {
) -> Option<Arc<[u8]>> {
poll_fn(|cx| {
let mut modules = self.0.lock().unwrap();
let module = modules.get_mut(specifier).unwrap();
Expand All @@ -72,7 72,7 @@ impl EszipV2Modules {
pub(crate) async fn take_module_source<'a>(
&'a self,
specifier: &str,
) -> Option<Arc<Vec<u8>>> {
) -> Option<Arc<[u8]>> {
poll_fn(|cx| {
let mut modules = self.0.lock().unwrap();
let module = modules.get_mut(specifier).unwrap();
Expand All @@ -99,7 99,7 @@ impl EszipV2Modules {
pub(crate) async fn get_module_source_map<'a>(
&'a self,
specifier: &str,
) -> Option<Arc<Vec<u8>>> {
) -> Option<Arc<[u8]>> {
poll_fn(|cx| {
let mut modules = self.0.lock().unwrap();
let module = modules.get_mut(specifier).unwrap();
Expand All @@ -124,7 124,7 @@ impl EszipV2Modules {
pub(crate) async fn take_module_source_map<'a>(
&'a self,
specifier: &str,
) -> Option<Arc<Vec<u8>>> {
) -> Option<Arc<[u8]>> {
let source = poll_fn(|cx| {
let mut modules = self.0.lock().unwrap();
let module = modules.get_mut(specifier).unwrap();
Expand Down Expand Up @@ -187,7 187,7 @@ pub enum EszipV2SourceSlot {
length: usize,
wakers: Vec<Waker>,
},
Ready(Arc<Vec<u8>>),
Ready(Arc<[u8]>),
Taken,
}

Expand Down Expand Up @@ -278,7 278,7 @@ impl EszipV2 {
n => return Err(ParseError::InvalidV2ModuleKind(n, read)),
};
let source = if source_offset == 0 && source_len == 0 {
EszipV2SourceSlot::Ready(Arc::new(vec![]))
EszipV2SourceSlot::Ready(Arc::new([]))
} else {
EszipV2SourceSlot::Pending {
offset: source_offset as usize,
Expand All @@ -287,7 287,7 @@ impl EszipV2 {
}
};
let source_map = if source_map_offset == 0 && source_map_len == 0 {
EszipV2SourceSlot::Ready(Arc::new(vec![]))
EszipV2SourceSlot::Ready(Arc::new([]))
} else {
EszipV2SourceSlot::Pending {
offset: source_map_offset as usize,
Expand Down Expand Up @@ -392,7 392,7 @@ impl EszipV2 {
EszipV2Module::Module { ref mut source, .. } => {
let slot = std::mem::replace(
source,
EszipV2SourceSlot::Ready(Arc::new(source_bytes)),
EszipV2SourceSlot::Ready(Arc::from(source_bytes)),
);

match slot {
Expand Down Expand Up @@ -440,7 440,7 @@ impl EszipV2 {
} => {
let slot = std::mem::replace(
source_map,
EszipV2SourceSlot::Ready(Arc::new(source_map_bytes)),
EszipV2SourceSlot::Ready(Arc::from(source_map_bytes)),
);

match slot {
Expand Down Expand Up @@ -478,7 478,7 @@ impl EszipV2 {
&mut self,
kind: ModuleKind,
specifier: String,
source: Arc<Vec<u8>>,
source: Arc<[u8]>,
) {
debug_assert!(matches!(kind, ModuleKind::Json | ModuleKind::Jsonc));

Expand All @@ -496,7 496,7 @@ impl EszipV2 {
EszipV2Module::Module {
kind,
source: EszipV2SourceSlot::Ready(source),
source_map: EszipV2SourceSlot::Ready(Arc::new(vec![])),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
},
);
modules.to_front(&specifier);
Expand Down Expand Up @@ -723,9 723,12 @@ impl EszipV2 {

match module {
deno_graph::Module::Esm(module) => {
let (source, source_map) = match module.media_type {
let source: Arc<[u8]>;
let source_map: Arc<[u8]>;
match module.media_type {
deno_graph::MediaType::JavaScript | deno_graph::MediaType::Mjs => {
(module.source.as_bytes().to_owned(), vec![])
source = Arc::from(module.source.clone());
source_map = Arc::new( []);
}
deno_graph::MediaType::Jsx
| deno_graph::MediaType::TypeScript
Expand All @@ -742,8 745,8 @@ impl EszipV2 {
text,
source_map: maybe_source_map,
} = parsed_source.transpile(emit_options)?;
let source_map = maybe_source_map.unwrap_or_default();
(text.into_bytes(), source_map.into_bytes())
source = Arc::from(text.into_bytes());
source_map = Arc::from(maybe_source_map.unwrap_or_default().into_bytes());
}
_ => {
return Err(anyhow::anyhow!(
Expand All @@ -757,8 760,8 @@ impl EszipV2 {
let specifier = module.specifier.to_string();
let eszip_module = EszipV2Module::Module {
kind: ModuleKind::JavaScript,
source: EszipV2SourceSlot::Ready(Arc::new(source)),
source_map: EszipV2SourceSlot::Ready(Arc::new(source_map)),
source: EszipV2SourceSlot::Ready(source),
source_map: EszipV2SourceSlot::Ready(source_map),
};
modules.insert(specifier, eszip_module);

Expand All @@ -782,10 785,8 @@ impl EszipV2 {
let specifier = module.specifier.to_string();
let eszip_module = EszipV2Module::Module {
kind: ModuleKind::Json,
source: EszipV2SourceSlot::Ready(Arc::new(
module.source.as_bytes().to_owned(),
)),
source_map: EszipV2SourceSlot::Ready(Arc::new(vec![])),
source: EszipV2SourceSlot::Ready( module.source.clone().into()),
source_map: EszipV2SourceSlot::Ready(Arc::new([])),
};
modules.insert(specifier, eszip_module);
Ok(())
Expand Down Expand Up @@ -1528,7 1529,7 @@ mod tests {
EmitOptions::default(),
)
.unwrap();
let import_map_bytes = Arc::new(content.as_bytes().to_vec());
let import_map_bytes = Arc::from(content);
eszip.add_import_map(
ModuleKind::Json,
specifier.to_string(),
Expand Down Expand Up @@ -1604,7 1605,7 @@ mod tests {
EmitOptions::default(),
)
.unwrap();
let import_map_bytes = Arc::new(content.as_bytes().to_vec());
let import_map_bytes = Arc::from(content);
eszip.add_import_map(
ModuleKind::Json,
specifier.to_string(),
Expand Down Expand Up @@ -1671,7 1672,7 @@ mod tests {
EmitOptions::default(),
)
.unwrap();
let import_map_bytes = Arc::new(content.as_bytes().to_vec());
let import_map_bytes = Arc::from(content);
eszip.add_import_map(
ModuleKind::Jsonc,
specifier.to_string(),
Expand Down