internal: add "Shuffle Crate Graph" command

This commit is contained in:
Jonas Schievink 2021-12-07 15:38:12 +01:00
parent 9ae3158292
commit deb5c1426d
10 changed files with 106 additions and 0 deletions

1
.gitignore vendored
View File

@ -13,3 +13,4 @@ generated_diagnostic.adoc
.DS_Store
/out/
/dump.lsif
.envrc

View File

@ -56,6 +56,7 @@ mod typing;
mod view_crate_graph;
mod view_hir;
mod view_item_tree;
mod shuffle_crate_graph;
use std::sync::Arc;
@ -177,6 +178,10 @@ impl AnalysisHost {
pub fn raw_database_mut(&mut self) -> &mut RootDatabase {
&mut self.db
}
pub fn shuffle_crate_graph(&mut self) {
shuffle_crate_graph::shuffle_crate_graph(&mut self.db);
}
}
impl Default for AnalysisHost {

View File

@ -0,0 +1,70 @@
use std::sync::Arc;
use ide_db::base_db::salsa::Durability;
use ide_db::base_db::{CrateGraph, SourceDatabase};
use ide_db::RootDatabase;
use rustc_hash::FxHashMap;
// Feature: Shuffle Crate Graph
//
// Randomizes all crate IDs in the crate graph, for debugging.
//
// |===
// | Editor | Action Name
//
// | VS Code | **Rust Analyzer: Shuffle Crate Graph**
// |===
pub(crate) fn shuffle_crate_graph(db: &mut RootDatabase) {
let crate_graph = db.crate_graph();
let mut shuffled_ids = crate_graph.iter().collect::<Vec<_>>();
shuffle(&mut shuffled_ids);
let mut new_graph = CrateGraph::default();
let mut map = FxHashMap::default();
for old_id in shuffled_ids.iter().copied() {
let data = &crate_graph[old_id];
let new_id = new_graph.add_crate_root(
data.root_file_id,
data.edition,
data.display_name.clone(),
data.version.clone(),
data.cfg_options.clone(),
data.potential_cfg_options.clone(),
data.env.clone(),
data.proc_macro.clone(),
data.origin.clone(),
);
map.insert(old_id, new_id);
}
for old_id in shuffled_ids.iter().copied() {
let data = &crate_graph[old_id];
for dep in &data.dependencies {
let mut new_dep = dep.clone();
new_dep.crate_id = map[&dep.crate_id];
new_graph.add_dep(map[&old_id], new_dep).unwrap();
}
}
db.set_crate_graph_with_durability(Arc::new(new_graph), Durability::HIGH);
}
fn shuffle<T>(slice: &mut [T]) {
let mut rng = oorandom::Rand32::new(seed());
let mut remaining = slice.len() - 1;
while remaining > 0 {
let index = rng.rand_range(0..remaining as u32);
slice.swap(remaining, index as usize);
remaining -= 1;
}
}
fn seed() -> u64 {
use std::collections::hash_map::RandomState;
use std::hash::{BuildHasher, Hasher};
RandomState::new().build_hasher().finish()
}

View File

@ -97,6 +97,11 @@ pub(crate) fn handle_memory_usage(state: &mut GlobalState, _: ()) -> Result<Stri
Ok(out)
}
pub(crate) fn handle_shuffle_crate_graph(state: &mut GlobalState, _: ()) -> Result<()> {
state.analysis_host.shuffle_crate_graph();
Ok(())
}
pub(crate) fn handle_syntax_tree(
snap: GlobalStateSnapshot,
params: lsp_ext::SyntaxTreeParams,

View File

@ -31,6 +31,14 @@ impl Request for MemoryUsage {
const METHOD: &'static str = "rust-analyzer/memoryUsage";
}
pub enum ShuffleCrateGraph {}
impl Request for ShuffleCrateGraph {
type Params = ();
type Result = ();
const METHOD: &'static str = "rust-analyzer/shuffleCrateGraph";
}
pub enum ReloadWorkspace {}
impl Request for ReloadWorkspace {

View File

@ -567,6 +567,7 @@ impl GlobalState {
Ok(())
})?
.on_sync_mut::<lsp_ext::MemoryUsage>(handlers::handle_memory_usage)?
.on_sync_mut::<lsp_ext::ShuffleCrateGraph>(handlers::handle_shuffle_crate_graph)?
.on_sync::<lsp_ext::JoinLines>(handlers::handle_join_lines)?
.on_sync::<lsp_ext::OnEnter>(handlers::handle_on_enter)?
.on_sync::<lsp_types::request::SelectionRangeRequest>(handlers::handle_selection_range)?

View File

@ -171,6 +171,11 @@
"title": "Memory Usage (Clears Database)",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.shuffleCrateGraph",
"title": "Shuffle Crate Graph",
"category": "Rust Analyzer"
},
{
"command": "rust-analyzer.reloadWorkspace",
"title": "Reload workspace",

View File

@ -85,6 +85,15 @@ export function memoryUsage(ctx: Ctx): Cmd {
};
}
export function shuffleCrateGraph(ctx: Ctx): Cmd {
return async () => {
const client = ctx.client;
if (!client) return;
await client.sendRequest(ra.shuffleCrateGraph);
};
}
export function matchingBrace(ctx: Ctx): Cmd {
return async () => {
const editor = ctx.activeRustEditor;

View File

@ -9,6 +9,7 @@ export interface AnalyzerStatusParams {
}
export const analyzerStatus = new lc.RequestType<AnalyzerStatusParams, string, void>("rust-analyzer/analyzerStatus");
export const memoryUsage = new lc.RequestType0<string, void>("rust-analyzer/memoryUsage");
export const shuffleCrateGraph = new lc.RequestType0<null, void>("rust-analyzer/shuffleCrateGraph");
export interface ServerStatusParams {
health: "ok" | "warning" | "error";

View File

@ -117,6 +117,7 @@ async function initCommonContext(context: vscode.ExtensionContext, ctx: Ctx) {
ctx.registerCommand('analyzerStatus', commands.analyzerStatus);
ctx.registerCommand('memoryUsage', commands.memoryUsage);
ctx.registerCommand('shuffleCrateGraph', commands.shuffleCrateGraph);
ctx.registerCommand('reloadWorkspace', commands.reloadWorkspace);
ctx.registerCommand('matchingBrace', commands.matchingBrace);
ctx.registerCommand('joinLines', commands.joinLines);