Skip to content

Commit 1aff8af

Browse files
Replace pretty-printer Box<dyn Write> with &mut String
1 parent 209bde1 commit 1aff8af

File tree

4 files changed

+47
-60
lines changed

4 files changed

+47
-60
lines changed

src/librustc/hir/print.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::hir::{GenericParam, GenericParamKind, GenericArg};
1717

1818
use std::borrow::Cow;
1919
use std::cell::Cell;
20-
use std::io::{self, Write, Read};
20+
use std::io::{self, Read};
2121
use std::vec;
2222

2323
pub enum AnnNode<'a> {
@@ -101,18 +101,14 @@ impl<'a> PrintState<'a> for State<'a> {
101101
#[allow(non_upper_case_globals)]
102102
pub const indent_unit: usize = 4;
103103

104-
#[allow(non_upper_case_globals)]
105-
pub const default_columns: usize = 78;
106-
107-
108104
/// Requires you to pass an input filename and reader so that
109105
/// it can scan the input text for comments to copy forward.
110106
pub fn print_crate<'a>(cm: &'a SourceMap,
111107
sess: &ParseSess,
112108
krate: &hir::Crate,
113109
filename: FileName,
114110
input: &mut dyn Read,
115-
out: Box<dyn Write + 'a>,
111+
out: &'a mut String,
116112
ann: &'a dyn PpAnn)
117113
-> io::Result<()> {
118114
let mut s = State::new_from_input(cm, sess, filename, input, out, ann);
@@ -130,20 +126,20 @@ impl<'a> State<'a> {
130126
sess: &ParseSess,
131127
filename: FileName,
132128
input: &mut dyn Read,
133-
out: Box<dyn Write + 'a>,
129+
out: &'a mut String,
134130
ann: &'a dyn PpAnn)
135131
-> State<'a> {
136132
let comments = comments::gather_comments(sess, filename, input);
137133
State::new(cm, out, ann, Some(comments))
138134
}
139135

140136
pub fn new(cm: &'a SourceMap,
141-
out: Box<dyn Write + 'a>,
137+
out: &'a mut String,
142138
ann: &'a dyn PpAnn,
143139
comments: Option<Vec<comments::Comment>>)
144140
-> State<'a> {
145141
State {
146-
s: pp::mk_printer(out, default_columns),
142+
s: pp::mk_printer(out),
147143
cm: Some(cm),
148144
comments,
149145
cur_cmnt: 0,
@@ -156,10 +152,10 @@ impl<'a> State<'a> {
156152
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
157153
where F: FnOnce(&mut State<'_>) -> io::Result<()>
158154
{
159-
let mut wr = Vec::new();
155+
let mut wr = String::new();
160156
{
161157
let mut printer = State {
162-
s: pp::mk_printer(Box::new(&mut wr), default_columns),
158+
s: pp::mk_printer(&mut wr),
163159
cm: None,
164160
comments: None,
165161
cur_cmnt: 0,
@@ -169,7 +165,7 @@ pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
169165
f(&mut printer).unwrap();
170166
printer.s.eof().unwrap();
171167
}
172-
String::from_utf8(wr).unwrap()
168+
wr
173169
}
174170

175171
pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility, w: S) -> String {

src/librustc_driver/pretty.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,11 @@ pub fn print_after_parsing(sess: &Session,
728728
let (src, src_name) = get_source(input, sess);
729729

730730
let mut rdr = &*src;
731-
let mut out = Vec::new();
731+
let mut out = String::new();
732732

733733
if let PpmSource(s) = ppm {
734734
// Silently ignores an identified node.
735-
let out: &mut dyn Write = &mut out;
735+
let out = &mut out;
736736
s.call_with_pp_support(sess, None, move |annotation| {
737737
debug!("pretty printing source code {:?}", s);
738738
let sess = annotation.sess();
@@ -741,15 +741,15 @@ pub fn print_after_parsing(sess: &Session,
741741
krate,
742742
src_name,
743743
&mut rdr,
744-
box out,
744+
out,
745745
annotation.pp_ann(),
746746
false)
747747
}).unwrap()
748748
} else {
749749
unreachable!();
750750
};
751751

752-
write_output(out, ofile);
752+
write_output(out.into_bytes(), ofile);
753753
}
754754

755755
pub fn print_after_hir_lowering<'tcx>(
@@ -773,12 +773,12 @@ pub fn print_after_hir_lowering<'tcx>(
773773
let (src, src_name) = get_source(input, tcx.sess);
774774

775775
let mut rdr = &src[..];
776-
let mut out = Vec::new();
776+
let mut out = String::new();
777777

778778
match (ppm, opt_uii) {
779779
(PpmSource(s), _) => {
780780
// Silently ignores an identified node.
781-
let out: &mut dyn Write = &mut out;
781+
let out = &mut out;
782782
s.call_with_pp_support(tcx.sess, Some(tcx), move |annotation| {
783783
debug!("pretty printing source code {:?}", s);
784784
let sess = annotation.sess();
@@ -787,14 +787,14 @@ pub fn print_after_hir_lowering<'tcx>(
787787
krate,
788788
src_name,
789789
&mut rdr,
790-
box out,
790+
out,
791791
annotation.pp_ann(),
792792
true)
793793
})
794794
}
795795

796796
(PpmHir(s), None) => {
797-
let out: &mut dyn Write = &mut out;
797+
let out = &mut out;
798798
s.call_with_pp_support_hir(tcx, move |annotation, krate| {
799799
debug!("pretty printing source code {:?}", s);
800800
let sess = annotation.sess();
@@ -803,21 +803,21 @@ pub fn print_after_hir_lowering<'tcx>(
803803
krate,
804804
src_name,
805805
&mut rdr,
806-
box out,
806+
out,
807807
annotation.pp_ann())
808808
})
809809
}
810810

811811
(PpmHirTree(s), None) => {
812-
let out: &mut dyn Write = &mut out;
812+
let out = &mut out;
813813
s.call_with_pp_support_hir(tcx, move |_annotation, krate| {
814814
debug!("pretty printing source code {:?}", s);
815-
write!(out, "{:#?}", krate)
816-
})
815+
*out = format!("{:#?}", krate);
816+
});
817817
}
818818

819819
(PpmHir(s), Some(uii)) => {
820-
let out: &mut dyn Write = &mut out;
820+
let out = &mut out;
821821
s.call_with_pp_support_hir(tcx, move |annotation, _| {
822822
debug!("pretty printing source code {:?}", s);
823823
let sess = annotation.sess();
@@ -826,7 +826,7 @@ pub fn print_after_hir_lowering<'tcx>(
826826
&sess.parse_sess,
827827
src_name,
828828
&mut rdr,
829-
box out,
829+
out,
830830
annotation.pp_ann());
831831
for node_id in uii.all_matching_node_ids(hir_map) {
832832
let hir_id = tcx.hir().node_to_hir_id(node_id);
@@ -843,13 +843,13 @@ pub fn print_after_hir_lowering<'tcx>(
843843
}
844844

845845
(PpmHirTree(s), Some(uii)) => {
846-
let out: &mut dyn Write = &mut out;
846+
let out = &mut out;
847847
s.call_with_pp_support_hir(tcx, move |_annotation, _krate| {
848848
debug!("pretty printing source code {:?}", s);
849849
for node_id in uii.all_matching_node_ids(tcx.hir()) {
850850
let hir_id = tcx.hir().node_to_hir_id(node_id);
851851
let node = tcx.hir().get(hir_id);
852-
write!(out, "{:#?}", node)?;
852+
out.push_str(&format!("{:#?}", node));
853853
}
854854
Ok(())
855855
})
@@ -859,7 +859,7 @@ pub fn print_after_hir_lowering<'tcx>(
859859
}
860860
.unwrap();
861861

862-
write_output(out, ofile);
862+
write_output(out.into_bytes(), ofile);
863863
}
864864

865865
// In an ideal world, this would be a public function called by the driver after

src/libsyntax/print/pp.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,8 @@ crate struct PrintStackElem {
236236

237237
const SIZE_INFINITY: isize = 0xffff;
238238

239-
pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'a> {
239+
pub fn mk_printer(out: &mut String) -> Printer<'_> {
240+
let linewidth = 78;
240241
// Yes 55, it makes the ring buffers big enough to never fall behind.
241242
let n: usize = 55 * linewidth;
242243
debug!("mk_printer {}", linewidth);
@@ -259,7 +260,7 @@ pub fn mk_printer<'a>(out: Box<dyn io::Write+'a>, linewidth: usize) -> Printer<'
259260
}
260261

261262
pub struct Printer<'a> {
262-
out: Box<dyn io::Write+'a>,
263+
out: &'a mut String,
263264
buf_max_len: usize,
264265
/// Width of lines we're constrained to
265266
margin: isize,
@@ -300,8 +301,6 @@ impl Default for BufEntry {
300301
}
301302
}
302303

303-
const SPACES: [u8; 128] = [b' '; 128];
304-
305304
impl<'a> Printer<'a> {
306305
pub fn last_token(&mut self) -> Token {
307306
self.buf[self.right].token.clone()
@@ -497,10 +496,10 @@ impl<'a> Printer<'a> {
497496

498497
crate fn print_newline(&mut self, amount: isize) -> io::Result<()> {
499498
debug!("NEWLINE {}", amount);
500-
let ret = writeln!(self.out);
499+
self.out.push('\n');
501500
self.pending_indentation = 0;
502501
self.indent(amount);
503-
ret
502+
Ok(())
504503
}
505504

506505
crate fn indent(&mut self, amount: isize) {
@@ -587,20 +586,14 @@ impl<'a> Printer<'a> {
587586
//
588587
// write!(self.out, "{: >n$}", "", n = self.pending_indentation as usize)?;
589588
//
590-
// But that is significantly slower than using `SPACES`. This code is
591-
// sufficiently hot, and indents can get sufficiently large, that the
592-
// difference is significant on some workloads.
593-
let spaces_len = SPACES.len() as isize;
594-
while self.pending_indentation >= spaces_len {
595-
self.out.write_all(&SPACES)?;
596-
self.pending_indentation -= spaces_len;
597-
}
598-
if self.pending_indentation > 0 {
599-
self.out.write_all(&SPACES[0..self.pending_indentation as usize])?;
600-
self.pending_indentation = 0;
601-
}
589+
// But that is significantly slower. This code is sufficiently hot, and indents can get
590+
// sufficiently large, that the difference is significant on some workloads.
591+
self.out.reserve(self.pending_indentation as usize);
592+
self.out.extend(std::iter::repeat(' ').take(self.pending_indentation as usize));
593+
self.pending_indentation = 0;
594+
self.out.push_str(&s);
602595

603-
write!(self.out, "{}", s)
596+
Ok(())
604597
}
605598

606599
crate fn print(&mut self, token: Token, l: isize) -> io::Result<()> {

src/libsyntax/print/pprust.rs

+10-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use syntax_pos::{self, BytePos};
2121
use syntax_pos::{DUMMY_SP, FileName};
2222

2323
use std::borrow::Cow;
24-
use std::io::{self, Write, Read};
24+
use std::io::{self, Read};
2525
use std::vec;
2626

2727
pub enum AnnNode<'a> {
@@ -54,9 +54,9 @@ pub struct State<'a> {
5454
is_expanded: bool
5555
}
5656

57-
fn rust_printer<'a>(writer: Box<dyn Write+'a>, ann: &'a dyn PpAnn) -> State<'a> {
57+
fn rust_printer<'a>(writer: &'a mut String, ann: &'a dyn PpAnn) -> State<'a> {
5858
State {
59-
s: pp::mk_printer(writer, DEFAULT_COLUMNS),
59+
s: pp::mk_printer(writer),
6060
cm: None,
6161
comments: None,
6262
cur_cmnt: 0,
@@ -68,16 +68,14 @@ fn rust_printer<'a>(writer: Box<dyn Write+'a>, ann: &'a dyn PpAnn) -> State<'a>
6868

6969
crate const INDENT_UNIT: usize = 4;
7070

71-
crate const DEFAULT_COLUMNS: usize = 78;
72-
7371
/// Requires you to pass an input filename and reader so that
7472
/// it can scan the input text for comments to copy forward.
7573
pub fn print_crate<'a>(cm: &'a SourceMap,
7674
sess: &ParseSess,
7775
krate: &ast::Crate,
7876
filename: FileName,
7977
input: &mut dyn Read,
80-
out: Box<dyn Write+'a>,
78+
out: &mut String,
8179
ann: &'a dyn PpAnn,
8280
is_expanded: bool) -> io::Result<()> {
8381
let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded);
@@ -111,20 +109,20 @@ impl<'a> State<'a> {
111109
sess: &ParseSess,
112110
filename: FileName,
113111
input: &mut dyn Read,
114-
out: Box<dyn Write+'a>,
112+
out: &'a mut String,
115113
ann: &'a dyn PpAnn,
116114
is_expanded: bool) -> State<'a> {
117115
let comments = comments::gather_comments(sess, filename, input);
118116
State::new(cm, out, ann, Some(comments), is_expanded)
119117
}
120118

121119
pub fn new(cm: &'a SourceMap,
122-
out: Box<dyn Write+'a>,
120+
out: &'a mut String,
123121
ann: &'a dyn PpAnn,
124122
comments: Option<Vec<comments::Comment>>,
125123
is_expanded: bool) -> State<'a> {
126124
State {
127-
s: pp::mk_printer(out, DEFAULT_COLUMNS),
125+
s: pp::mk_printer(out),
128126
cm: Some(cm),
129127
comments,
130128
cur_cmnt: 0,
@@ -138,14 +136,14 @@ impl<'a> State<'a> {
138136
pub fn to_string<F>(f: F) -> String where
139137
F: FnOnce(&mut State<'_>) -> io::Result<()>,
140138
{
141-
let mut wr = Vec::new();
139+
let mut wr = String::new();
142140
{
143141
let ann = NoAnn;
144-
let mut printer = rust_printer(Box::new(&mut wr), &ann);
142+
let mut printer = rust_printer(&mut wr, &ann);
145143
f(&mut printer).unwrap();
146144
printer.s.eof().unwrap();
147145
}
148-
String::from_utf8(wr).unwrap()
146+
wr
149147
}
150148

151149
fn binop_to_string(op: BinOpToken) -> &'static str {

0 commit comments

Comments
 (0)