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

fix(caches/in-memory): allow explicit in-memory caches #26807

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions cli/args/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@ pub struct Flags {
pub argv: Vec<String>,
pub subcommand: DenoSubcommand,

/// Flag
pub in_memory_cache: Option<bool>,
pub frozen_lockfile: Option<bool>,
pub ca_stores: Option<Vec<String>>,
pub ca_data: Option<CaData>,
Expand Down Expand Up @@ -2821,6 +2823,7 @@ fn run_args(command: Command, top_level: bool) -> Command {
})
.arg(env_file_arg())
.arg(no_code_cache_arg())
.arg(in_memory_cache_arg())
}

fn run_subcommand() -> Command {
Expand Down Expand Up @@ -3951,6 +3954,13 @@ fn no_clear_screen_arg() -> Arg {
.help_heading(FILE_WATCHING_HEADING)
}

fn in_memory_cache_arg() -> Arg {
Arg::new("in_memory_cache")
.long("in-memory-cache")
.help("Disable all filesystem caches and use in-memory cache only")
.action(ArgAction::SetTrue)
}

fn no_code_cache_arg() -> Arg {
Arg::new("no-code-cache")
.long("no-code-cache")
Expand Down
4 changes: 4 additions & 0 deletions cli/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,10 @@ impl CliOptions {
self.flags.code_cache_enabled
}

pub fn in_memory_cache(&self) -> bool {
self.flags.in_memory_cache.unwrap_or(false)
}

pub fn watch_paths(&self) -> Vec<PathBuf> {
let mut full_paths = Vec::new();
if let DenoSubcommand::Run(RunFlags {
Expand Down
17 changes: 15 additions & 2 deletions cli/cache/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::module_info::MODULE_INFO_CACHE_DB;
use super::node::NODE_ANALYSIS_CACHE_DB;

pub struct Caches {
in_memory: bool,
dir_provider: Arc<DenoDirProvider>,
fmt_incremental_cache_db: OnceCell<CacheDB>,
lint_incremental_cache_db: OnceCell<CacheDB>,
Expand All @@ -27,8 +28,9 @@ pub struct Caches {
}

impl Caches {
pub fn new(dir: Arc<DenoDirProvider>) -> Self {
pub fn new(dir: Arc<DenoDirProvider>, in_memory: bool) -> Self {
Self {
in_memory,
dir_provider: dir,
fmt_incremental_cache_db: Default::default(),
lint_incremental_cache_db: Default::default(),
Expand All @@ -41,13 +43,17 @@ impl Caches {
}

fn make_db(
&self,
cell: &OnceCell<CacheDB>,
config: &'static CacheDBConfiguration,
path: Option<PathBuf>,
) -> CacheDB {
cell
.get_or_init(|| {
if let Some(path) = path {
// Return in memory cache if in_memory is true
if self.in_memory {
CacheDB::in_memory(config, crate::version::DENO_VERSION_INFO.deno)
} else if let Some(path) = path {
CacheDB::from_path(
config,
path,
Expand All @@ -62,6 +68,7 @@ impl Caches {

pub fn fmt_incremental_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.fmt_incremental_cache_db,
&INCREMENTAL_CACHE_DB,
self
Expand All @@ -74,6 +81,7 @@ impl Caches {

pub fn lint_incremental_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.lint_incremental_cache_db,
&INCREMENTAL_CACHE_DB,
self
Expand All @@ -86,6 +94,7 @@ impl Caches {

pub fn dep_analysis_db(&self) -> CacheDB {
Self::make_db(
self,
&self.dep_analysis_db,
&MODULE_INFO_CACHE_DB,
self
Expand All @@ -98,6 +107,7 @@ impl Caches {

pub fn fast_check_db(&self) -> CacheDB {
Self::make_db(
self,
&self.fast_check_db,
&FAST_CHECK_CACHE_DB,
self
Expand All @@ -110,6 +120,7 @@ impl Caches {

pub fn node_analysis_db(&self) -> CacheDB {
Self::make_db(
self,
&self.node_analysis_db,
&NODE_ANALYSIS_CACHE_DB,
self
Expand All @@ -122,6 +133,7 @@ impl Caches {

pub fn type_checking_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.type_checking_cache_db,
&TYPE_CHECK_CACHE_DB,
self
Expand All @@ -134,6 +146,7 @@ impl Caches {

pub fn code_cache_db(&self) -> CacheDB {
Self::make_db(
self,
&self.code_cache_db,
&CODE_CACHE_DB,
self
Expand Down
5 changes: 4 additions & 1 deletion cli/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,10 @@ impl CliFactory {
pub fn caches(&self) -> Result<&Arc<Caches>, AnyError> {
self.services.caches.get_or_try_init(|| {
let cli_options = self.cli_options()?;
let caches = Arc::new(Caches::new(self.deno_dir_provider()?.clone()));
let caches = Arc::new(Caches::new(
self.deno_dir_provider()?.clone(),
cli_options.in_memory_cache(),
));
// Warm up the caches we know we'll likely need based on the CLI mode
match cli_options.sub_command() {
DenoSubcommand::Run(_)
Expand Down
2 changes: 1 addition & 1 deletion cli/standalone/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ pub async fn run(data: StandaloneData) -> Result<i32, AnyError> {
unstable_detect_cjs: metadata.unstable_config.detect_cjs,
},
));
let cache_db = Caches::new(deno_dir_provider.clone());
let cache_db = Caches::new(deno_dir_provider.clone(), false);
let node_analysis_cache = NodeAnalysisCache::new(cache_db.node_analysis_db());
let cli_node_resolver = Arc::new(CliNodeResolver::new(
cjs_tracker.clone(),
Expand Down