Commit f91cd447 authored by Rob Pike's avatar Rob Pike

save a few ns by inlining (which mostly simplifies things anyway).

a couple of cleanups.
don't keep big buffers in the free list.

R=rsc
CC=golang-dev
https://golang.org/cl/166078
parent 353ef80f
...@@ -16,6 +16,11 @@ const ( ...@@ -16,6 +16,11 @@ const (
udigits = "0123456789ABCDEF"; udigits = "0123456789ABCDEF";
) )
const (
signed = true;
unsigned = false;
)
var padZeroBytes = make([]byte, nByte) var padZeroBytes = make([]byte, nByte)
var padSpaceBytes = make([]byte, nByte) var padSpaceBytes = make([]byte, nByte)
...@@ -31,18 +36,19 @@ func init() { ...@@ -31,18 +36,19 @@ func init() {
// A fmt is the raw formatter used by Printf etc. // A fmt is the raw formatter used by Printf etc.
// It prints into a bytes.Buffer that must be set up externally. // It prints into a bytes.Buffer that must be set up externally.
type fmt struct { type fmt struct {
intbuf [nByte]byte; intbuf [nByte]byte;
buf *bytes.Buffer; buf *bytes.Buffer;
wid int; // width, precision
wid int;
prec int;
// flags
widPresent bool; widPresent bool;
prec int;
precPresent bool; precPresent bool;
// flags minus bool;
minus bool; plus bool;
plus bool; sharp bool;
sharp bool; space bool;
space bool; zero bool;
zero bool;
} }
func (f *fmt) clearflags() { func (f *fmt) clearflags() {
...@@ -98,7 +104,8 @@ func (f *fmt) writePadding(n int, padding []byte) { ...@@ -98,7 +104,8 @@ func (f *fmt) writePadding(n int, padding []byte) {
} }
// Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus) // Append b to f.buf, padded on left (w > 0) or right (w < 0 or f.minus)
func (f *fmt) padBytes(b []byte) { // clear flags aftewards.
func (f *fmt) pad(b []byte) {
var padding []byte; var padding []byte;
var left, right int; var left, right int;
if f.widPresent && f.wid != 0 { if f.widPresent && f.wid != 0 {
...@@ -111,10 +118,12 @@ func (f *fmt) padBytes(b []byte) { ...@@ -111,10 +118,12 @@ func (f *fmt) padBytes(b []byte) {
if right > 0 { if right > 0 {
f.writePadding(right, padding) f.writePadding(right, padding)
} }
f.clearflags();
} }
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus) // append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
func (f *fmt) pad(s string) { // clear flags aftewards.
func (f *fmt) padString(s string) {
var padding []byte; var padding []byte;
var left, right int; var left, right int;
if f.widPresent && f.wid != 0 { if f.widPresent && f.wid != 0 {
...@@ -127,13 +136,9 @@ func (f *fmt) pad(s string) { ...@@ -127,13 +136,9 @@ func (f *fmt) pad(s string) {
if right > 0 { if right > 0 {
f.writePadding(right, padding) f.writePadding(right, padding)
} }
f.clearflags();
} }
// format val into buf, ending at buf[i]. (printing is easier right-to-left;
// that's why the bidi languages are right-to-left except for numbers. wait,
// never mind.) val is known to be unsigned. we could make things maybe
// marginally faster by splitting the 32-bit case out into a separate function
// but it's not worth the duplication, so val has 64 bits.
func putint(buf []byte, base, val uint64, digits string) int { func putint(buf []byte, base, val uint64, digits string) int {
i := len(buf) - 1; i := len(buf) - 1;
for val >= base { for val >= base {
...@@ -148,17 +153,17 @@ func putint(buf []byte, base, val uint64, digits string) int { ...@@ -148,17 +153,17 @@ func putint(buf []byte, base, val uint64, digits string) int {
// fmt_boolean formats a boolean. // fmt_boolean formats a boolean.
func (f *fmt) fmt_boolean(v bool) { func (f *fmt) fmt_boolean(v bool) {
if v { if v {
f.pad("true") f.padString("true")
} else { } else {
f.pad("false") f.padString("false")
} }
f.clearflags();
} }
// integer; interprets prec but not wid. // integer; interprets prec but not wid. Once formatted, result is sent to pad()
func (f *fmt) integer(a int64, base uint, is_signed bool, digits string) []byte { // and then flags are cleared.
func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
var buf []byte = &f.intbuf; var buf []byte = &f.intbuf;
negative := is_signed && a < 0; negative := signedness == signed && a < 0;
if negative { if negative {
a = -a a = -a
} }
...@@ -176,154 +181,136 @@ func (f *fmt) integer(a int64, base uint, is_signed bool, digits string) []byte ...@@ -176,154 +181,136 @@ func (f *fmt) integer(a int64, base uint, is_signed bool, digits string) []byte
} }
} }
i := putint(buf, uint64(base), uint64(a), digits); // format a into buf, ending at buf[i]. (printing is easier right-to-left.)
for i > 0 && prec > (nByte-1-i) { // a is made into unsigned ua. we could make things
buf[i] = '0'; // marginally faster by splitting the 32-bit case out into a separate
// block but it's not worth the duplication, so ua has 64 bits.
i := len(f.intbuf);
ua := uint64(a);
for ua >= base {
i--;
buf[i] = digits[ua%base];
ua /= base;
}
i--;
buf[i] = digits[ua];
for i > 0 && prec > nByte-i {
i--; i--;
buf[i] = '0';
} }
// Various prefixes: 0x, -, etc.
if f.sharp { if f.sharp {
switch base { switch base {
case 8: case 8:
if buf[i+1] != '0' { if buf[i] != '0' {
buf[i] = '0';
i--; i--;
buf[i] = '0';
} }
case 16: case 16:
i--;
buf[i] = 'x' + digits[10] - 'a'; buf[i] = 'x' + digits[10] - 'a';
i--; i--;
buf[i] = '0'; buf[i] = '0';
i--;
} }
} }
if negative { if negative {
buf[i] = '-';
i--; i--;
buf[i] = '-';
} else if f.plus { } else if f.plus {
buf[i] = '+';
i--; i--;
buf[i] = '+';
} else if f.space { } else if f.space {
buf[i] = ' ';
i--; i--;
buf[i] = ' ';
} }
return buf[i+1 : nByte]; f.pad(buf[i:]);
} }
// fmt_d64 formats an int64 in decimal. // fmt_d64 formats an int64 in decimal.
func (f *fmt) fmt_d64(v int64) { func (f *fmt) fmt_d64(v int64) { f.integer(v, 10, signed, ldigits) }
f.padBytes(f.integer(v, 10, true, ldigits));
f.clearflags();
}
// fmt_d32 formats an int32 in decimal. // fmt_d32 formats an int32 in decimal.
func (f *fmt) fmt_d32(v int32) { f.fmt_d64(int64(v)) } func (f *fmt) fmt_d32(v int32) { f.integer(int64(v), 10, signed, ldigits) }
// fmt_d formats an int in decimal. // fmt_d formats an int in decimal.
func (f *fmt) fmt_d(v int) { f.fmt_d64(int64(v)) } func (f *fmt) fmt_d(v int) { f.integer(int64(v), 10, signed, ldigits) }
// fmt_ud64 formats a uint64 in decimal. // fmt_ud64 formats a uint64 in decimal.
func (f *fmt) fmt_ud64(v uint64) *fmt { func (f *fmt) fmt_ud64(v uint64) { f.integer(int64(v), 10, unsigned, ldigits) }
f.padBytes(f.integer(int64(v), 10, false, ldigits));
f.clearflags();
return f;
}
// fmt_ud32 formats a uint32 in decimal. // fmt_ud32 formats a uint32 in decimal.
func (f *fmt) fmt_ud32(v uint32) { f.fmt_ud64(uint64(v)) } func (f *fmt) fmt_ud32(v uint32) { f.integer(int64(v), 10, unsigned, ldigits) }
// fmt_ud formats a uint in decimal. // fmt_ud formats a uint in decimal.
func (f *fmt) fmt_ud(v uint) { f.fmt_ud64(uint64(v)) } func (f *fmt) fmt_ud(v uint) { f.integer(int64(v), 10, unsigned, ldigits) }
// fmt_x64 formats an int64 in hexadecimal. // fmt_x64 formats an int64 in hexadecimal.
func (f *fmt) fmt_x64(v int64) { func (f *fmt) fmt_x64(v int64) { f.integer(v, 16, signed, ldigits) }
f.padBytes(f.integer(v, 16, true, ldigits));
f.clearflags();
}
// fmt_x32 formats an int32 in hexadecimal. // fmt_x32 formats an int32 in hexadecimal.
func (f *fmt) fmt_x32(v int32) { f.fmt_x64(int64(v)) } func (f *fmt) fmt_x32(v int32) { f.integer(int64(v), 16, signed, ldigits) }
// fmt_x formats an int in hexadecimal. // fmt_x formats an int in hexadecimal.
func (f *fmt) fmt_x(v int) { f.fmt_x64(int64(v)) } func (f *fmt) fmt_x(v int) { f.integer(int64(v), 16, signed, ldigits) }
// fmt_ux64 formats a uint64 in hexadecimal. // fmt_ux64 formats a uint64 in hexadecimal.
func (f *fmt) fmt_ux64(v uint64) { func (f *fmt) fmt_ux64(v uint64) { f.integer(int64(v), 16, unsigned, ldigits) }
f.padBytes(f.integer(int64(v), 16, false, ldigits));
f.clearflags();
}
// fmt_ux32 formats a uint32 in hexadecimal. // fmt_ux32 formats a uint32 in hexadecimal.
func (f *fmt) fmt_ux32(v uint32) { f.fmt_ux64(uint64(v)) } func (f *fmt) fmt_ux32(v uint32) { f.integer(int64(v), 16, unsigned, ldigits) }
// fmt_ux formats a uint in hexadecimal. // fmt_ux formats a uint in hexadecimal.
func (f *fmt) fmt_ux(v uint) { f.fmt_ux64(uint64(v)) } func (f *fmt) fmt_ux(v uint) { f.integer(int64(v), 16, unsigned, ldigits) }
// fmt_X64 formats an int64 in upper case hexadecimal. // fmt_X64 formats an int64 in upper case hexadecimal.
func (f *fmt) fmt_X64(v int64) { func (f *fmt) fmt_X64(v int64) { f.integer(v, 16, signed, udigits) }
f.padBytes(f.integer(v, 16, true, udigits));
f.clearflags();
}
// fmt_X32 formats an int32 in upper case hexadecimal. // fmt_X32 formats an int32 in upper case hexadecimal.
func (f *fmt) fmt_X32(v int32) { f.fmt_X64(int64(v)) } func (f *fmt) fmt_X32(v int32) { f.integer(int64(v), 16, signed, udigits) }
// fmt_X formats an int in upper case hexadecimal. // fmt_X formats an int in upper case hexadecimal.
func (f *fmt) fmt_X(v int) { f.fmt_X64(int64(v)) } func (f *fmt) fmt_X(v int) { f.integer(int64(v), 16, signed, udigits) }
// fmt_uX64 formats a uint64 in upper case hexadecimal. // fmt_uX64 formats a uint64 in upper case hexadecimal.
func (f *fmt) fmt_uX64(v uint64) { func (f *fmt) fmt_uX64(v uint64) { f.integer(int64(v), 16, unsigned, udigits) }
f.padBytes(f.integer(int64(v), 16, false, udigits));
f.clearflags();
}
// fmt_uX32 formats a uint32 in upper case hexadecimal. // fmt_uX32 formats a uint32 in upper case hexadecimal.
func (f *fmt) fmt_uX32(v uint32) { f.fmt_uX64(uint64(v)) } func (f *fmt) fmt_uX32(v uint32) { f.integer(int64(v), 16, unsigned, udigits) }
// fmt_uX formats a uint in upper case hexadecimal. // fmt_uX formats a uint in upper case hexadecimal.
func (f *fmt) fmt_uX(v uint) { f.fmt_uX64(uint64(v)) } func (f *fmt) fmt_uX(v uint) { f.integer(int64(v), 16, unsigned, udigits) }
// fmt_o64 formats an int64 in octal. // fmt_o64 formats an int64 in octal.
func (f *fmt) fmt_o64(v int64) { func (f *fmt) fmt_o64(v int64) { f.integer(v, 8, signed, ldigits) }
f.padBytes(f.integer(v, 8, true, ldigits));
f.clearflags();
}
// fmt_o32 formats an int32 in octal. // fmt_o32 formats an int32 in octal.
func (f *fmt) fmt_o32(v int32) { f.fmt_o64(int64(v)) } func (f *fmt) fmt_o32(v int32) { f.integer(int64(v), 8, signed, ldigits) }
// fmt_o formats an int in octal. // fmt_o formats an int in octal.
func (f *fmt) fmt_o(v int) { f.fmt_o64(int64(v)) } func (f *fmt) fmt_o(v int) { f.integer(int64(v), 8, signed, ldigits) }
// fmt_uo64 formats a uint64 in octal. // fmt_uo64 formats a uint64 in octal.
func (f *fmt) fmt_uo64(v uint64) { func (f *fmt) fmt_uo64(v uint64) { f.integer(int64(v), 8, unsigned, ldigits) }
f.padBytes(f.integer(int64(v), 8, false, ldigits));
f.clearflags();
}
// fmt_uo32 formats a uint32 in octal. // fmt_uo32 formats a uint32 in octal.
func (f *fmt) fmt_uo32(v uint32) { f.fmt_uo64(uint64(v)) } func (f *fmt) fmt_uo32(v uint32) { f.integer(int64(v), 8, unsigned, ldigits) }
// fmt_uo formats a uint in octal. // fmt_uo formats a uint in octal.
func (f *fmt) fmt_uo(v uint) { f.fmt_uo64(uint64(v)) } func (f *fmt) fmt_uo(v uint) { f.integer(int64(v), 8, unsigned, ldigits) }
// fmt_b64 formats a uint64 in binary. // fmt_b64 formats a uint64 in binary.
func (f *fmt) fmt_b64(v uint64) { func (f *fmt) fmt_b64(v uint64) { f.integer(int64(v), 2, unsigned, ldigits) }
f.padBytes(f.integer(int64(v), 2, false, ldigits));
f.clearflags();
}
// fmt_b32 formats a uint32 in binary. // fmt_b32 formats a uint32 in binary.
func (f *fmt) fmt_b32(v uint32) { f.fmt_b64(uint64(v)) } func (f *fmt) fmt_b32(v uint32) { f.integer(int64(v), 2, unsigned, ldigits) }
// fmt_b formats a uint in binary. // fmt_b formats a uint in binary.
func (f *fmt) fmt_b(v uint) { f.fmt_b64(uint64(v)) } func (f *fmt) fmt_b(v uint) { f.integer(int64(v), 2, unsigned, ldigits) }
// fmt_c formats a Unicode character. // fmt_c formats a Unicode character.
func (f *fmt) fmt_c(v int) { func (f *fmt) fmt_c(v int) { f.padString(string(v)) }
f.pad(string(v));
f.clearflags();
}
// fmt_s formats a string. // fmt_s formats a string.
func (f *fmt) fmt_s(s string) { func (f *fmt) fmt_s(s string) {
...@@ -332,8 +319,7 @@ func (f *fmt) fmt_s(s string) { ...@@ -332,8 +319,7 @@ func (f *fmt) fmt_s(s string) {
s = s[0:f.prec] s = s[0:f.prec]
} }
} }
f.pad(s); f.padString(s);
f.clearflags();
} }
// fmt_sx formats a string as a hexadecimal encoding of its bytes. // fmt_sx formats a string as a hexadecimal encoding of its bytes.
...@@ -347,8 +333,7 @@ func (f *fmt) fmt_sx(s string) { ...@@ -347,8 +333,7 @@ func (f *fmt) fmt_sx(s string) {
t += string(ldigits[v>>4]); t += string(ldigits[v>>4]);
t += string(ldigits[v&0xF]); t += string(ldigits[v&0xF]);
} }
f.pad(t); f.padString(t);
f.clearflags();
} }
// fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes. // fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes.
...@@ -359,8 +344,7 @@ func (f *fmt) fmt_sX(s string) { ...@@ -359,8 +344,7 @@ func (f *fmt) fmt_sX(s string) {
t += string(udigits[v>>4]); t += string(udigits[v>>4]);
t += string(udigits[v&0xF]); t += string(udigits[v&0xF]);
} }
f.pad(t); f.padString(t);
f.clearflags();
} }
// fmt_q formats a string as a double-quoted, escaped Go string constant. // fmt_q formats a string as a double-quoted, escaped Go string constant.
...@@ -371,8 +355,7 @@ func (f *fmt) fmt_q(s string) { ...@@ -371,8 +355,7 @@ func (f *fmt) fmt_q(s string) {
} else { } else {
quoted = strconv.Quote(s) quoted = strconv.Quote(s)
} }
f.pad(quoted); f.padString(quoted);
f.clearflags();
} }
// floating-point // floating-point
...@@ -384,12 +367,7 @@ func doPrec(f *fmt, def int) int { ...@@ -384,12 +367,7 @@ func doPrec(f *fmt, def int) int {
return def; return def;
} }
func fmtString(f *fmt, s string) { // Add a plus sign or space to the floating-point string representation if missing and required.
f.pad(s);
f.clearflags();
}
// Add a plus sign or space to the string if missing and required.
func (f *fmt) plusSpace(s string) { func (f *fmt) plusSpace(s string) {
if s[0] != '-' { if s[0] != '-' {
if f.plus { if f.plus {
...@@ -398,7 +376,7 @@ func (f *fmt) plusSpace(s string) { ...@@ -398,7 +376,7 @@ func (f *fmt) plusSpace(s string) {
s = " " + s s = " " + s
} }
} }
fmtString(f, s); f.padString(s);
} }
// fmt_e64 formats a float64 in the form -1.23e+12. // fmt_e64 formats a float64 in the form -1.23e+12.
...@@ -439,7 +417,7 @@ func (f *fmt) fmt_g32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'g', doPrec(f, ...@@ -439,7 +417,7 @@ func (f *fmt) fmt_g32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'g', doPrec(f,
func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1))) } func (f *fmt) fmt_G32(v float32) { f.plusSpace(strconv.Ftoa32(v, 'G', doPrec(f, -1))) }
// fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2). // fmt_fb32 formats a float32 in the form -123p3 (exponent is power of 2).
func (f *fmt) fmt_fb32(v float32) { fmtString(f, strconv.Ftoa32(v, 'b', 0)) } func (f *fmt) fmt_fb32(v float32) { f.padString(strconv.Ftoa32(v, 'b', 0)) }
// float // float
func (x *fmt) f(a float) { func (x *fmt) f(a float) {
......
...@@ -136,7 +136,6 @@ type GoStringer interface { ...@@ -136,7 +136,6 @@ type GoStringer interface {
GoString() string; GoString() string;
} }
const runeSelf = utf8.RuneSelf
const allocSize = 32 const allocSize = 32
type pp struct { type pp struct {
...@@ -149,17 +148,25 @@ type pp struct { ...@@ -149,17 +148,25 @@ type pp struct {
// A leaky bucket of reusable pp structures. // A leaky bucket of reusable pp structures.
var ppFree = make(chan *pp, 100) var ppFree = make(chan *pp, 100)
// Allocate a new pp struct. Probably can grab the previous one from ppFree.
func newPrinter() *pp { func newPrinter() *pp {
p, ok := <-ppFree; p, ok := <-ppFree;
if !ok { if !ok {
p = new(pp) p = new(pp)
} }
p.buf.Reset();
p.fmt.init(&p.buf); p.fmt.init(&p.buf);
return p; return p;
} }
func (p *pp) free() { _ = ppFree <- p } // Save used pp structs in ppFree; avoids an allocation per invocation.
func (p *pp) free() {
// Don't hold on to pp structs with large buffers.
if cap(p.buf.Bytes()) > 1024 {
return
}
p.buf.Reset();
_ = ppFree <- p;
}
func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent } func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent }
...@@ -182,7 +189,7 @@ func (p *pp) Flag(b int) bool { ...@@ -182,7 +189,7 @@ func (p *pp) Flag(b int) bool {
} }
func (p *pp) add(c int) { func (p *pp) add(c int) {
if c < runeSelf { if c < utf8.RuneSelf {
p.buf.WriteByte(byte(c)) p.buf.WriteByte(byte(c))
} else { } else {
w := utf8.EncodeRune(c, &p.runeBuf); w := utf8.EncodeRune(c, &p.runeBuf);
...@@ -250,8 +257,9 @@ func Sprint(a ...) string { ...@@ -250,8 +257,9 @@ func Sprint(a ...) string {
v := reflect.NewValue(a).(*reflect.StructValue); v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter(); p := newPrinter();
p.doprint(v, false, false); p.doprint(v, false, false);
s := p.buf.String();
p.free(); p.free();
return p.buf.String(); return s;
} }
// These routines end in 'ln', do not take a format string, // These routines end in 'ln', do not take a format string,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment