Skip to content

Commit c3200c3

Browse files
committed
Tweak -Zmacro-stats measurement.
It currently reports net size, i.e. size(output) - size(input). After some use I think this is sub-optimal, and it's better to just report size(output). Because for derive macros the input size is always 1, and for attribute macros it's almost always 1.
1 parent 111e9bc commit c3200c3

File tree

4 files changed

+35
-43
lines changed

4 files changed

+35
-43
lines changed

compiler/rustc_expand/src/stats.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ pub struct MacroStat {
1515
/// Number of uses of the macro.
1616
pub uses: usize,
1717

18-
/// Net increase in number of lines of code (when pretty-printed), i.e.
19-
/// `lines(output) - lines(invocation)`. Can be negative because a macro
20-
/// output may be smaller than the invocation.
21-
pub lines: isize,
22-
23-
/// Net increase in number of lines of code (when pretty-printed), i.e.
24-
/// `bytes(output) - bytes(invocation)`. Can be negative because a macro
25-
/// output may be smaller than the invocation.
26-
pub bytes: isize,
18+
/// Number of lines of code (when pretty-printed).
19+
pub lines: usize,
20+
21+
/// Number of bytes of code (when pretty-printed).
22+
pub bytes: usize,
2723
}
2824

2925
pub(crate) fn elems_to_string<T>(elems: &SmallVec<[T; 1]>, f: impl Fn(&T) -> String) -> String {
@@ -131,16 +127,12 @@ pub(crate) fn update_macro_stats(
131127
input: &str,
132128
fragment: &AstFragment,
133129
) {
134-
fn lines_and_bytes(s: &str) -> (usize, usize) {
135-
(s.trim_end().split('\n').count(), s.len())
136-
}
137-
138130
// Measure the size of the output by pretty-printing it and counting
139131
// the lines and bytes.
140132
let name = Symbol::intern(&pprust::path_to_string(path));
141133
let output = fragment.to_string();
142-
let (in_l, in_b) = lines_and_bytes(input);
143-
let (out_l, out_b) = lines_and_bytes(&output);
134+
let num_lines = output.trim_end().split('\n').count();
135+
let num_bytes = output.len();
144136

145137
// This code is useful for debugging `-Zmacro-stats`. For every
146138
// invocation it prints the full input and output.
@@ -157,7 +149,7 @@ pub(crate) fn update_macro_stats(
157149
{name}: [{crate_name}] ({fragment_kind:?}) {span}\n\
158150
-------------------------------\n\
159151
{input}\n\
160-
-- ({in_l} lines, {in_b} bytes) --> ({out_l} lines, {out_b} bytes) --\n\
152+
-- {num_lines} lines, {num_bytes} bytes --\n\
161153
{output}\n\
162154
"
163155
);
@@ -166,6 +158,6 @@ pub(crate) fn update_macro_stats(
166158
// The recorded size is the difference between the input and the output.
167159
let entry = ecx.macro_stats.entry((name, macro_kind)).or_insert(MacroStat::default());
168160
entry.uses += 1;
169-
entry.lines += out_l as isize - in_l as isize;
170-
entry.bytes += out_b as isize - in_b as isize;
161+
entry.lines += num_lines;
162+
entry.bytes += num_bytes;
171163
}

compiler/rustc_interface/src/passes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,9 +354,9 @@ fn print_macro_stats(ecx: &ExtCtxt<'_>) {
354354
"{prefix} {:<name_w$}{:>uses_w$}{:>lines_w$}{:>avg_lines_w$}{:>bytes_w$}{:>avg_bytes_w$}",
355355
name,
356356
thousands::usize_with_underscores(uses),
357-
thousands::isize_with_underscores(lines),
357+
thousands::usize_with_underscores(lines),
358358
thousands::f64p1_with_underscores(avg_lines),
359-
thousands::isize_with_underscores(bytes),
359+
thousands::usize_with_underscores(bytes),
360360
thousands::f64p1_with_underscores(avg_bytes),
361361
);
362362
}

src/doc/unstable-book/src/compiler-flags/macro-stats.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ generated code is normally invisible to the programmer.
1010

1111
This flag helps identify such cases. When enabled, the compiler measures the
1212
effect on code size of all used macros and prints a table summarizing that
13-
effect. For each distinct macro, it counts how many times it is used, and the
14-
net effect on code size (in terms of lines of code, and bytes of code). The
13+
effect. For each distinct macro, it counts how many times it is used, and how
14+
much code it produces when expanded (in lines of code, and bytes of code). The
1515
code size evaluation uses the compiler's internal pretty-printing, and so will
1616
be independent of the formatting in the original code.
1717

18-
Note that the net effect of a macro may be negative. E.g. the `cfg!` and
18+
Note that the output size of a macro may be zero. E.g. the `cfg!` and
1919
`#[test]` macros often strip out code.
2020

2121
If a macro is identified as causing a large increase in code size, it is worth

tests/ui/stats/macro-stats.stderr

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ macro-stats ====================================================================
22
macro-stats MACRO EXPANSION STATS: macro_stats
33
macro-stats Macro Name Uses Lines Avg Lines Bytes Avg Bytes
44
macro-stats -----------------------------------------------------------------------------------
5-
macro-stats #[derive(Clone)] 8 56 7.0 1_660 207.5
6-
macro-stats #[derive(PartialOrd)] 1 16 16.0 654 654.0
7-
macro-stats #[derive(Hash)] 2 15 7.5 547 273.5
8-
macro-stats #[derive(Ord)] 1 14 14.0 489 489.0
9-
macro-stats q! 1 24 24.0 435 435.0
10-
macro-stats #[derive(Default)] 2 14 7.0 367 183.5
11-
macro-stats #[derive(Eq)] 1 10 10.0 312 312.0
12-
macro-stats #[derive(Debug)] 1 7 7.0 261 261.0
13-
macro-stats #[derive(PartialEq)] 1 8 8.0 247 247.0
14-
macro-stats #[derive(Copy)] 1 1 1.0 46 46.0
15-
macro-stats p! 1 2 2.0 28 28.0
16-
macro-stats trait_impl_tys! 1 1 1.0 11 11.0
17-
macro-stats foreign_item! 1 0 0.0 6 6.0
18-
macro-stats impl_const! 1 0 0.0 4 4.0
19-
macro-stats trait_tys! 1 1 1.0 3 3.0
20-
macro-stats u32! 1 0 0.0 -3 -3.0
21-
macro-stats none! 1 0 0.0 -3 -3.0
22-
macro-stats n99! 2 0 0.0 -8 -4.0
5+
macro-stats #[derive(Clone)] 8 64 8.0 1_788 223.5
6+
macro-stats #[derive(PartialOrd)] 1 17 17.0 675 675.0
7+
macro-stats #[derive(Hash)] 2 17 8.5 577 288.5
8+
macro-stats q! 1 26 26.0 519 519.0
9+
macro-stats #[derive(Ord)] 1 15 15.0 503 503.0
10+
macro-stats #[derive(Default)] 2 16 8.0 403 201.5
11+
macro-stats #[derive(Eq)] 1 11 11.0 325 325.0
12+
macro-stats #[derive(Debug)] 1 8 8.0 277 277.0
13+
macro-stats #[derive(PartialEq)] 1 9 9.0 267 267.0
14+
macro-stats #[derive(Copy)] 1 2 2.0 61 61.0
15+
macro-stats p! 1 3 3.0 32 32.0
16+
macro-stats trait_impl_tys! 1 2 2.0 28 28.0
17+
macro-stats foreign_item! 1 1 1.0 21 21.0
2318
macro-stats this_is_a_really_really_long_macro_name!
24-
macro-stats 1 0 0.0 -30 -30.0
25-
macro-stats #[test] 1 -6 -6.0 -158 -158.0
19+
macro-stats 1 1 1.0 18 18.0
20+
macro-stats impl_const! 1 1 1.0 17 17.0
21+
macro-stats trait_tys! 1 2 2.0 15 15.0
22+
macro-stats n99! 2 2 1.0 4 2.0
23+
macro-stats none! 1 1 1.0 4 4.0
24+
macro-stats u32! 1 1 1.0 3 3.0
25+
macro-stats #[test] 1 1 1.0 0 0.0
2626
macro-stats ===================================================================================

0 commit comments

Comments
 (0)