Skip to content

Commit a82326d

Browse files
authored
perf(compile): use bytes already in memory after downloading executable (#28000)
Not sure if this will help #27988 but it's better anyway.
1 parent a0c5ef8 commit a82326d

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

cli/standalone/binary.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use capacity_builder::BytesAppendable;
1616
use deno_ast::MediaType;
1717
use deno_ast::ModuleKind;
1818
use deno_ast::ModuleSpecifier;
19+
use deno_cache_dir::CACHE_PERM;
1920
use deno_core::anyhow::bail;
2021
use deno_core::anyhow::Context;
2122
use deno_core::error::AnyError;
@@ -46,6 +47,7 @@ use deno_lib::util::hash::FastInsecureHasher;
4647
use deno_lib::version::DENO_VERSION_INFO;
4748
use deno_npm::resolution::SerializedNpmResolutionSnapshot;
4849
use deno_npm::NpmSystemInfo;
50+
use deno_path_util::fs::atomic_write_file_with_retries;
4951
use deno_path_util::url_from_directory_path;
5052
use deno_path_util::url_to_file_path;
5153
use deno_resolver::workspace::WorkspaceResolver;
@@ -285,17 +287,17 @@ impl<'a> DenoCompileBinaryWriter<'a> {
285287
let download_directory = self.deno_dir.dl_folder_path();
286288
let binary_path = download_directory.join(&binary_path_suffix);
287289

288-
if !binary_path.exists() {
289-
self
290-
.download_base_binary(&download_directory, &binary_path_suffix)
291-
.await
292-
.context("Setting up base binary.")?;
293-
}
294-
295290
let read_file = |path: &Path| -> Result<Vec<u8>, AnyError> {
296291
std::fs::read(path).with_context(|| format!("Reading {}", path.display()))
297292
};
298-
let archive_data = read_file(&binary_path)?;
293+
let archive_data = if binary_path.exists() {
294+
read_file(&binary_path)?
295+
} else {
296+
self
297+
.download_base_binary(&binary_path, &binary_path_suffix)
298+
.await
299+
.context("Setting up base binary.")?
300+
};
299301
let temp_dir = tempfile::TempDir::new()?;
300302
let base_binary_path = archive::unpack_into_dir(archive::UnpackArgs {
301303
exe_name: "denort",
@@ -311,9 +313,9 @@ impl<'a> DenoCompileBinaryWriter<'a> {
311313

312314
async fn download_base_binary(
313315
&self,
314-
output_directory: &Path,
316+
output_path: &Path,
315317
binary_path_suffix: &str,
316-
) -> Result<(), AnyError> {
318+
) -> Result<Vec<u8>, AnyError> {
317319
let download_url = format!("https://dl.deno.land/{binary_path_suffix}");
318320
let maybe_bytes = {
319321
let progress_bars = ProgressBar::new(ProgressBarStyle::DownloadBars);
@@ -340,12 +342,15 @@ impl<'a> DenoCompileBinaryWriter<'a> {
340342
std::fs::create_dir_all(dir)
341343
.with_context(|| format!("Creating {}", dir.display()))
342344
};
343-
create_dir_all(output_directory)?;
344-
let output_path = output_directory.join(binary_path_suffix);
345345
create_dir_all(output_path.parent().unwrap())?;
346-
std::fs::write(&output_path, bytes)
347-
.with_context(|| format!("Writing {}", output_path.display()))?;
348-
Ok(())
346+
atomic_write_file_with_retries(
347+
&CliSys::default(),
348+
output_path,
349+
&bytes,
350+
CACHE_PERM,
351+
)
352+
.with_context(|| format!("Writing {}", output_path.display()))?;
353+
Ok(bytes)
349354
}
350355

351356
/// This functions creates a standalone deno binary by appending a bundle

0 commit comments

Comments
 (0)