From 1be58056e18be6ecc576faa2957536c43156c11f Mon Sep 17 00:00:00 2001 From: Yoshiki Matsuda Date: Fri, 29 Apr 2022 16:54:27 +0900 Subject: [PATCH] use BufReader for counting zero bytes --- compiler/rustc_metadata/src/rmeta/encoder.rs | 28 ++++++++++---------- compiler/rustc_serialize/src/opaque.rs | 4 +++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 2d038fba17a..3bd7b6115ae 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -40,7 +40,7 @@ use rustc_span::{ use rustc_target::abi::VariantIdx; use std::borrow::Borrow; use std::hash::Hash; -use std::io::{Seek, Write}; +use std::io::{Read, Seek, Write}; use std::iter; use std::num::NonZeroUsize; use std::path::{Path, PathBuf}; @@ -734,12 +734,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { assert_eq!(total_bytes, computed_total_bytes); if tcx.sess.meta_stats() { - // let mut zero_bytes = 0; - // for e in self.opaque.data.iter() { - // if *e == 0 { - // zero_bytes += 1; - // } - // } + let mut zero_bytes = 0; + let file = std::io::BufReader::new(self.opaque.file()); + for e in file.bytes() { + if e.unwrap() == 0 { + zero_bytes += 1; + } + } let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64; let p = |label, bytes| { @@ -747,13 +748,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { }; eprintln!(""); - // FIXME print zero bytes - //eprintln!( - // "{} metadata bytes, of which {} bytes ({:.1}%) are zero", - // total_bytes, - // zero_bytes, - // perc(zero_bytes) - //); + eprintln!( + "{} metadata bytes, of which {} bytes ({:.1}%) are zero", + total_bytes, + zero_bytes, + perc(zero_bytes) + ); p("preamble", preamble_bytes); p("dep", dep_bytes); p("lib feature", lib_feature_bytes); diff --git a/compiler/rustc_serialize/src/opaque.rs b/compiler/rustc_serialize/src/opaque.rs index 366efe9cfa5..5c17ef6ace2 100644 --- a/compiler/rustc_serialize/src/opaque.rs +++ b/compiler/rustc_serialize/src/opaque.rs @@ -297,6 +297,10 @@ impl FileEncoder { } } + pub fn file(&self) -> &File { + &self.file + } + #[inline] fn capacity(&self) -> usize { self.buf.len()