Skip to content

Commit 9a926e5

Browse files
committed
Fix metadata stats.
This commit: - Counts some things that weren't being counted previously, and adds an assertion that ensure everything is counted. - Reorders things so the `eprintln`s order matches the code order. - Adds percentages, and makes clear that the zero bytes count is orthogonal to the other measurements. Example of the new output: ``` 55463779 metadata bytes, of which 18054531 bytes (32.6%) are zero preamble: 30 bytes ( 0.0%) dep: 0 bytes ( 0.0%) lib feature: 17458 bytes ( 0.0%) lang item: 337 bytes ( 0.0%) diagnostic item: 1788 bytes ( 0.0%) native lib: 0 bytes ( 0.0%) foreign modules: 5113 bytes ( 0.0%) def-path table: 720180 bytes ( 1.3%) traits: 359 bytes ( 0.0%) impls: 64624 bytes ( 0.1%) incoherent_impls: 130 bytes ( 0.0%) mir: 16137354 bytes (29.1%) item: 23773099 bytes (42.9%) interpret_alloc_index: 599 bytes ( 0.0%) proc-macro-data: 0 bytes ( 0.0%) tables: 10081135 bytes (18.2%) debugger visualizers: 0 bytes ( 0.0%) exported symbols: 5666 bytes ( 0.0%) hygiene: 1539390 bytes ( 2.8%) def-path hashes: 2752564 bytes ( 5.0%) source_map: 363540 bytes ( 0.7%) final: 413 bytes ( 0.0%) ```
1 parent b2eed72 commit 9a926e5

File tree

1 file changed

+69
-23
lines changed

1 file changed

+69
-23
lines changed

compiler/rustc_metadata/src/rmeta/encoder.rs

+69-23
Original file line numberDiff line numberDiff line change
@@ -539,9 +539,11 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
539539

540540
fn encode_crate_root(&mut self) -> Lazy<CrateRoot<'tcx>> {
541541
let tcx = self.tcx;
542-
let mut i = self.position();
542+
let mut i = 0;
543+
let preamble_bytes = self.position() - i;
543544

544545
// Encode the crate deps
546+
i = self.position();
545547
let crate_deps = self.encode_crate_deps();
546548
let dylib_dependency_formats = self.encode_dylib_dependency_formats();
547549
let dep_bytes = self.position() - i;
@@ -567,7 +569,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
567569
let native_libraries = self.encode_native_libraries();
568570
let native_lib_bytes = self.position() - i;
569571

572+
i = self.position();
570573
let foreign_modules = self.encode_foreign_modules();
574+
let foreign_modules_bytes = self.position() - i;
571575

572576
// Encode DefPathTable
573577
i = self.position();
@@ -587,6 +591,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
587591
i = self.position();
588592
let incoherent_impls = self.encode_incoherent_impls();
589593
let incoherent_impls_bytes = self.position() - i;
594+
590595
// Encode MIR.
591596
i = self.position();
592597
self.encode_mir();
@@ -599,6 +604,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
599604
let item_bytes = self.position() - i;
600605

601606
// Encode the allocation index
607+
i = self.position();
602608
let interpret_alloc_index = {
603609
let mut interpret_alloc_index = Vec::new();
604610
let mut n = 0;
@@ -621,6 +627,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
621627
}
622628
self.lazy(interpret_alloc_index)
623629
};
630+
let interpret_alloc_index_bytes = self.position() - i;
624631

625632
// Encode the proc macro data. This affects 'tables',
626633
// so we need to do this before we encode the tables
@@ -665,9 +672,9 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
665672
let source_map = self.encode_source_map();
666673
let source_map_bytes = self.position() - i;
667674

675+
i = self.position();
668676
let attrs = tcx.hir().krate_attrs();
669677
let has_default_lib_allocator = tcx.sess.contains_name(&attrs, sym::default_lib_allocator);
670-
671678
let root = self.lazy(CrateRoot {
672679
name: tcx.crate_name(LOCAL_CRATE),
673680
extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
@@ -710,9 +717,34 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
710717
expn_hashes,
711718
def_path_hash_map,
712719
});
720+
let final_bytes = self.position() - i;
713721

714722
let total_bytes = self.position();
715723

724+
let computed_total_bytes = preamble_bytes
725+
+ dep_bytes
726+
+ lib_feature_bytes
727+
+ lang_item_bytes
728+
+ diagnostic_item_bytes
729+
+ native_lib_bytes
730+
+ foreign_modules_bytes
731+
+ def_path_table_bytes
732+
+ traits_bytes
733+
+ impls_bytes
734+
+ incoherent_impls_bytes
735+
+ mir_bytes
736+
+ item_bytes
737+
+ interpret_alloc_index_bytes
738+
+ proc_macro_data_bytes
739+
+ tables_bytes
740+
+ debugger_visualizers_bytes
741+
+ exported_symbols_bytes
742+
+ hygiene_bytes
743+
+ def_path_hash_map_bytes
744+
+ source_map_bytes
745+
+ final_bytes;
746+
assert_eq!(total_bytes, computed_total_bytes);
747+
716748
if tcx.sess.meta_stats() {
717749
let mut zero_bytes = 0;
718750
for e in self.opaque.data.iter() {
@@ -721,27 +753,41 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
721753
}
722754
}
723755

724-
eprintln!("metadata stats:");
725-
eprintln!(" dep bytes: {}", dep_bytes);
726-
eprintln!(" lib feature bytes: {}", lib_feature_bytes);
727-
eprintln!(" lang item bytes: {}", lang_item_bytes);
728-
eprintln!(" diagnostic item bytes: {}", diagnostic_item_bytes);
729-
eprintln!(" native bytes: {}", native_lib_bytes);
730-
eprintln!(" debugger visualizers bytes: {}", debugger_visualizers_bytes);
731-
eprintln!(" source_map bytes: {}", source_map_bytes);
732-
eprintln!(" traits bytes: {}", traits_bytes);
733-
eprintln!(" impls bytes: {}", impls_bytes);
734-
eprintln!(" incoherent_impls bytes: {}", incoherent_impls_bytes);
735-
eprintln!(" exp. symbols bytes: {}", exported_symbols_bytes);
736-
eprintln!(" def-path table bytes: {}", def_path_table_bytes);
737-
eprintln!(" def-path hashes bytes: {}", def_path_hash_map_bytes);
738-
eprintln!(" proc-macro-data-bytes: {}", proc_macro_data_bytes);
739-
eprintln!(" mir bytes: {}", mir_bytes);
740-
eprintln!(" item bytes: {}", item_bytes);
741-
eprintln!(" table bytes: {}", tables_bytes);
742-
eprintln!(" hygiene bytes: {}", hygiene_bytes);
743-
eprintln!(" zero bytes: {}", zero_bytes);
744-
eprintln!(" total bytes: {}", total_bytes);
756+
let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;
757+
let p = |label, bytes| {
758+
eprintln!("{:>21}: {:>8} bytes ({:4.1}%)", label, bytes, perc(bytes));
759+
};
760+
761+
eprintln!("");
762+
eprintln!(
763+
"{} metadata bytes, of which {} bytes ({:.1}%) are zero",
764+
total_bytes,
765+
zero_bytes,
766+
perc(zero_bytes)
767+
);
768+
p("preamble", preamble_bytes);
769+
p("dep", dep_bytes);
770+
p("lib feature", lib_feature_bytes);
771+
p("lang item", lang_item_bytes);
772+
p("diagnostic item", diagnostic_item_bytes);
773+
p("native lib", native_lib_bytes);
774+
p("foreign modules", foreign_modules_bytes);
775+
p("def-path table", def_path_table_bytes);
776+
p("traits", traits_bytes);
777+
p("impls", impls_bytes);
778+
p("incoherent_impls", incoherent_impls_bytes);
779+
p("mir", mir_bytes);
780+
p("item", item_bytes);
781+
p("interpret_alloc_index", interpret_alloc_index_bytes);
782+
p("proc-macro-data", proc_macro_data_bytes);
783+
p("tables", tables_bytes);
784+
p("debugger visualizers", debugger_visualizers_bytes);
785+
p("exported symbols", exported_symbols_bytes);
786+
p("hygiene", hygiene_bytes);
787+
p("def-path hashes", def_path_hash_map_bytes);
788+
p("source_map", source_map_bytes);
789+
p("final", final_bytes);
790+
eprintln!("");
745791
}
746792

747793
root

0 commit comments

Comments
 (0)