Skip to content

internal: Use ManuallyDrop in RootDatabase to improve build times #10069

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

Merged
merged 1 commit into from
Aug 28, 2021
Merged
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
20 changes: 16 additions & 4 deletions crates/ide_db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub mod path_transform;
pub mod search;
pub mod rename;

use std::{fmt, sync::Arc};
use std::{fmt, mem::ManuallyDrop, sync::Arc};

use base_db::{
salsa::{self, Durability},
Expand All @@ -44,7 +44,19 @@ pub use base_db;
hir::db::HirDatabaseStorage
)]
pub struct RootDatabase {
storage: salsa::Storage<RootDatabase>,
// We use `ManuallyDrop` here because every codegen unit that contains a
// `&RootDatabase -> &dyn OtherDatabase` cast will instantiate its drop glue in the vtable,
// which duplicates `Weak::drop` and `Arc::drop` tens of thousands of times, which makes
// compile times of all `ide_*` and downstream crates suffer greatly.
storage: ManuallyDrop<salsa::Storage<RootDatabase>>,
}

impl Drop for RootDatabase {
fn drop(&mut self) {
unsafe {
ManuallyDrop::drop(&mut self.storage);
}
}
}

impl fmt::Debug for RootDatabase {
Expand Down Expand Up @@ -93,7 +105,7 @@ impl Default for RootDatabase {

impl RootDatabase {
pub fn new(lru_capacity: Option<usize>) -> RootDatabase {
let mut db = RootDatabase { storage: salsa::Storage::default() };
let mut db = RootDatabase { storage: ManuallyDrop::new(salsa::Storage::default()) };
db.set_crate_graph_with_durability(Default::default(), Durability::HIGH);
db.set_local_roots_with_durability(Default::default(), Durability::HIGH);
db.set_library_roots_with_durability(Default::default(), Durability::HIGH);
Expand All @@ -112,7 +124,7 @@ impl RootDatabase {

impl salsa::ParallelDatabase for RootDatabase {
fn snapshot(&self) -> salsa::Snapshot<RootDatabase> {
salsa::Snapshot::new(RootDatabase { storage: self.storage.snapshot() })
salsa::Snapshot::new(RootDatabase { storage: ManuallyDrop::new(self.storage.snapshot()) })
}
}

Expand Down