Skip to content

Commit afeb43a

Browse files
committed
Inline fast paths in formatting.
1 parent abb8906 commit afeb43a

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

library/core/src/fmt/mod.rs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ pub trait Write {
166166
/// assert_eq!(&buf, "ab");
167167
/// ```
168168
#[stable(feature = "fmt_write_char", since = "1.1.0")]
169+
#[inline]
169170
fn write_char(&mut self, c: char) -> Result {
170171
self.write_str(c.encode_utf8(&mut [0; 4]))
171172
}
@@ -189,21 +190,25 @@ pub trait Write {
189190
/// assert_eq!(&buf, "world");
190191
/// ```
191192
#[stable(feature = "rust1", since = "1.0.0")]
193+
#[inline]
192194
fn write_fmt(mut self: &mut Self, args: Arguments<'_>) -> Result {
193195
write(&mut self, args)
194196
}
195197
}
196198

197199
#[stable(feature = "fmt_write_blanket_impl", since = "1.4.0")]
198200
impl<W: Write + ?Sized> Write for &mut W {
201+
#[inline]
199202
fn write_str(&mut self, s: &str) -> Result {
200203
(**self).write_str(s)
201204
}
202205

206+
#[inline]
203207
fn write_char(&mut self, c: char) -> Result {
204208
(**self).write_char(c)
205209
}
206210

211+
#[inline]
207212
fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
208213
(**self).write_fmt(args)
209214
}
@@ -240,6 +245,7 @@ impl<'a> Formatter<'a> {
240245
/// Currently not intended for use outside of the standard library.
241246
#[unstable(feature = "fmt_internals", reason = "internal to standard library", issue = "none")]
242247
#[doc(hidden)]
248+
#[inline]
243249
pub fn new(buf: &'a mut (dyn Write + 'a)) -> Formatter<'a> {
244250
Formatter {
245251
flags: 0,
@@ -1277,6 +1283,7 @@ pub trait UpperExp {
12771283
/// [`write!`]: crate::write!
12781284
#[stable(feature = "rust1", since = "1.0.0")]
12791285
#[cfg(not(bootstrap))]
1286+
#[inline]
12801287
pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result {
12811288
match args.inner {
12821289
Inner::Fn(f) => f(&mut Formatter::new(output)),
@@ -1549,11 +1556,17 @@ impl<'a> Formatter<'a> {
15491556
/// assert_eq!(&format!("{Foo:0>4}"), "0Foo");
15501557
/// ```
15511558
#[stable(feature = "rust1", since = "1.0.0")]
1559+
#[inline]
15521560
pub fn pad(&mut self, s: &str) -> Result {
15531561
// Make sure there's a fast path up front
15541562
if self.width.is_none() && self.precision.is_none() {
1555-
return self.buf.write_str(s);
1563+
self.buf.write_str(s)
1564+
} else {
1565+
self.pad_slow(s)
15561566
}
1567+
}
1568+
1569+
fn pad_slow(&mut self, s: &str) -> Result {
15571570
// The `precision` field can be interpreted as a `max-width` for the
15581571
// string being formatted.
15591572
let s = if let Some(max) = self.precision {
@@ -1733,6 +1746,7 @@ impl<'a> Formatter<'a> {
17331746
/// assert_eq!(&format!("{Foo:0>8}"), "Foo");
17341747
/// ```
17351748
#[stable(feature = "rust1", since = "1.0.0")]
1749+
#[inline]
17361750
pub fn write_str(&mut self, data: &str) -> Result {
17371751
self.buf.write_str(data)
17381752
}
@@ -1756,6 +1770,7 @@ impl<'a> Formatter<'a> {
17561770
/// assert_eq!(&format!("{:0>8}", Foo(2)), "Foo 2");
17571771
/// ```
17581772
#[stable(feature = "rust1", since = "1.0.0")]
1773+
#[inline]
17591774
pub fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result {
17601775
write(self.buf, fmt)
17611776
}
@@ -1768,6 +1783,7 @@ impl<'a> Formatter<'a> {
17681783
note = "use the `sign_plus`, `sign_minus`, `alternate`, \
17691784
or `sign_aware_zero_pad` methods instead"
17701785
)]
1786+
#[inline]
17711787
pub fn flags(&self) -> u32 {
17721788
self.flags
17731789
}
@@ -1801,6 +1817,7 @@ impl<'a> Formatter<'a> {
18011817
/// ```
18021818
#[must_use]
18031819
#[stable(feature = "fmt_flags", since = "1.5.0")]
1820+
#[inline]
18041821
pub fn fill(&self) -> char {
18051822
self.fill
18061823
}
@@ -1838,6 +1855,7 @@ impl<'a> Formatter<'a> {
18381855
/// ```
18391856
#[must_use]
18401857
#[stable(feature = "fmt_flags_align", since = "1.28.0")]
1858+
#[inline]
18411859
pub fn align(&self) -> Option<Alignment> {
18421860
match self.align {
18431861
rt::v1::Alignment::Left => Some(Alignment::Left),
@@ -1873,6 +1891,7 @@ impl<'a> Formatter<'a> {
18731891
/// ```
18741892
#[must_use]
18751893
#[stable(feature = "fmt_flags", since = "1.5.0")]
1894+
#[inline]
18761895
pub fn width(&self) -> Option<usize> {
18771896
self.width
18781897
}
@@ -1904,6 +1923,7 @@ impl<'a> Formatter<'a> {
19041923
/// ```
19051924
#[must_use]
19061925
#[stable(feature = "fmt_flags", since = "1.5.0")]
1926+
#[inline]
19071927
pub fn precision(&self) -> Option<usize> {
19081928
self.precision
19091929
}
@@ -1936,6 +1956,7 @@ impl<'a> Formatter<'a> {
19361956
/// ```
19371957
#[must_use]
19381958
#[stable(feature = "fmt_flags", since = "1.5.0")]
1959+
#[inline]
19391960
pub fn sign_plus(&self) -> bool {
19401961
self.flags & (1 << FlagV1::SignPlus as u32) != 0
19411962
}
@@ -1965,6 +1986,7 @@ impl<'a> Formatter<'a> {
19651986
/// ```
19661987
#[must_use]
19671988
#[stable(feature = "fmt_flags", since = "1.5.0")]
1989+
#[inline]
19681990
pub fn sign_minus(&self) -> bool {
19691991
self.flags & (1 << FlagV1::SignMinus as u32) != 0
19701992
}
@@ -1993,6 +2015,7 @@ impl<'a> Formatter<'a> {
19932015
/// ```
19942016
#[must_use]
19952017
#[stable(feature = "fmt_flags", since = "1.5.0")]
2018+
#[inline]
19962019
pub fn alternate(&self) -> bool {
19972020
self.flags & (1 << FlagV1::Alternate as u32) != 0
19982021
}
@@ -2019,6 +2042,7 @@ impl<'a> Formatter<'a> {
20192042
/// ```
20202043
#[must_use]
20212044
#[stable(feature = "fmt_flags", since = "1.5.0")]
2045+
#[inline]
20222046
pub fn sign_aware_zero_pad(&self) -> bool {
20232047
self.flags & (1 << FlagV1::SignAwareZeroPad as u32) != 0
20242048
}
@@ -2440,14 +2464,17 @@ impl<'a> Formatter<'a> {
24402464

24412465
#[stable(since = "1.2.0", feature = "formatter_write")]
24422466
impl Write for Formatter<'_> {
2467+
#[inline]
24432468
fn write_str(&mut self, s: &str) -> Result {
24442469
self.buf.write_str(s)
24452470
}
24462471

2472+
#[inline]
24472473
fn write_char(&mut self, c: char) -> Result {
24482474
self.buf.write_char(c)
24492475
}
24502476

2477+
#[inline]
24512478
fn write_fmt(&mut self, args: Arguments<'_>) -> Result {
24522479
write(self.buf, args)
24532480
}
@@ -2535,6 +2562,7 @@ impl Debug for str {
25352562

25362563
#[stable(feature = "rust1", since = "1.0.0")]
25372564
impl Display for str {
2565+
#[inline]
25382566
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
25392567
f.pad(self)
25402568
}

library/std/src/io/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,6 +1655,7 @@ pub trait Write {
16551655
/// }
16561656
/// ```
16571657
#[stable(feature = "rust1", since = "1.0.0")]
1658+
#[inline]
16581659
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<()> {
16591660
// Create a shim which translates a Write to a fmt::Write and saves
16601661
// off I/O errors. instead of discarding them

0 commit comments

Comments
 (0)