Skip to content
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

analyze: borrowck performance improvements #1111

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
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
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()?);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it faster to just read it all at once? Sometimes I've found that to be faster.

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
Loading