Skip to content

Commit 296ca4c

Browse files
committed
Reuse buffer for read_to_end.
1 parent 3c25ddf commit 296ca4c

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

compiler/rustc_incremental/src/persist/file_format.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
1212
use std::env;
1313
use std::fs;
14-
use std::io::{self, Read};
14+
use std::io;
1515
use std::path::Path;
1616

17-
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
17+
use rustc_serialize::opaque::{FileDecoder, FileEncodeResult, FileEncoder};
1818
use rustc_serialize::Encoder;
1919

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

64-
let mut file = io::BufReader::new(file);
64+
let mut file = FileDecoder::new(file);
6565

6666
// Check FILE_MAGIC
6767
{

compiler/rustc_incremental/src/persist/load.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use rustc_middle::ty::query::OnDiskCache;
77
use rustc_serialize::opaque::FileDecoder;
88
use rustc_serialize::Decodable as RustcDecodable;
99
use rustc_session::Session;
10-
use std::fs;
11-
use std::io::{self, Read, Seek};
1210
use std::path::Path;
1311

1412
use super::data::*;
@@ -51,7 +49,7 @@ fn load_data(
5149
report_incremental_info: bool,
5250
path: &Path,
5351
nightly_build: bool,
54-
) -> LoadResult<io::BufReader<fs::File>> {
52+
) -> LoadResult<FileDecoder> {
5553
match file_format::read_file(report_incremental_info, path, nightly_build) {
5654
Ok(Some(file)) => LoadResult::Ok { data: file },
5755
Ok(None) => {
@@ -118,9 +116,8 @@ pub fn load_dep_graph(sess: &Session) -> DepGraphFuture {
118116
let work_products_path = work_products_path(sess);
119117
let load_result = load_data(report_incremental_info, &work_products_path, nightly_build);
120118

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

@@ -213,11 +209,8 @@ pub fn load_query_result_cache<'a>(
213209
&query_cache_path(sess),
214210
sess.is_nightly_build(),
215211
) {
216-
LoadResult::Ok { data: mut file } => {
217-
let start_pos = file.seek(io::SeekFrom::Current(0)).unwrap() as usize;
218-
file.seek(io::SeekFrom::Start(0)).unwrap();
219-
let mut bytes = Vec::new();
220-
file.read_to_end(&mut bytes).unwrap();
212+
LoadResult::Ok { data: file } => {
213+
let (bytes, start_pos) = file.read_all().unwrap();
221214
Some(OnDiskCache::new(sess, bytes, start_pos, definitions))
222215
}
223216
_ => Some(OnDiskCache::new_empty(sess.source_map())),

compiler/rustc_serialize/src/opaque.rs

+20-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::serialize::{self, Decoder as _, Encoder as _};
33
use std::borrow::Cow;
44
use std::convert::TryInto;
55
use std::fs::File;
6-
use std::io::{self, BufReader, Read, Seek, SeekFrom, Write};
6+
use std::io::{self, Read, Seek, SeekFrom, Write};
77
use std::mem::MaybeUninit;
88
use std::path::Path;
99
use std::ptr;
@@ -683,22 +683,18 @@ impl<'a> serialize::Decoder for Decoder<'a> {
683683

684684
pub struct FileDecoder {
685685
file: File,
686-
buf: Box<[u8]>,
686+
buf: Vec<u8>,
687687
pos: usize,
688688
cap: usize,
689689
}
690690

691691
impl FileDecoder {
692692
#[inline]
693-
pub fn new(file: BufReader<File>) -> Self {
693+
pub fn new(file: File) -> Self {
694694
const CAP: usize = 8 * 1024;
695695
let mut buf = Vec::with_capacity(CAP);
696696
buf.resize(CAP, 0u8);
697-
let old_buf = file.buffer();
698-
let len = old_buf.len();
699-
buf[..len].copy_from_slice(old_buf);
700-
let file = file.into_inner();
701-
FileDecoder { file, buf: buf.into(), pos: 0, cap: len }
697+
FileDecoder { file, buf, pos: 0, cap: 0 }
702698
}
703699

704700
#[inline]
@@ -708,14 +704,21 @@ impl FileDecoder {
708704
}
709705

710706
#[inline]
711-
pub fn read_all(self) -> Result<(Box<[u8]>, usize), io::Error> {
712-
let mut file = self.file;
713-
let start_pos = file.seek(SeekFrom::Current(0))?;
714-
let start_pos = start_pos.try_into().unwrap();
715-
file.seek(SeekFrom::Start(0))?;
716-
let mut bytes = Vec::new();
717-
file.read_to_end(&mut bytes)?;
718-
Ok((bytes.into(), start_pos))
707+
pub fn read_all(self) -> Result<(Vec<u8>, usize), io::Error> {
708+
let FileDecoder { mut file, mut buf, cap, pos } = self;
709+
let file_pos = file.seek(SeekFrom::Current(0))?;
710+
let file_pos: usize = file_pos.try_into().unwrap();
711+
if file_pos == cap {
712+
// We still have the beginning of the file on-buffer.
713+
// Avoid dropping it and re-reading it.
714+
buf.resize(cap, 0u8);
715+
file.read_to_end(&mut buf)?;
716+
} else {
717+
file.seek(SeekFrom::Start(0))?;
718+
buf.clear();
719+
file.read_to_end(&mut buf)?;
720+
}
721+
Ok((buf, file_pos - cap + pos))
719722
}
720723

721724
#[inline]
@@ -732,7 +735,7 @@ impl FileDecoder {
732735
}
733736
}
734737

735-
fn read_exact(&mut self, mut out: &mut [u8]) -> Result<(), io::Error> {
738+
pub fn read_exact(&mut self, mut out: &mut [u8]) -> Result<(), io::Error> {
736739
loop {
737740
let len = out.len();
738741
if len == 0 {

0 commit comments

Comments
 (0)