Skip to content

Commit

Permalink
Reuse buffer for read_to_end.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 13, 2021
1 parent 3c25ddf commit 296ca4c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_incremental/src/persist/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use std::env;
use std::fs;
use std::io::{self, Read};
use std::io;
use std::path::Path;

use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_serialize::opaque::{FileDecoder, FileEncodeResult, FileEncoder};
use rustc_serialize::Encoder;

/// The first few bytes of files generated by incremental compilation.
Expand Down Expand Up @@ -54,14 +54,14 @@ pub fn read_file(
report_incremental_info: bool,
path: &Path,
nightly_build: bool,
) -> io::Result<Option<io::BufReader<fs::File>>> {
) -> io::Result<Option<FileDecoder>> {
let file = match fs::File::open(path) {
Ok(file) => file,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(None),
Err(err) => return Err(err),
};

let mut file = io::BufReader::new(file);
let mut file = FileDecoder::new(file);

// Check FILE_MAGIC
{
Expand Down
17 changes: 5 additions & 12 deletions compiler/rustc_incremental/src/persist/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ use rustc_middle::ty::query::OnDiskCache;
use rustc_serialize::opaque::FileDecoder;
use rustc_serialize::Decodable as RustcDecodable;
use rustc_session::Session;
use std::fs;
use std::io::{self, Read, Seek};
use std::path::Path;

use super::data::*;
Expand Down Expand Up @@ -51,7 +49,7 @@ fn load_data(
report_incremental_info: bool,
path: &Path,
nightly_build: bool,
) -> LoadResult<io::BufReader<fs::File>> {
) -> LoadResult<FileDecoder> {
match file_format::read_file(report_incremental_info, path, nightly_build) {
Ok(Some(file)) => LoadResult::Ok { data: file },
Ok(None) => {
Expand Down Expand Up @@ -118,9 +116,8 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
let work_products_path = work_products_path(sess);
let load_result = load_data(report_incremental_info, &work_products_path, nightly_build);

if let LoadResult::Ok { data: file } = load_result {
if let LoadResult::Ok { data: mut work_product_decoder } = load_result {
// Decode the list of work_products
let mut work_product_decoder = FileDecoder::new(file);
let work_products: Vec<SerializedWorkProduct> =
RustcDecodable::decode(&mut work_product_decoder).unwrap_or_else(|e| {
let msg = format!(
Expand Down Expand Up @@ -165,8 +162,7 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
match load_data(report_incremental_info, &path, nightly_build) {
LoadResult::DataOutOfDate => LoadResult::DataOutOfDate,
LoadResult::Error { message } => LoadResult::Error { message },
LoadResult::Ok { data: file } => {
let mut decoder = FileDecoder::new(file);
LoadResult::Ok { data: mut decoder } => {
let prev_commandline_args_hash = u64::decode(&mut decoder)
.expect("Error reading commandline arg hash from cached dep-graph");

Expand Down Expand Up @@ -213,11 +209,8 @@ pub fn load_query_result_cache<'a>(
&query_cache_path(sess),
sess.is_nightly_build(),
) {
LoadResult::Ok { data: mut file } => {
let start_pos = file.seek(io::SeekFrom::Current(0)).unwrap() as usize;
file.seek(io::SeekFrom::Start(0)).unwrap();
let mut bytes = Vec::new();
file.read_to_end(&mut bytes).unwrap();
LoadResult::Ok { data: file } => {
let (bytes, start_pos) = file.read_all().unwrap();
Some(OnDiskCache::new(sess, bytes, start_pos, definitions))
}
_ => Some(OnDiskCache::new_empty(sess.source_map())),
Expand Down
37 changes: 20 additions & 17 deletions compiler/rustc_serialize/src/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::serialize::{self, Decoder as _, Encoder as _};
use std::borrow::Cow;
use std::convert::TryInto;
use std::fs::File;
use std::io::{self, BufReader, Read, Seek, SeekFrom, Write};
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::mem::MaybeUninit;
use std::path::Path;
use std::ptr;
Expand Down Expand Up @@ -683,22 +683,18 @@ impl<'a> serialize::Decoder for Decoder<'a> {

pub struct FileDecoder {
file: File,
buf: Box<[u8]>,
buf: Vec<u8>,
pos: usize,
cap: usize,
}

impl FileDecoder {
#[inline]
pub fn new(file: BufReader<File>) -> Self {
pub fn new(file: File) -> Self {
const CAP: usize = 8 * 1024;
let mut buf = Vec::with_capacity(CAP);
buf.resize(CAP, 0u8);
let old_buf = file.buffer();
let len = old_buf.len();
buf[..len].copy_from_slice(old_buf);
let file = file.into_inner();
FileDecoder { file, buf: buf.into(), pos: 0, cap: len }
FileDecoder { file, buf, pos: 0, cap: 0 }
}

#[inline]
Expand All @@ -708,14 +704,21 @@ impl FileDecoder {
}

#[inline]
pub fn read_all(self) -> Result<(Box<[u8]>, usize), io::Error> {
let mut file = self.file;
let start_pos = file.seek(SeekFrom::Current(0))?;
let start_pos = start_pos.try_into().unwrap();
file.seek(SeekFrom::Start(0))?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;
Ok((bytes.into(), start_pos))
pub fn read_all(self) -> Result<(Vec<u8>, usize), io::Error> {
let FileDecoder { mut file, mut buf, cap, pos } = self;
let file_pos = file.seek(SeekFrom::Current(0))?;
let file_pos: usize = file_pos.try_into().unwrap();
if file_pos == cap {
// We still have the beginning of the file on-buffer.
// Avoid dropping it and re-reading it.
buf.resize(cap, 0u8);
file.read_to_end(&mut buf)?;
} else {
file.seek(SeekFrom::Start(0))?;
buf.clear();
file.read_to_end(&mut buf)?;
}
Ok((buf, file_pos - cap + pos))
}

#[inline]
Expand All @@ -732,7 +735,7 @@ impl FileDecoder {
}
}

fn read_exact(&mut self, mut out: &mut [u8]) -> Result<(), io::Error> {
pub fn read_exact(&mut self, mut out: &mut [u8]) -> Result<(), io::Error> {
loop {
let len = out.len();
if len == 0 {
Expand Down

0 comments on commit 296ca4c

Please sign in to comment.