Skip to content

Commit 0c47091

Browse files
committed
Overhaul to_readable_str.
It's a function that prints numbers with underscores inserted for readability (e.g. "1_234_567"), used by `-Zmeta-stats` and `-Zinput-stats`. It's the only thing in `rustc_middle::util::common`, which is a bizarre location for it. This commit: - moves it to `rustc_data_structures`, a more logical crate for it; - puts it in a module `thousands`, like the similar crates.io crate; - renames it `format_with_underscores`, which is a clearer name; - rewrites it to be more concise; - slightly improves the testing.
1 parent 4ced93e commit 0c47091

File tree

8 files changed

+42
-48
lines changed

8 files changed

+42
-48
lines changed

compiler/rustc_data_structures/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub mod sync;
7676
pub mod tagged_ptr;
7777
pub mod temp_dir;
7878
pub mod thinvec;
79+
pub mod thousands;
7980
pub mod transitive_relation;
8081
pub mod unhash;
8182
pub mod unord;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! This is an extremely bare-bones alternative to the `thousands` crate on
2+
//! crates.io, for printing large numbers in a readable fashion.
3+
4+
#[cfg(test)]
5+
mod tests;
6+
7+
// Converts the number to a string, with underscores as the thousands separator.
8+
pub fn format_with_underscores(n: usize) -> String {
9+
let mut s = n.to_string();
10+
let mut i = s.len();
11+
while i > 3 {
12+
i -= 3;
13+
s.insert(i, '_');
14+
}
15+
s
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use super::*;
2+
3+
#[test]
4+
fn test_format_with_underscores() {
5+
assert_eq!("0", format_with_underscores(0));
6+
assert_eq!("1", format_with_underscores(1));
7+
assert_eq!("99", format_with_underscores(99));
8+
assert_eq!("345", format_with_underscores(345));
9+
assert_eq!("1_000", format_with_underscores(1_000));
10+
assert_eq!("12_001", format_with_underscores(12_001));
11+
assert_eq!("999_999", format_with_underscores(999_999));
12+
assert_eq!("1_000_000", format_with_underscores(1_000_000));
13+
assert_eq!("12_345_678", format_with_underscores(12_345_678));
14+
}

compiler/rustc_metadata/src/rmeta/encoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
99
use rustc_data_structures::memmap::{Mmap, MmapMut};
1010
use rustc_data_structures::sync::{Lrc, join, par_for_each_in};
1111
use rustc_data_structures::temp_dir::MaybeTempDir;
12+
use rustc_data_structures::thousands::format_with_underscores;
1213
use rustc_feature::Features;
1314
use rustc_hir as hir;
1415
use rustc_hir::def_id::{CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, LocalDefId, LocalDefIdSet};
@@ -22,7 +23,6 @@ use rustc_middle::traits::specialization_graph;
2223
use rustc_middle::ty::codec::TyEncoder;
2324
use rustc_middle::ty::fast_reject::{self, TreatParams};
2425
use rustc_middle::ty::{AssocItemContainer, SymbolName};
25-
use rustc_middle::util::common::to_readable_str;
2626
use rustc_middle::{bug, span_bug};
2727
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder, opaque};
2828
use rustc_session::config::{CrateType, OptLevel};
@@ -782,7 +782,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
782782
"{} {:<23}{:>10} ({:4.1}%)",
783783
prefix,
784784
label,
785-
to_readable_str(size),
785+
format_with_underscores(size),
786786
perc(size)
787787
);
788788
}
@@ -791,7 +791,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
791791
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
792792
prefix,
793793
"Total",
794-
to_readable_str(total_bytes),
794+
format_with_underscores(total_bytes),
795795
perc(zero_bytes)
796796
);
797797
eprintln!("{prefix}");

compiler/rustc_middle/src/util/common.rs

-22
This file was deleted.

compiler/rustc_middle/src/util/common/tests.rs

-14
This file was deleted.

compiler/rustc_middle/src/util/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
pub mod bug;
2-
pub mod common;
32

43
#[derive(Default, Copy, Clone)]
54
pub struct Providers {

compiler/rustc_passes/src/input_stats.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
use rustc_ast::visit::BoundKind;
66
use rustc_ast::{self as ast, NodeId, visit as ast_visit};
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8+
use rustc_data_structures::thousands::format_with_underscores;
89
use rustc_hir::{self as hir, AmbigArg, HirId, intravisit as hir_visit};
910
use rustc_middle::hir::map::Map;
1011
use rustc_middle::ty::TyCtxt;
11-
use rustc_middle::util::common::to_readable_str;
1212
use rustc_span::Span;
1313
use rustc_span::def_id::LocalDefId;
1414

@@ -144,10 +144,10 @@ impl<'k> StatCollector<'k> {
144144
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
145145
prefix,
146146
label,
147-
to_readable_str(size),
147+
format_with_underscores(size),
148148
percent(size, total_size),
149-
to_readable_str(node.stats.count),
150-
to_readable_str(node.stats.size)
149+
format_with_underscores(node.stats.count),
150+
format_with_underscores(node.stats.size)
151151
);
152152
if !node.subnodes.is_empty() {
153153
// We will soon sort, so the initial order does not matter.
@@ -163,9 +163,9 @@ impl<'k> StatCollector<'k> {
163163
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
164164
prefix,
165165
label,
166-
to_readable_str(size),
166+
format_with_underscores(size),
167167
percent(size, total_size),
168-
to_readable_str(subnode.count),
168+
format_with_underscores(subnode.count),
169169
);
170170
}
171171
}
@@ -175,8 +175,8 @@ impl<'k> StatCollector<'k> {
175175
"{} {:<18}{:>10} {:>14}",
176176
prefix,
177177
"Total",
178-
to_readable_str(total_size),
179-
to_readable_str(total_count),
178+
format_with_underscores(total_size),
179+
format_with_underscores(total_count),
180180
);
181181
eprintln!("{prefix}");
182182
}

0 commit comments

Comments
 (0)