From 86c3c4f34397a29c2bf1847bddfea562a2369a4f Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Mon, 27 Mar 2023 16:01:52 -0600 Subject: feat(core): initialize SQLite off-main-thread (#18401) This gets SQLite off the flamegraph and reduces initialization time by somewhere between 0.2ms and 0.5ms. In addition, I took the opportunity to move all the cache management code to a single place and reduce duplication. While the PR has a net gain of lines, much of that is just being a bit more deliberate with how we're recovering from errors. The existing caches had various policies for dealing with cache corruption, so I've unified them and tried to isolate the decisions we make for recovery in a single place (see `open_connection` in `CacheDB`). The policy I chose was: 1. Retry twice to open on-disk caches 2. If that fails, try to delete the file and recreate it on-disk 3. If we fail to delete the file or re-create a new cache, use a fallback strategy that can be chosen per-cache: InMemory (temporary cache for the process run), BlackHole (ignore writes, return empty reads), or Error (fail on every operation). The caches all use the same general code now, and share the cache failure recovery policy. In addition, it cleans up a TODO in the `NodeAnalysisCache`. --- test_util/src/lib.rs | 111 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 103 insertions(+), 8 deletions(-) (limited to 'test_util/src') diff --git a/test_util/src/lib.rs b/test_util/src/lib.rs index 33b5ae8bc..d4effd88b 100644 --- a/test_util/src/lib.rs +++ b/test_util/src/lib.rs @@ -1897,22 +1897,117 @@ pub fn new_deno_dir() -> TempDir { TempDir::new() } +/// Because we need to keep the [`TempDir`] alive for the entire run of this command, +/// we have to effectively reproduce the entire builder-pattern object for [`Command`]. pub struct DenoCmd { - // keep the deno dir directory alive for the duration of the command _deno_dir: TempDir, cmd: Command, } -impl Deref for DenoCmd { - type Target = Command; - fn deref(&self) -> &Command { - &self.cmd +impl DenoCmd { + pub fn args(&mut self, args: I) -> &mut Self + where + I: IntoIterator, + S: AsRef, + { + self.cmd.args(args); + self + } + + pub fn arg(&mut self, arg: S) -> &mut Self + where + S: AsRef, + { + self.cmd.arg(arg); + self + } + + pub fn envs(&mut self, vars: I) -> &mut Self + where + I: IntoIterator, + K: AsRef, + V: AsRef, + { + self.cmd.envs(vars); + self + } + + pub fn env(&mut self, key: K, val: V) -> &mut Self + where + K: AsRef, + V: AsRef, + { + self.cmd.env(key, val); + self + } + + pub fn env_remove(&mut self, key: K) -> &mut Self + where + K: AsRef, + { + self.cmd.env_remove(key); + self + } + + pub fn stdin>(&mut self, cfg: T) -> &mut Self { + self.cmd.stdin(cfg); + self + } + + pub fn stdout>(&mut self, cfg: T) -> &mut Self { + self.cmd.stdout(cfg); + self + } + + pub fn stderr>(&mut self, cfg: T) -> &mut Self { + self.cmd.stderr(cfg); + self + } + + pub fn current_dir>(&mut self, dir: P) -> &mut Self { + self.cmd.current_dir(dir); + self + } + + pub fn output(&mut self) -> Result { + self.cmd.output() + } + + pub fn status(&mut self) -> Result { + self.cmd.status() + } + + pub fn spawn(&mut self) -> Result { + Ok(DenoChild { + _deno_dir: self._deno_dir.clone(), + child: self.cmd.spawn()?, + }) + } +} + +/// We need to keep the [`TempDir`] around until the child has finished executing, so +/// this acts as a RAII guard. +pub struct DenoChild { + _deno_dir: TempDir, + child: Child, +} + +impl Deref for DenoChild { + type Target = Child; + fn deref(&self) -> &Child { + &self.child + } +} + +impl DerefMut for DenoChild { + fn deref_mut(&mut self) -> &mut Child { + &mut self.child } } -impl DerefMut for DenoCmd { - fn deref_mut(&mut self) -> &mut Command { - &mut self.cmd +impl DenoChild { + pub fn wait_with_output(self) -> Result { + self.child.wait_with_output() } } -- cgit v1.2.3