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 (
udigits = "0123456789ABCDEF";
)
const (
signed = true;
unsigned = false;
)
var padZeroBytes = make([]byte, nByte)
var padSpaceBytes = make([]byte, nByte)
......@@ -31,18 +36,19 @@ func init() {
// A fmt is the raw formatter used by Printf etc.
// It prints into a bytes.Buffer that must be set up externally.
type fmt struct {
intbuf [nByte]byte;
buf *bytes.Buffer;
wid int;
intbuf [nByte]byte;
buf *bytes.Buffer;
// width, precision
wid int;
prec int;
// flags
widPresent bool;
prec int;
precPresent bool;
// flags
minus bool;
plus bool;
sharp bool;
space bool;
zero bool;
minus bool;
plus bool;
sharp bool;
space bool;
zero bool;
}
func (f *fmt) clearflags() {
......@@ -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)
func (f *fmt) padBytes(b []byte) {
// clear flags aftewards.
func (f *fmt) pad(b []byte) {
var padding []byte;
var left, right int;
if f.widPresent && f.wid != 0 {
......@@ -111,10 +118,12 @@ func (f *fmt) padBytes(b []byte) {
if right > 0 {
f.writePadding(right, padding)
}
f.clearflags();
}
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus)
func (f *fmt) pad(s string) {
// append s to buf, padded on left (w > 0) or right (w < 0 or f.minus).
// clear flags aftewards.
func (f *fmt) padString(s string) {
var padding []byte;
var left, right int;
if f.widPresent && f.wid != 0 {
......@@ -127,13 +136,9 @@ func (f *fmt) pad(s string) {
if right > 0 {
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 {
i := len(buf) - 1;
for val >= base {
......@@ -148,17 +153,17 @@ func putint(buf []byte, base, val uint64, digits string) int {
// fmt_boolean formats a boolean.
func (f *fmt) fmt_boolean(v bool) {
if v {
f.pad("true")
f.padString("true")
} else {
f.pad("false")
f.padString("false")
}
f.clearflags();
}
// integer; interprets prec but not wid.
func (f *fmt) integer(a int64, base uint, is_signed bool, digits string) []byte {
// integer; interprets prec but not wid. Once formatted, result is sent to pad()
// and then flags are cleared.
func (f *fmt) integer(a int64, base uint64, signedness bool, digits string) {
var buf []byte = &f.intbuf;
negative := is_signed && a < 0;
negative := signedness == signed && a < 0;
if negative {
a = -a
}
......@@ -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);
for i > 0 && prec > (nByte-1-i) {
buf[i] = '0';
// format a into buf, ending at buf[i]. (printing is easier right-to-left.)
// a is made into unsigned ua. we could make things
// 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--;
buf[i] = '0';
}
// Various prefixes: 0x, -, etc.
if f.sharp {
switch base {
case 8:
if buf[i+1] != '0' {
buf[i] = '0';
if buf[i] != '0' {
i--;
buf[i] = '0';
}
case 16:
i--;
buf[i] = 'x' + digits[10] - 'a';
i--;
buf[i] = '0';
i--;
}
}
if negative {
buf[i] = '-';
i--;
buf[i] = '-';
} else if f.plus {
buf[i] = '+';
i--;
buf[i] = '+';
} else if f.space {
buf[i] = ' ';
i--;
buf[i] = ' ';
}
return buf[i+1 : nByte];
f.pad(buf[i:]);
}
// fmt_d64 formats an int64 in decimal.
func (f *fmt) fmt_d64(v int64) {
f.padBytes(f.integer(v, 10, true, ldigits));
f.clearflags();
}
func (f *fmt) fmt_d64(v int64) { f.integer(v, 10, signed, ldigits) }
// 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.
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.
func (f *fmt) fmt_ud64(v uint64) *fmt {
f.padBytes(f.integer(int64(v), 10, false, ldigits));
f.clearflags();
return f;
}
func (f *fmt) fmt_ud64(v uint64) { f.integer(int64(v), 10, unsigned, ldigits) }
// 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.
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.
func (f *fmt) fmt_x64(v int64) {
f.padBytes(f.integer(v, 16, true, ldigits));
f.clearflags();
}
func (f *fmt) fmt_x64(v int64) { f.integer(v, 16, signed, ldigits) }
// 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.
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.
func (f *fmt) fmt_ux64(v uint64) {
f.padBytes(f.integer(int64(v), 16, false, ldigits));
f.clearflags();
}
func (f *fmt) fmt_ux64(v uint64) { f.integer(int64(v), 16, unsigned, ldigits) }
// 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.
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.
func (f *fmt) fmt_X64(v int64) {
f.padBytes(f.integer(v, 16, true, udigits));
f.clearflags();
}
func (f *fmt) fmt_X64(v int64) { f.integer(v, 16, signed, udigits) }
// 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.
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.
func (f *fmt) fmt_uX64(v uint64) {
f.padBytes(f.integer(int64(v), 16, false, udigits));
f.clearflags();
}
func (f *fmt) fmt_uX64(v uint64) { f.integer(int64(v), 16, unsigned, udigits) }
// 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.
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.
func (f *fmt) fmt_o64(v int64) {
f.padBytes(f.integer(v, 8, true, ldigits));
f.clearflags();
}
func (f *fmt) fmt_o64(v int64) { f.integer(v, 8, signed, ldigits) }
// 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.
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.
func (f *fmt) fmt_uo64(v uint64) {
f.padBytes(f.integer(int64(v), 8, false, ldigits));
f.clearflags();
}
func (f *fmt) fmt_uo64(v uint64) { f.integer(int64(v), 8, unsigned, ldigits) }
// 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.
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.
func (f *fmt) fmt_b64(v uint64) {
f.padBytes(f.integer(int64(v), 2, false, ldigits));
f.clearflags();
}
func (f *fmt) fmt_b64(v uint64) { f.integer(int64(v), 2, unsigned, ldigits) }
// 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.
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.
func (f *fmt) fmt_c(v int) {
f.pad(string(v));
f.clearflags();
}
func (f *fmt) fmt_c(v int) { f.padString(string(v)) }
// fmt_s formats a string.
func (f *fmt) fmt_s(s string) {
......@@ -332,8 +319,7 @@ func (f *fmt) fmt_s(s string) {
s = s[0:f.prec]
}
}
f.pad(s);
f.clearflags();
f.padString(s);
}
// fmt_sx formats a string as a hexadecimal encoding of its bytes.
......@@ -347,8 +333,7 @@ func (f *fmt) fmt_sx(s string) {
t += string(ldigits[v>>4]);
t += string(ldigits[v&0xF]);
}
f.pad(t);
f.clearflags();
f.padString(t);
}
// fmt_sX formats a string as an uppercase hexadecimal encoding of its bytes.
......@@ -359,8 +344,7 @@ func (f *fmt) fmt_sX(s string) {
t += string(udigits[v>>4]);
t += string(udigits[v&0xF]);
}
f.pad(t);
f.clearflags();
f.padString(t);
}
// fmt_q formats a string as a double-quoted, escaped Go string constant.
......@@ -371,8 +355,7 @@ func (f *fmt) fmt_q(s string) {
} else {
quoted = strconv.Quote(s)
}
f.pad(quoted);
f.clearflags();
f.padString(quoted);
}
// floating-point
......@@ -384,12 +367,7 @@ func doPrec(f *fmt, def int) int {
return def;
}
func fmtString(f *fmt, s string) {
f.pad(s);
f.clearflags();
}
// Add a plus sign or space to the string if missing and required.
// Add a plus sign or space to the floating-point string representation if missing and required.
func (f *fmt) plusSpace(s string) {
if s[0] != '-' {
if f.plus {
......@@ -398,7 +376,7 @@ func (f *fmt) plusSpace(s string) {
s = " " + s
}
}
fmtString(f, s);
f.padString(s);
}
// 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,
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).
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
func (x *fmt) f(a float) {
......
......@@ -136,7 +136,6 @@ type GoStringer interface {
GoString() string;
}
const runeSelf = utf8.RuneSelf
const allocSize = 32
type pp struct {
......@@ -149,17 +148,25 @@ type pp struct {
// A leaky bucket of reusable pp structures.
var ppFree = make(chan *pp, 100)
// Allocate a new pp struct. Probably can grab the previous one from ppFree.
func newPrinter() *pp {
p, ok := <-ppFree;
if !ok {
p = new(pp)
}
p.buf.Reset();
p.fmt.init(&p.buf);
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 }
......@@ -182,7 +189,7 @@ func (p *pp) Flag(b int) bool {
}
func (p *pp) add(c int) {
if c < runeSelf {
if c < utf8.RuneSelf {
p.buf.WriteByte(byte(c))
} else {
w := utf8.EncodeRune(c, &p.runeBuf);
......@@ -250,8 +257,9 @@ func Sprint(a ...) string {
v := reflect.NewValue(a).(*reflect.StructValue);
p := newPrinter();
p.doprint(v, false, false);
s := p.buf.String();
p.free();
return p.buf.String();
return s;
}
// 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