Skip to content

Commit d762adf

Browse files
authored
analyze: borrowck performance improvements (#1111)
I found some easy borrowck/Polonius performance improvements while investigating a performance regression on a development branch. 1. Use `BufReader`/`BufWriter` when loading/saving from the `polonius_cache`. Otherwise `bincode` reads/writes each 8-byte field individually. 2. Don't dump Polonius facts to `inspect/FUNC/*.facts` unless requested with `C2RUST_ANALYZE_DUMP_POLONIUS_FACTS=1`. This takes a nontrivial amount of time for big functions and is only needed when debugging certain borrowck issues. Improvements compared to master (all measurements taken on the second run, after populating `polonius_cache/`): * `cargo test algo_md5`: 25s -> 4.6s * `cargo test --release algo_md5`: 16s -> 2.1s
2 parents b3aecb0 + 4dc0d1e commit d762adf

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

c2rust-analyze/src/borrowck/dump.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
1+
//! Copied partly from rustc `compiler/rustc_borrowck/src/facts.rs`, which is dual-licensed MIT and
2+
//! Apache 2.0.
13
use crate::borrowck::atoms::{AllFacts, AtomMaps, Loan, Origin, Output, Path, Point, Variable};
24
use rustc_hash::{FxHashMap, FxHashSet};
3-
/// Copied partly from rustc `compiler/rustc_borrowck/src/facts.rs`, which is dual-licensed MIT and
4-
/// Apache 2.0.
55
use std::collections::{BTreeMap, BTreeSet};
6+
use std::env;
67
use std::error::Error;
78
use std::fmt::Write as _;
89
use std::fs::{self, File};
910
use std::hash::Hash;
1011
use std::io::{BufWriter, Write};
1112
use std::path;
1213

14+
thread_local! {
15+
static DUMP_FACTS: bool = {
16+
env::var("C2RUST_ANALYZE_DUMP_POLONIUS_FACTS").map_or(false, |val| &val == "1")
17+
};
18+
}
19+
1320
pub fn dump_facts_to_dir(
1421
facts: &AllFacts,
1522
maps: &AtomMaps,
1623
dir: impl AsRef<path::Path>,
1724
) -> Result<(), Box<dyn Error>> {
25+
if !DUMP_FACTS.with(|&flag| flag) {
26+
return Ok(());
27+
}
1828
let dir: &path::Path = dir.as_ref();
1929
fs::create_dir_all(dir)?;
2030
let wr = FactWriter { maps, dir };
@@ -60,6 +70,9 @@ pub fn dump_output_to_dir(
6070
maps: &AtomMaps,
6171
dir: impl AsRef<path::Path>,
6272
) -> Result<(), Box<dyn Error>> {
73+
if !DUMP_FACTS.with(|&flag| flag) {
74+
return Ok(());
75+
}
6376
let dir: &path::Path = dir.as_ref();
6477
fs::create_dir_all(dir)?;
6578
let wr = FactWriter { maps, dir };

c2rust-analyze/src/borrowck/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use std::collections::HashMap;
1919
use std::fmt::{Debug, Formatter, Write as _};
2020
use std::fs::{self, File};
2121
use std::hash::{Hash, Hasher};
22+
use std::io::{BufReader, BufWriter};
2223

2324
mod atoms;
2425
mod def_use;
@@ -383,7 +384,7 @@ fn run_polonius<'tcx>(
383384
fn try_load_cached_output(facts_hash: &str) -> Option<Output> {
384385
let path = format!("polonius_cache/{}.output", facts_hash);
385386

386-
let f = File::open(&path).ok()?;
387+
let f = BufReader::new(File::open(&path).ok()?);
387388
let raw = match bincode::deserialize_from(f) {
388389
Ok(x) => x,
389390
Err(e) => {
@@ -492,7 +493,7 @@ fn save_cached_output(facts_hash: &str, output: &Output) -> Result<(), bincode::
492493
),
493494
);
494495

495-
let f = File::create(path)?;
496+
let f = BufWriter::new(File::create(path)?);
496497
bincode::serialize_into(f, &raw)
497498
}
498499

0 commit comments

Comments
 (0)