Skip to content

Deduplicate crates in crate graph #14476

@Veykril

Description

@Veykril

It's incredible that we aren't actually doing this already. When we load a project that contains multiple workspaces we do not deduplicate equal crates in the crategraph! That means just for the sysroot alone that it is loaded multiple times into memory and queries executed on the sysroot of one workspace are not reused for another. It wouldn't surprise me if some of the high memory usage reports we get comes from projects that have a lot of cargo workspaces in them.

Here all we do is append crates, but we are not deduplicating them. Nor are we doing so after having built the final crategraph.

/// Extends this crate graph by adding a complete disjoint second crate
/// graph and adjust the ids in the [`ProcMacroPaths`] accordingly.
///
/// The ids of the crates in the `other` graph are shifted by the return
/// amount.
pub fn extend(&mut self, other: CrateGraph, proc_macros: &mut ProcMacroPaths) -> u32 {
let start = self.arena.len() as u32;
self.arena.extend(other.arena.into_iter().map(|(id, mut data)| {
let new_id = id.shift(start);
for dep in &mut data.dependencies {
dep.crate_id = dep.crate_id.shift(start);
}
(new_id, data)
}));
*proc_macros = mem::take(proc_macros)
.into_iter()
.map(|(id, macros)| (id.shift(start), macros))
.collect();
start
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-perfperformance issuesBroken WindowBugs / technical debt to be addressed immediatelyC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions