From cdd81ca5b991829077d9a38bf88b184cbb58054c Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Tue, 6 Aug 2024 13:48:52 -0700 Subject: [PATCH 1/3] analyze: borrowck: use BufReader/BufWriter for polonius cache save/load --- c2rust-analyze/src/borrowck/mod.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/c2rust-analyze/src/borrowck/mod.rs b/c2rust-analyze/src/borrowck/mod.rs index 19fa1b621f..2330a62d14 100644 --- a/c2rust-analyze/src/borrowck/mod.rs +++ b/c2rust-analyze/src/borrowck/mod.rs @@ -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; @@ -383,7 +384,7 @@ fn run_polonius<'tcx>( fn try_load_cached_output(facts_hash: &str) -> Option { 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) => { @@ -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) } From ff7517587ff0d5a394b932c6c185a9edfa7523ac Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Tue, 6 Aug 2024 16:33:24 -0700 Subject: [PATCH 2/3] analyze: fix doc comment in borrowck::dump --- c2rust-analyze/src/borrowck/dump.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/c2rust-analyze/src/borrowck/dump.rs b/c2rust-analyze/src/borrowck/dump.rs index be9fa03485..9a918122ee 100644 --- a/c2rust-analyze/src/borrowck/dump.rs +++ b/c2rust-analyze/src/borrowck/dump.rs @@ -1,7 +1,7 @@ +//! 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::error::Error; use std::fmt::Write as _; From 4dc0d1e61c286e0da9cb4bd8a2d9a9dd4773a52d Mon Sep 17 00:00:00 2001 From: Stuart Pernsteiner Date: Tue, 6 Aug 2024 16:33:53 -0700 Subject: [PATCH 3/3] analyze: borrowck: don't dump facts to disk for debugging by default --- c2rust-analyze/src/borrowck/dump.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/c2rust-analyze/src/borrowck/dump.rs b/c2rust-analyze/src/borrowck/dump.rs index 9a918122ee..dd3414cd4c 100644 --- a/c2rust-analyze/src/borrowck/dump.rs +++ b/c2rust-analyze/src/borrowck/dump.rs @@ -3,6 +3,7 @@ use crate::borrowck::atoms::{AllFacts, AtomMaps, Loan, Origin, Output, Path, Point, Variable}; use rustc_hash::{FxHashMap, FxHashSet}; use std::collections::{BTreeMap, BTreeSet}; +use std::env; use std::error::Error; use std::fmt::Write as _; use std::fs::{self, File}; @@ -10,11 +11,20 @@ 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, ) -> Result<(), Box> { + 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 }; @@ -60,6 +70,9 @@ pub fn dump_output_to_dir( maps: &AtomMaps, dir: impl AsRef, ) -> Result<(), Box> { + 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 };