Skip to content

Commit

Permalink
analyze: borrowck performance improvements (#1111)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
spernsteiner authored Aug 7, 2024
2 parents b3aecb0 + 4dc0d1e commit d762adf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 15 additions & 2 deletions c2rust-analyze/src/borrowck/dump.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
//! Copied partly from rustc `compiler/rustc_borrowck/src/facts.rs`, which is dual-licensed MIT and
//! Apache 2.0.
use crate::borrowck::atoms::{AllFacts, AtomMaps, Loan, Origin, Output, Path, Point, Variable};
use rustc_hash::{FxHashMap, FxHashSet};
/// Copied partly from rustc `compiler/rustc_borrowck/src/facts.rs`, which is dual-licensed MIT and
/// Apache 2.0.
use std::collections::{BTreeMap, BTreeSet};
use std::env;
use std::error::Error;
use std::fmt::Write as _;
use std::fs::{self, File};
use std::hash::Hash;
use std::io::{BufWriter, Write};
use std::path;

thread_local! {
static DUMP_FACTS: bool = {
env::var("C2RUST_ANALYZE_DUMP_POLONIUS_FACTS").map_or(false, |val| &val == "1")
};
}

pub fn dump_facts_to_dir(
facts: &AllFacts,
maps: &AtomMaps,
dir: impl AsRef<path::Path>,
) -> Result<(), Box<dyn Error>> {
if !DUMP_FACTS.with(|&flag| flag) {
return Ok(());
}
let dir: &path::Path = dir.as_ref();
fs::create_dir_all(dir)?;
let wr = FactWriter { maps, dir };
Expand Down Expand Up @@ -60,6 +70,9 @@ pub fn dump_output_to_dir(
maps: &AtomMaps,
dir: impl AsRef<path::Path>,
) -> Result<(), Box<dyn Error>> {
if !DUMP_FACTS.with(|&flag| flag) {
return Ok(());
}
let dir: &path::Path = dir.as_ref();
fs::create_dir_all(dir)?;
let wr = FactWriter { maps, dir };
Expand Down
5 changes: 3 additions & 2 deletions c2rust-analyze/src/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::collections::HashMap;
use std::fmt::{Debug, Formatter, Write as _};
use std::fs::{self, File};
use std::hash::{Hash, Hasher};
use std::io::{BufReader, BufWriter};

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

let f = File::open(&path).ok()?;
let f = BufReader::new(File::open(&path).ok()?);
let raw = match bincode::deserialize_from(f) {
Ok(x) => x,
Err(e) => {
Expand Down Expand Up @@ -492,7 +493,7 @@ fn save_cached_output(facts_hash: &str, output: &Output) -> Result<(), bincode::
),
);

let f = File::create(path)?;
let f = BufWriter::new(File::create(path)?);
bincode::serialize_into(f, &raw)
}

Expand Down

0 comments on commit d762adf

Please sign in to comment.