Skip to content

Commit

Permalink
seek instead of opening a new file handle
Browse files Browse the repository at this point in the history
  • Loading branch information
m-ysk committed Jul 2, 2022
1 parent ea0e0f4 commit a206121
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2254,10 +2254,10 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
let root = ecx.encode_crate_root();

ecx.opaque.flush();
let mut file = std::fs::OpenOptions::new()
.write(true)
.open(path)
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to open the file: {}", err)));

let mut file = ecx.opaque.file();
// We will return to this position after writing the root position.
let pos_before_seek = file.stream_position().unwrap();

// Encode the root position.
let header = METADATA_HEADER.len();
Expand All @@ -2267,6 +2267,9 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) {
file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8])
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to write to the file: {}", err)));

// Return to the position where we are before writing the root position.
file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap();

// Record metadata size for self-profiling
tcx.prof.artifact_size(
"crate_metadata",
Expand Down

0 comments on commit a206121

Please sign in to comment.