From c89f9f9ad1e3d940a460092ee7bc44eb046fa1e1 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 20 May 2024 13:49:12 -0400 Subject: [PATCH] perf(cache): compile ts to js in parallel for `deno cache` (#23892) Closes https://github.com/denoland/deno/issues/23860 --- cli/emit.rs | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/cli/emit.rs b/cli/emit.rs index a3352e01f8..2106442262 100644 --- a/cli/emit.rs +++ b/cli/emit.rs @@ -7,6 +7,9 @@ use crate::cache::ParsedSourceCache; use deno_ast::SourceMapOption; use deno_ast::TranspileResult; use deno_core::error::AnyError; +use deno_core::futures::stream::FuturesUnordered; +use deno_core::futures::FutureExt; +use deno_core::futures::StreamExt; use deno_core::ModuleCodeString; use deno_core::ModuleSpecifier; use deno_graph::MediaType; @@ -48,28 +51,37 @@ impl Emitter { &self, graph: &ModuleGraph, ) -> Result<(), AnyError> { - // todo(dsherret): we could do this concurrently + let mut futures = FuturesUnordered::new(); for module in graph.modules() { - if let Module::Js(module) = module { - let is_emittable = matches!( - module.media_type, - MediaType::TypeScript - | MediaType::Mts - | MediaType::Cts - | MediaType::Jsx - | MediaType::Tsx - ); - if is_emittable { + let Module::Js(module) = module else { + continue; + }; + + let is_emittable = matches!( + module.media_type, + MediaType::TypeScript + | MediaType::Mts + | MediaType::Cts + | MediaType::Jsx + | MediaType::Tsx + ); + if is_emittable { + futures.push( self .emit_parsed_source( &module.specifier, module.media_type, &module.source, ) - .await?; - } + .boxed_local(), + ); } } + + while let Some(result) = futures.next().await { + result?; // surface errors + } + Ok(()) }