Skip to content

Commit e93654c

Browse files
committed
Pass the fmt! buffer to each conversion method
Achieves a little more speedup and avoids allocations around some strings in conv_str
1 parent c0bbc62 commit e93654c

File tree

2 files changed

+268
-58
lines changed

2 files changed

+268
-58
lines changed

src/libcore/unstable/extfmt.rs

+209
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,215 @@ pub mod ct {
470470
// decisions made a runtime. If it proves worthwhile then some of these
471471
// conditions can be evaluated at compile-time. For now though it's cleaner to
472472
// implement it this way, I think.
473+
#[cfg(stage1)]
474+
#[cfg(stage2)]
475+
#[cfg(stage3)]
476+
#[doc(hidden)]
477+
pub mod rt {
478+
use float;
479+
use str;
480+
use sys;
481+
use int;
482+
use uint;
483+
use vec;
484+
use option::{Some, None, Option};
485+
486+
pub const flag_none : u32 = 0u32;
487+
pub const flag_left_justify : u32 = 0b00000000000001u32;
488+
pub const flag_left_zero_pad : u32 = 0b00000000000010u32;
489+
pub const flag_space_for_sign : u32 = 0b00000000000100u32;
490+
pub const flag_sign_always : u32 = 0b00000000001000u32;
491+
pub const flag_alternate : u32 = 0b00000000010000u32;
492+
493+
pub enum Count { CountIs(uint), CountImplied, }
494+
495+
pub enum Ty { TyDefault, TyBits, TyHexUpper, TyHexLower, TyOctal, }
496+
497+
pub struct Conv {
498+
flags: u32,
499+
width: Count,
500+
precision: Count,
501+
ty: Ty,
502+
}
503+
504+
pub pure fn conv_int(cv: Conv, i: int, buf: &mut ~str) {
505+
let radix = 10;
506+
let prec = get_int_precision(cv);
507+
let mut s : ~str = uint_to_str_prec(int::abs(i) as uint, radix, prec);
508+
509+
let head = if i >= 0 {
510+
if have_flag(cv.flags, flag_sign_always) {
511+
Some('+')
512+
} else if have_flag(cv.flags, flag_space_for_sign) {
513+
Some(' ')
514+
} else {
515+
None
516+
}
517+
} else { Some('-') };
518+
unsafe { pad(cv, s, head, PadSigned, buf) };
519+
}
520+
pub pure fn conv_uint(cv: Conv, u: uint, buf: &mut ~str) {
521+
let prec = get_int_precision(cv);
522+
let mut rs =
523+
match cv.ty {
524+
TyDefault => uint_to_str_prec(u, 10, prec),
525+
TyHexLower => uint_to_str_prec(u, 16, prec),
526+
TyHexUpper => str::to_upper(uint_to_str_prec(u, 16, prec)),
527+
TyBits => uint_to_str_prec(u, 2, prec),
528+
TyOctal => uint_to_str_prec(u, 8, prec)
529+
};
530+
unsafe { pad(cv, rs, None, PadUnsigned, buf) };
531+
}
532+
pub pure fn conv_bool(cv: Conv, b: bool, buf: &mut ~str) {
533+
let s = if b { "true" } else { "false" };
534+
// run the boolean conversion through the string conversion logic,
535+
// giving it the same rules for precision, etc.
536+
conv_str(cv, s, buf);
537+
}
538+
pub pure fn conv_char(cv: Conv, c: char, buf: &mut ~str) {
539+
unsafe { pad(cv, "", Some(c), PadNozero, buf) };
540+
}
541+
pub pure fn conv_str(cv: Conv, s: &str, buf: &mut ~str) {
542+
// For strings, precision is the maximum characters
543+
// displayed
544+
let mut unpadded = match cv.precision {
545+
CountImplied => s,
546+
CountIs(max) => if (max as uint) < str::char_len(s) {
547+
str::slice(s, 0, max as uint)
548+
} else {
549+
s
550+
}
551+
};
552+
unsafe { pad(cv, unpadded, None, PadNozero, buf) };
553+
}
554+
pub pure fn conv_float(cv: Conv, f: float, buf: &mut ~str) {
555+
let (to_str, digits) = match cv.precision {
556+
CountIs(c) => (float::to_str_exact, c as uint),
557+
CountImplied => (float::to_str_digits, 6u)
558+
};
559+
let mut s = unsafe { to_str(f, digits) };
560+
let head = if 0.0 <= f {
561+
if have_flag(cv.flags, flag_sign_always) {
562+
Some('+')
563+
} else if have_flag(cv.flags, flag_space_for_sign) {
564+
Some(' ')
565+
} else {
566+
None
567+
}
568+
} else { None };
569+
unsafe { pad(cv, s, head, PadFloat, buf) };
570+
}
571+
pub pure fn conv_poly<T>(cv: Conv, v: &T, buf: &mut ~str) {
572+
let s = sys::log_str(v);
573+
conv_str(cv, s, buf);
574+
}
575+
576+
// Convert a uint to string with a minimum number of digits. If precision
577+
// is 0 and num is 0 then the result is the empty string. Could move this
578+
// to uint: but it doesn't seem all that useful.
579+
pub pure fn uint_to_str_prec(num: uint, radix: uint,
580+
prec: uint) -> ~str {
581+
return if prec == 0u && num == 0u {
582+
~""
583+
} else {
584+
let s = uint::to_str_radix(num, radix);
585+
let len = str::char_len(s);
586+
if len < prec {
587+
let diff = prec - len;
588+
let pad = str::from_chars(vec::from_elem(diff, '0'));
589+
pad + s
590+
} else { s }
591+
};
592+
}
593+
pub pure fn get_int_precision(cv: Conv) -> uint {
594+
return match cv.precision {
595+
CountIs(c) => c as uint,
596+
CountImplied => 1u
597+
};
598+
}
599+
600+
#[deriving(Eq)]
601+
pub enum PadMode { PadSigned, PadUnsigned, PadNozero, PadFloat }
602+
603+
pub fn pad(cv: Conv, mut s: &str, head: Option<char>, mode: PadMode,
604+
buf: &mut ~str) {
605+
let headsize = match head { Some(_) => 1, _ => 0 };
606+
let uwidth : uint = match cv.width {
607+
CountImplied => {
608+
for head.each |&c| {
609+
buf.push_char(c);
610+
}
611+
return buf.push_str(s);
612+
}
613+
CountIs(width) => { width as uint }
614+
};
615+
let strlen = str::char_len(s) + headsize;
616+
if uwidth <= strlen {
617+
for head.each |&c| {
618+
buf.push_char(c);
619+
}
620+
return buf.push_str(s);
621+
}
622+
let mut padchar = ' ';
623+
let diff = uwidth - strlen;
624+
if have_flag(cv.flags, flag_left_justify) {
625+
for head.each |&c| {
626+
buf.push_char(c);
627+
}
628+
buf.push_str(s);
629+
for diff.times {
630+
buf.push_char(padchar);
631+
}
632+
return;
633+
}
634+
let (might_zero_pad, signed) = match mode {
635+
PadNozero => (false, true),
636+
PadSigned => (true, true),
637+
PadFloat => (true, true),
638+
PadUnsigned => (true, false)
639+
};
640+
pure fn have_precision(cv: Conv) -> bool {
641+
return match cv.precision { CountImplied => false, _ => true };
642+
}
643+
let zero_padding = {
644+
if might_zero_pad && have_flag(cv.flags, flag_left_zero_pad) &&
645+
(!have_precision(cv) || mode == PadFloat) {
646+
padchar = '0';
647+
true
648+
} else {
649+
false
650+
}
651+
};
652+
let padstr = str::from_chars(vec::from_elem(diff, padchar));
653+
// This is completely heinous. If we have a signed value then
654+
// potentially rip apart the intermediate result and insert some
655+
// zeros. It may make sense to convert zero padding to a precision
656+
// instead.
657+
658+
if signed && zero_padding {
659+
for head.each |&head| {
660+
if head == '+' || head == '-' || head == ' ' {
661+
buf.push_char(head);
662+
buf.push_str(padstr);
663+
buf.push_str(s);
664+
return;
665+
}
666+
}
667+
}
668+
buf.push_str(padstr);
669+
for head.each |&c| {
670+
buf.push_char(c);
671+
}
672+
buf.push_str(s);
673+
}
674+
#[inline(always)]
675+
pub pure fn have_flag(flags: u32, f: u32) -> bool {
676+
flags & f != 0
677+
}
678+
}
679+
680+
// XXX: remove after a snapshot of the above changes have gone in
681+
#[cfg(stage0)]
473682
#[doc(hidden)]
474683
pub mod rt {
475684
use float;

src/libsyntax/ext/fmt.rs

+59-58
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,17 @@ fn pieces_to_expr(cx: @ext_ctxt, sp: span,
139139
make_conv_struct(cx, sp, rt_conv_flags, rt_conv_width,
140140
rt_conv_precision, rt_conv_ty)
141141
}
142-
fn make_conv_call(cx: @ext_ctxt, sp: span, conv_type: ~str, cnv: &Conv,
143-
arg: @ast::expr) -> @ast::expr {
142+
fn make_conv_call(cx: @ext_ctxt, sp: span, conv_type: &str, cnv: &Conv,
143+
arg: @ast::expr, buf: @ast::expr) -> @ast::expr {
144144
let fname = ~"conv_" + conv_type;
145145
let path = make_path_vec(cx, @fname);
146146
let cnv_expr = make_rt_conv_expr(cx, sp, cnv);
147-
let args = ~[cnv_expr, arg];
147+
let args = ~[cnv_expr, arg, buf];
148148
return mk_call_global(cx, arg.span, path, args);
149149
}
150150

151-
fn make_new_conv(cx: @ext_ctxt, sp: span, cnv: &Conv, arg: @ast::expr) ->
152-
@ast::expr {
153-
// FIXME: Move validation code into core::extfmt (Issue #2249)
154-
151+
fn make_new_conv(cx: @ext_ctxt, sp: span, cnv: &Conv,
152+
arg: @ast::expr, buf: @ast::expr) -> @ast::expr {
155153
fn is_signed_type(cnv: &Conv) -> bool {
156154
match cnv.ty {
157155
TyInt(s) => match s {
@@ -198,27 +196,17 @@ fn pieces_to_expr(cx: @ext_ctxt, sp: span,
198196
CountIs(_) => (),
199197
_ => cx.span_unimpl(sp, unsupported)
200198
}
201-
match cnv.ty {
202-
TyStr => return make_conv_call(cx, arg.span, ~"str", cnv, arg),
203-
TyInt(sign) => match sign {
204-
Signed => return make_conv_call(cx, arg.span, ~"int", cnv, arg),
205-
Unsigned => {
206-
return make_conv_call(cx, arg.span, ~"uint", cnv, arg)
207-
}
208-
},
209-
TyBool => return make_conv_call(cx, arg.span, ~"bool", cnv, arg),
210-
TyChar => return make_conv_call(cx, arg.span, ~"char", cnv, arg),
211-
TyHex(_) => {
212-
return make_conv_call(cx, arg.span, ~"uint", cnv, arg);
213-
}
214-
TyBits => return make_conv_call(cx, arg.span, ~"uint", cnv, arg),
215-
TyOctal => return make_conv_call(cx, arg.span, ~"uint", cnv, arg),
216-
TyFloat => {
217-
return make_conv_call(cx, arg.span, ~"float", cnv, arg);
218-
}
219-
TyPoly => return make_conv_call(cx, arg.span, ~"poly", cnv,
220-
mk_addr_of(cx, sp, arg))
221-
}
199+
let (name, actual_arg) = match cnv.ty {
200+
TyStr => ("str", arg),
201+
TyInt(Signed) => ("int", arg),
202+
TyBool => ("bool", arg),
203+
TyChar => ("char", arg),
204+
TyBits | TyOctal | TyHex(_) | TyInt(Unsigned) => ("uint", arg),
205+
TyFloat => ("float", arg),
206+
TyPoly => ("poly", mk_addr_of(cx, sp, arg))
207+
};
208+
return make_conv_call(cx, arg.span, name, cnv, actual_arg,
209+
mk_mut_addr_of(cx, arg.span, buf));
222210
}
223211
fn log_conv(c: &Conv) {
224212
debug!("Building conversion:");
@@ -270,57 +258,70 @@ fn pieces_to_expr(cx: @ext_ctxt, sp: span,
270258
}
271259
}
272260

273-
/* Translate each piece (portion of the fmt expression) into a ~str
274-
expression to be concatenated below */
275261
let fmt_sp = args[0].span;
276262
let mut n = 0u;
277263
let nargs = args.len();
278-
let pieces = do vec::map_consume(pieces) |pc| {
264+
265+
/* 'ident' is the local buffer building up the result of fmt! */
266+
let ident = cx.parse_sess().interner.intern(@~"__fmtbuf");
267+
let buf = || mk_path(cx, fmt_sp, ~[ident]);
268+
let str_ident = cx.parse_sess().interner.intern(@~"str");
269+
let push_ident = cx.parse_sess().interner.intern(@~"push_str");
270+
let mut stms = ~[];
271+
272+
/* Translate each piece (portion of the fmt expression) by invoking the
273+
corresponding function in core::unstable::extfmt. Each function takes a
274+
buffer to insert data into along with the data being formatted. */
275+
do vec::consume(pieces) |i, pc| {
279276
match pc {
280-
PieceString(s) => mk_uniq_str(cx, fmt_sp, s),
277+
/* Raw strings get appended via str::push_str */
278+
PieceString(s) => {
279+
let portion = mk_uniq_str(cx, fmt_sp, s);
280+
281+
/* If this is the first portion, then initialize the local
282+
buffer with it directly */
283+
if i == 0 {
284+
stms.push(mk_local(cx, fmt_sp, true, ident, portion));
285+
} else {
286+
let args = ~[mk_mut_addr_of(cx, fmt_sp, buf()), portion];
287+
let call = mk_call_global(cx,
288+
fmt_sp,
289+
~[str_ident, push_ident],
290+
args);
291+
stms.push(mk_stmt(cx, fmt_sp, call));
292+
}
293+
}
294+
295+
/* Invoke the correct conv function in extfmt */
281296
PieceConv(ref conv) => {
282297
n += 1u;
283298
if n >= nargs {
284299
cx.span_fatal(sp,
285300
~"not enough arguments to fmt! " +
286301
~"for the given format string");
287302
}
303+
288304
log_conv(conv);
289-
make_new_conv(cx, fmt_sp, conv, args[n])
305+
/* If the first portion is a conversion, then the local buffer
306+
must be initialized as an empty string */
307+
if i == 0 {
308+
stms.push(mk_local(cx, fmt_sp, true, ident,
309+
mk_uniq_str(cx, fmt_sp, ~"")));
310+
}
311+
stms.push(mk_stmt(cx, fmt_sp,
312+
make_new_conv(cx, fmt_sp, conv,
313+
args[n], buf())));
290314
}
291315
}
292-
};
316+
}
317+
293318
let expected_nargs = n + 1u; // n conversions + the fmt string
294319
if expected_nargs < nargs {
295320
cx.span_fatal
296321
(sp, fmt!("too many arguments to fmt!. found %u, expected %u",
297322
nargs, expected_nargs));
298323
}
299324

300-
/* Concatenate all of the strings together with str::push_str. This
301-
involves storing the first piece into a local variable, and then
302-
pushing each other piece onto the local. The local is contained in its
303-
own block to not conflict with other names as much as possible */
304-
let ident = cx.parse_sess().interner.intern(@~"__fmtbuf");
305-
let buf = || mk_path(cx, fmt_sp, ~[ident]);
306-
let str_ident = cx.parse_sess().interner.intern(@~"str");
307-
let push_ident = cx.parse_sess().interner.intern(@~"push_str");
308-
309-
let mut first = true;
310-
let stms = do vec::map_consume(pieces) |pc| {
311-
if first {
312-
first = false;
313-
mk_local(cx, fmt_sp, true, ident, pc)
314-
} else {
315-
let call = mk_call_global(cx,
316-
fmt_sp,
317-
~[str_ident, push_ident],
318-
~[mk_mut_addr_of(cx, fmt_sp, buf()),
319-
pc]);
320-
mk_stmt(cx, fmt_sp, call)
321-
}
322-
};
323-
324325
return mk_block(cx, fmt_sp, ~[], stms, Some(buf()));
325326
}
326327
//

0 commit comments

Comments
 (0)