Skip to content

Commit

Permalink
Reduce more allocations to make it faster
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 8, 2024
1 parent a738384 commit 5f1a064
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 26 deletions.
48 changes: 24 additions & 24 deletions src/source/wasm.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// Copyright 2018-2024 the Deno authors. MIT license.

use std::borrow::Cow;

use indexmap::IndexSet;
use string_capacity::StringBuilder;
use wasm_dep_analyzer::ValueType;
Expand Down Expand Up @@ -42,21 +40,10 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
&& deno_ast::swc::ast::Ident::verify_symbol(export_name).is_ok()
}

let mut internal_names_count = 0;
let export_names = wasm_deps
let is_valid_export_ident_per_export = wasm_deps
.exports
.iter()
.map(|export| {
let has_valid_export_ident = is_valid_ident(export.name);
if has_valid_export_ident {
Cow::Borrowed(export.name)
} else {
let export_name =
format!("__deno_wasm_export_{}__", internal_names_count);
internal_names_count += 1;
Cow::Owned(export_name)
}
})
.map(|export| is_valid_ident(export.name))
.collect::<Vec<_>>();
let unique_import_modules = wasm_deps
.imports
Expand All @@ -72,14 +59,27 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
}

for (i, export) in wasm_deps.exports.iter().enumerate() {
let export_name = &export_names[i];
let has_valid_export_ident = matches!(export_name, Cow::Borrowed(_));
let has_valid_export_ident = is_valid_export_ident_per_export[i];
if has_valid_export_ident {
builder.append("export ");
}
fn write_export_name<'a>(
builder: &mut StringBuilder<'a>,
export: &'a wasm_dep_analyzer::Export<'a>,
has_valid_export_ident: bool,
index: usize,
) {
if has_valid_export_ident {
builder.append(export.name);
} else {
builder.append("__deno_wasm_export_");
builder.append(index);
builder.append("__");
}
}
let mut add_var = |type_text: &'static str| {
builder.append("declare const ");
builder.append(export_name);
write_export_name(builder, export, has_valid_export_ident, i);
builder.append(": ");
builder.append(type_text);
builder.append(";\n");
Expand All @@ -90,7 +90,7 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {
match function_signature {
Ok(signature) => {
builder.append("declare function ");
builder.append(export_name);
write_export_name(builder, export, has_valid_export_ident, i);
builder.append('(');
for (i, param) in signature.params.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -131,7 +131,7 @@ fn wasm_module_deps_to_dts(wasm_deps: &wasm_dep_analyzer::WasmDeps) -> String {

if !has_valid_export_ident {
builder.append("export { ");
builder.append(export_name);
write_export_name(builder, export, has_valid_export_ident, i);
builder.append(" as \"");
builder.append(export.name);
builder.append("\" };\n");
Expand Down Expand Up @@ -237,10 +237,10 @@ export declare const name5: WebAssembly.Memory;
export declare const name6: number;
export declare const name7: unknown;
export declare const name8: unknown;
declare const __deno_wasm_export_1__: unknown;
export { __deno_wasm_export_1__ as \"name9--\" };
declare const __deno_wasm_export_2__: unknown;
export { __deno_wasm_export_2__ as \"default\" };
declare const __deno_wasm_export_8__: unknown;
export { __deno_wasm_export_8__ as \"name9--\" };
declare const __deno_wasm_export_9__: unknown;
export { __deno_wasm_export_9__ as \"default\" };
"
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/specs/ecosystem/mrii/rocket_io/0_1_3.test
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ mrii/rocket-io/0.1.3

-- stderr --
error: Uncaught Error: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './build/esm/socket' is not defined for types by "exports" in '<global_npm_dir>/socket.io-client/4.7.5/package.json' imported from 'file://<tmpdir>/src/types/socket-reserved-events.ts'
at Object.resolveModuleNames (ext:deno_tsc/99_main_compiler.js:742:28)
at actualResolveModuleNamesWorker (ext:deno_tsc/00_typescript.js:125027:142)
at Object.resolveModuleNameLiterals (ext:deno_tsc/99_main_compiler.js:768:28)
at resolveModuleNamesWorker (ext:deno_tsc/00_typescript.js:125466:20)
at resolveNamesReusingOldState (ext:deno_tsc/00_typescript.js:125608:14)
at resolveModuleNamesReusingOldState (ext:deno_tsc/00_typescript.js:125564:12)
Expand All @@ -83,4 +82,5 @@ error: Uncaught Error: [ERR_PACKAGE_PATH_NOT_EXPORTED] Package subpath './build/
at findSourceFile (ext:deno_tsc/00_typescript.js:126705:20)
at processImportedModules (ext:deno_tsc/00_typescript.js:127105:11)
at findSourceFileWorker (ext:deno_tsc/00_typescript.js:126854:7)
at findSourceFile (ext:deno_tsc/00_typescript.js:126705:20)

0 comments on commit 5f1a064

Please sign in to comment.