Skip to content

Commit 0d5573e

Browse files
committed
Auto merge of #105363 - WaffleLapkin:thin2win_box_next_argument, r=nnethercote
Shrink `rustc_parse_format::Piece` This makes both variants closer together in size (previously they were different by 208 bytes -- 16 vs 224). This may make things worse, but it's worth a try. r? `@nnethercote`
2 parents dfe3fe7 + c44c82d commit 0d5573e

File tree

5 files changed

+50
-44
lines changed

5 files changed

+50
-44
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4168,6 +4168,7 @@ dependencies = [
41684168
name = "rustc_parse_format"
41694169
version = "0.0.0"
41704170
dependencies = [
4171+
"rustc_data_structures",
41714172
"rustc_lexer",
41724173
]
41734174

compiler/rustc_builtin_macros/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ pub fn make_format_args(
333333
parse::Piece::String(s) => {
334334
unfinished_literal.push_str(s);
335335
}
336-
parse::Piece::NextArgument(parse::Argument { position, position_span, format }) => {
336+
parse::Piece::NextArgument(box parse::Argument { position, position_span, format }) => {
337337
if !unfinished_literal.is_empty() {
338338
template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
339339
unfinished_literal.clear();

compiler/rustc_parse_format/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ edition = "2021"
55

66
[dependencies]
77
rustc_lexer = { path = "../rustc_lexer" }
8+
rustc_data_structures = { path = "../rustc_data_structures" }

compiler/rustc_parse_format/src/lib.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ impl InnerOffset {
5858

5959
/// A piece is a portion of the format string which represents the next part
6060
/// to emit. These are emitted as a stream by the `Parser` class.
61-
#[derive(Copy, Clone, Debug, PartialEq)]
61+
#[derive(Clone, Debug, PartialEq)]
6262
pub enum Piece<'a> {
6363
/// A literal string which should directly be emitted
6464
String(&'a str),
6565
/// This describes that formatting should process the next argument (as
6666
/// specified inside) for emission.
67-
NextArgument(Argument<'a>),
67+
NextArgument(Box<Argument<'a>>),
6868
}
6969

7070
/// Representation of an argument specification.
@@ -244,7 +244,7 @@ impl<'a> Iterator for Parser<'a> {
244244
} else {
245245
self.suggest_positional_arg_instead_of_captured_arg(arg);
246246
}
247-
Some(NextArgument(arg))
247+
Some(NextArgument(Box::new(arg)))
248248
}
249249
}
250250
'}' => {
@@ -908,5 +908,9 @@ fn find_skips_from_snippet(
908908
(skips, true)
909909
}
910910

911+
// Assert a reasonable size for `Piece`
912+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
913+
rustc_data_structures::static_assert_size!(Piece<'_>, 16);
914+
911915
#[cfg(test)]
912916
mod tests;

compiler/rustc_parse_format/src/tests.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -76,51 +76,51 @@ fn invalid_precision() {
7676
fn format_nothing() {
7777
same(
7878
"{}",
79-
&[NextArgument(Argument {
79+
&[NextArgument(Box::new(Argument {
8080
position: ArgumentImplicitlyIs(0),
8181
position_span: InnerSpan { start: 2, end: 2 },
8282
format: fmtdflt(),
83-
})],
83+
}))],
8484
);
8585
}
8686
#[test]
8787
fn format_position() {
8888
same(
8989
"{3}",
90-
&[NextArgument(Argument {
90+
&[NextArgument(Box::new(Argument {
9191
position: ArgumentIs(3),
9292
position_span: InnerSpan { start: 2, end: 3 },
9393
format: fmtdflt(),
94-
})],
94+
}))],
9595
);
9696
}
9797
#[test]
9898
fn format_position_nothing_else() {
9999
same(
100100
"{3:}",
101-
&[NextArgument(Argument {
101+
&[NextArgument(Box::new(Argument {
102102
position: ArgumentIs(3),
103103
position_span: InnerSpan { start: 2, end: 3 },
104104
format: fmtdflt(),
105-
})],
105+
}))],
106106
);
107107
}
108108
#[test]
109109
fn format_named() {
110110
same(
111111
"{name}",
112-
&[NextArgument(Argument {
112+
&[NextArgument(Box::new(Argument {
113113
position: ArgumentNamed("name"),
114114
position_span: InnerSpan { start: 2, end: 6 },
115115
format: fmtdflt(),
116-
})],
116+
}))],
117117
)
118118
}
119119
#[test]
120120
fn format_type() {
121121
same(
122122
"{3:x}",
123-
&[NextArgument(Argument {
123+
&[NextArgument(Box::new(Argument {
124124
position: ArgumentIs(3),
125125
position_span: InnerSpan { start: 2, end: 3 },
126126
format: FormatSpec {
@@ -134,14 +134,14 @@ fn format_type() {
134134
ty: "x",
135135
ty_span: None,
136136
},
137-
})],
137+
}))],
138138
);
139139
}
140140
#[test]
141141
fn format_align_fill() {
142142
same(
143143
"{3:>}",
144-
&[NextArgument(Argument {
144+
&[NextArgument(Box::new(Argument {
145145
position: ArgumentIs(3),
146146
position_span: InnerSpan { start: 2, end: 3 },
147147
format: FormatSpec {
@@ -155,11 +155,11 @@ fn format_align_fill() {
155155
ty: "",
156156
ty_span: None,
157157
},
158-
})],
158+
}))],
159159
);
160160
same(
161161
"{3:0<}",
162-
&[NextArgument(Argument {
162+
&[NextArgument(Box::new(Argument {
163163
position: ArgumentIs(3),
164164
position_span: InnerSpan { start: 2, end: 3 },
165165
format: FormatSpec {
@@ -173,11 +173,11 @@ fn format_align_fill() {
173173
ty: "",
174174
ty_span: None,
175175
},
176-
})],
176+
}))],
177177
);
178178
same(
179179
"{3:*<abcd}",
180-
&[NextArgument(Argument {
180+
&[NextArgument(Box::new(Argument {
181181
position: ArgumentIs(3),
182182
position_span: InnerSpan { start: 2, end: 3 },
183183
format: FormatSpec {
@@ -191,14 +191,14 @@ fn format_align_fill() {
191191
ty: "abcd",
192192
ty_span: Some(InnerSpan::new(6, 10)),
193193
},
194-
})],
194+
}))],
195195
);
196196
}
197197
#[test]
198198
fn format_counts() {
199199
same(
200200
"{:10x}",
201-
&[NextArgument(Argument {
201+
&[NextArgument(Box::new(Argument {
202202
position: ArgumentImplicitlyIs(0),
203203
position_span: InnerSpan { start: 2, end: 2 },
204204
format: FormatSpec {
@@ -212,11 +212,11 @@ fn format_counts() {
212212
ty: "x",
213213
ty_span: None,
214214
},
215-
})],
215+
}))],
216216
);
217217
same(
218218
"{:10$.10x}",
219-
&[NextArgument(Argument {
219+
&[NextArgument(Box::new(Argument {
220220
position: ArgumentImplicitlyIs(0),
221221
position_span: InnerSpan { start: 2, end: 2 },
222222
format: FormatSpec {
@@ -230,11 +230,11 @@ fn format_counts() {
230230
ty: "x",
231231
ty_span: None,
232232
},
233-
})],
233+
}))],
234234
);
235235
same(
236236
"{1:0$.10x}",
237-
&[NextArgument(Argument {
237+
&[NextArgument(Box::new(Argument {
238238
position: ArgumentIs(1),
239239
position_span: InnerSpan { start: 2, end: 3 },
240240
format: FormatSpec {
@@ -248,11 +248,11 @@ fn format_counts() {
248248
ty: "x",
249249
ty_span: None,
250250
},
251-
})],
251+
}))],
252252
);
253253
same(
254254
"{:.*x}",
255-
&[NextArgument(Argument {
255+
&[NextArgument(Box::new(Argument {
256256
position: ArgumentImplicitlyIs(1),
257257
position_span: InnerSpan { start: 2, end: 2 },
258258
format: FormatSpec {
@@ -266,11 +266,11 @@ fn format_counts() {
266266
ty: "x",
267267
ty_span: None,
268268
},
269-
})],
269+
}))],
270270
);
271271
same(
272272
"{:.10$x}",
273-
&[NextArgument(Argument {
273+
&[NextArgument(Box::new(Argument {
274274
position: ArgumentImplicitlyIs(0),
275275
position_span: InnerSpan { start: 2, end: 2 },
276276
format: FormatSpec {
@@ -284,11 +284,11 @@ fn format_counts() {
284284
ty: "x",
285285
ty_span: None,
286286
},
287-
})],
287+
}))],
288288
);
289289
same(
290290
"{:a$.b$?}",
291-
&[NextArgument(Argument {
291+
&[NextArgument(Box::new(Argument {
292292
position: ArgumentImplicitlyIs(0),
293293
position_span: InnerSpan { start: 2, end: 2 },
294294
format: FormatSpec {
@@ -302,11 +302,11 @@ fn format_counts() {
302302
ty: "?",
303303
ty_span: None,
304304
},
305-
})],
305+
}))],
306306
);
307307
same(
308308
"{:.4}",
309-
&[NextArgument(Argument {
309+
&[NextArgument(Box::new(Argument {
310310
position: ArgumentImplicitlyIs(0),
311311
position_span: InnerSpan { start: 2, end: 2 },
312312
format: FormatSpec {
@@ -320,14 +320,14 @@ fn format_counts() {
320320
ty: "",
321321
ty_span: None,
322322
},
323-
})],
323+
}))],
324324
)
325325
}
326326
#[test]
327327
fn format_flags() {
328328
same(
329329
"{:-}",
330-
&[NextArgument(Argument {
330+
&[NextArgument(Box::new(Argument {
331331
position: ArgumentImplicitlyIs(0),
332332
position_span: InnerSpan { start: 2, end: 2 },
333333
format: FormatSpec {
@@ -341,11 +341,11 @@ fn format_flags() {
341341
ty: "",
342342
ty_span: None,
343343
},
344-
})],
344+
}))],
345345
);
346346
same(
347347
"{:+#}",
348-
&[NextArgument(Argument {
348+
&[NextArgument(Box::new(Argument {
349349
position: ArgumentImplicitlyIs(0),
350350
position_span: InnerSpan { start: 2, end: 2 },
351351
format: FormatSpec {
@@ -359,7 +359,7 @@ fn format_flags() {
359359
ty: "",
360360
ty_span: None,
361361
},
362-
})],
362+
}))],
363363
);
364364
}
365365
#[test]
@@ -368,7 +368,7 @@ fn format_mixture() {
368368
"abcd {3:x} efg",
369369
&[
370370
String("abcd "),
371-
NextArgument(Argument {
371+
NextArgument(Box::new(Argument {
372372
position: ArgumentIs(3),
373373
position_span: InnerSpan { start: 7, end: 8 },
374374
format: FormatSpec {
@@ -382,7 +382,7 @@ fn format_mixture() {
382382
ty: "x",
383383
ty_span: None,
384384
},
385-
}),
385+
})),
386386
String(" efg"),
387387
],
388388
);
@@ -391,18 +391,18 @@ fn format_mixture() {
391391
fn format_whitespace() {
392392
same(
393393
"{ }",
394-
&[NextArgument(Argument {
394+
&[NextArgument(Box::new(Argument {
395395
position: ArgumentImplicitlyIs(0),
396396
position_span: InnerSpan { start: 2, end: 3 },
397397
format: fmtdflt(),
398-
})],
398+
}))],
399399
);
400400
same(
401401
"{ }",
402-
&[NextArgument(Argument {
402+
&[NextArgument(Box::new(Argument {
403403
position: ArgumentImplicitlyIs(0),
404404
position_span: InnerSpan { start: 2, end: 4 },
405405
format: fmtdflt(),
406-
})],
406+
}))],
407407
);
408408
}

0 commit comments

Comments
 (0)