diff --git a/crates/ide-core/src/lib.rs b/crates/ide-core/src/lib.rs index 457e8ac55..64746680d 100644 --- a/crates/ide-core/src/lib.rs +++ b/crates/ide-core/src/lib.rs @@ -15,6 +15,7 @@ pub mod inspection_settings; pub mod ipc_path; pub mod java_runtime; pub mod jsonc; +pub mod memory; pub mod output_encoding; pub mod platform; pub mod process; diff --git a/crates/ide-core/src/memory.rs b/crates/ide-core/src/memory.rs new file mode 100644 index 000000000..e42e6fd6e --- /dev/null +++ b/crates/ide-core/src/memory.rs @@ -0,0 +1,20 @@ +//! Returning freed heap pages to the OS. + +/// Hands pages the allocator has freed but kept back to the OS. Call after a burst of +/// short-lived allocations (a full index scan); until then they still count towards the +/// process's memory footprint. +pub fn release_free_pages() { + #[cfg(target_os = "macos")] + { + unsafe extern "C" { + fn malloc_zone_pressure_relief(zone: *mut std::ffi::c_void, goal: usize) -> usize; + } + // SAFETY: a null zone covers every zone; a zero goal releases as much as possible. + unsafe { malloc_zone_pressure_relief(std::ptr::null_mut(), 0) }; + } + #[cfg(all(target_os = "linux", target_env = "gnu"))] + // SAFETY: malloc_trim only returns free memory to the OS. + unsafe { + libc::malloc_trim(0); + } +} diff --git a/crates/ide-index/src/store.rs b/crates/ide-index/src/store.rs index 3c6882148..a558c4649 100644 --- a/crates/ide-index/src/store.rs +++ b/crates/ide-index/src/store.rs @@ -13,6 +13,9 @@ const FILES: TableDefinition<&str, &[u8]> = TableDefinition::new("files"); const META: TableDefinition<&str, &[u8]> = TableDefinition::new("meta"); /// Bump when [`FileRecord`] or its encoding changes. 4: a symbol's base names. const SCHEMA: &str = "openidea-index-4"; +/// redb's page cache, which otherwise grows to 1 GiB. Queries run on the in-memory snapshot; +/// the database is read once at startup and written incrementally. +const DB_CACHE_BYTES: usize = 64 * 1024 * 1024; /// The real Git directory for a checkout/worktree (`root/.git` may be a file /// containing `gitdir: ...`). @@ -389,7 +392,7 @@ impl Store { fn open_db(&self) -> Result { let file = self.snapshot.with_file_name("index.redb"); - let db = match Database::create(&file) { + let db = match Database::builder().set_cache_size(DB_CACHE_BYTES).create(&file) { Ok(db) => db, Err(redb::DatabaseError::DatabaseAlreadyOpen) => { return Err(format!("{} is in use", file.display())); @@ -398,7 +401,7 @@ impl Store { tracing::warn!("index cache {} unreadable ({e}); rebuilding", file.display()); let _ = std::fs::remove_file(&file); let _ = std::fs::remove_file(&self.snapshot); - Database::create(&file).map_err(err)? + Database::builder().set_cache_size(DB_CACHE_BYTES).create(&file).map_err(err)? } }; let valid = { diff --git a/crates/ide-index/src/worker.rs b/crates/ide-index/src/worker.rs index a220d5d4b..4405ad6a1 100644 --- a/crates/ide-index/src/worker.rs +++ b/crates/ide-index/src/worker.rs @@ -472,6 +472,8 @@ impl Shared { fn full_scan(&self, found: Option>, guard: SystemTime) { self.scan(None, found, guard); + // Parsing every file churns through far more memory than the index keeps. + ide_core::memory::release_free_pages(); } fn scan(&self, dirs: Option<&[PathBuf]>, prewalked: Option>, guard: SystemTime) -> Option { diff --git a/crates/ide-vcs/src/log_index.rs b/crates/ide-vcs/src/log_index.rs index cd6efd63f..a84afd16d 100644 --- a/crates/ide-vcs/src/log_index.rs +++ b/crates/ide-vcs/src/log_index.rs @@ -19,6 +19,9 @@ const MESSAGES: TableDefinition<&str, &str> = TableDefinition::new("messages-v1" const MAX_MEMORY_COMMITS: usize = 4096; const MAX_MEMORY_MESSAGES: usize = 1024; const MAX_DIRTY_BATCH: usize = 16_384; +/// redb's page cache, which otherwise grows to 1 GiB per repository. Hot commits are already +/// kept in `memory`. +const DB_CACHE_BYTES: usize = 32 * 1024 * 1024; pub(crate) struct IndexedRow { pub id: ObjectId, @@ -269,7 +272,11 @@ impl LogIndex { let path = dir.join("commits.redb"); let mut databases = DATABASES.get_or_init(Mutex::default).lock().unwrap_or_else(|p| p.into_inner()); self.db = databases.get(&path).and_then(Weak::upgrade).or_else(|| { - let db = Arc::new(std::fs::create_dir_all(dir).ok().and_then(|_| Database::create(&path).ok())?); + let db = Arc::new( + std::fs::create_dir_all(dir) + .ok() + .and_then(|_| Database::builder().set_cache_size(DB_CACHE_BYTES).create(&path).ok())?, + ); drop_stale_tables(&db); databases.insert(path, Arc::downgrade(&db)); Some(db)