construct EncodedMetadata in encode_and_write_metadata

This commit is contained in:
Yoshiki Matsuda 2022-04-28 10:55:04 +09:00
parent bb75c4b46a
commit c26c461c0c
2 changed files with 17 additions and 12 deletions

View File

@ -68,11 +68,9 @@ enum MetadataKind {
.unwrap_or_else(|err| tcx.sess.fatal(&format!("couldn't create a temp dir: {}", err)));
let metadata_tmpdir = MaybeTempDir::new(metadata_tmpdir, tcx.sess.opts.cg.save_temps);
let metadata_filename = metadata_tmpdir.as_ref().join(METADATA_FILENAME);
let metadata = match metadata_kind {
match metadata_kind {
MetadataKind::None => {
let metadata = EncodedMetadata::new();
let _ = emit_metadata(tcx.sess, metadata.raw_data(), &metadata_tmpdir);
metadata
let _ = emit_metadata(tcx.sess, &[], &metadata_tmpdir);
}
MetadataKind::Uncompressed | MetadataKind::Compressed => {
encode_metadata(tcx, &metadata_filename)
@ -82,7 +80,7 @@ enum MetadataKind {
let _prof_timer = tcx.sess.prof.generic_activity("write_crate_metadata");
let need_metadata_file = tcx.sess.opts.output_types.contains_key(&OutputType::Metadata);
if need_metadata_file {
let metadata_filename = if need_metadata_file {
if let Err(e) = non_durable_rename(&metadata_filename, &out_filename) {
tcx.sess.fatal(&format!("failed to write {}: {}", out_filename.display(), e));
}
@ -92,7 +90,12 @@ enum MetadataKind {
.span_diagnostic
.emit_artifact_notification(&out_filename, "metadata");
}
}
out_filename
} else {
metadata_filename
};
let raw_data = std::fs::read(metadata_filename).unwrap();
let metadata = EncodedMetadata::from_raw_data(raw_data);
let need_metadata_module = metadata_kind == MetadataKind::Compressed;

View File

@ -2146,13 +2146,18 @@ pub fn new() -> EncodedMetadata {
EncodedMetadata { raw_data: Vec::new() }
}
#[inline]
pub fn from_raw_data(raw_data: Vec<u8>) -> Self {
Self { raw_data }
}
#[inline]
pub fn raw_data(&self) -> &[u8] {
&self.raw_data
}
}
pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetadata {
pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata");
// Since encoding metadata is not in a query, and nothing is cached,
@ -2170,11 +2175,10 @@ pub fn encode_metadata(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetada
// It can be removed if it turns out to cause trouble or be detrimental to performance.
join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE));
},
)
.0
);
}
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetadata {
fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) {
let mut encoder = opaque::FileEncoder::new(path.as_ref())
.unwrap_or_else(|err| tcx.sess.fatal(&format!("failed to create file encoder: {}", err)));
encoder.emit_raw_bytes(METADATA_HEADER);
@ -2226,8 +2230,6 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>, path: impl AsRef<Path>) -> EncodedMetad
// Record metadata size for self-profiling
tcx.prof.artifact_size("crate_metadata", "crate_metadata", result.len() as u64);
EncodedMetadata { raw_data: result }
}
pub fn provide(providers: &mut Providers) {