• Kirill Smelkov's avatar
    xfmt: Addons to fmt and strconv with focus on formatting text without allocations · 1aa677c8
    Kirill Smelkov authored
    Std fmt works ok unless you need to do text formatting in hot codepaths.
    There fmt becomes inappropriate as it is slow and does allocations on
    every formatting.
    
    strconv also does not have append routines for every needed case, e.g.
    there is no strconv.AppendRune, no strconv.AppendHex etc.
    
    So xfmt
    
    1. provides append routines for builtin types lacking in strconv
    
    2. introduces xfmt.Stringer interface which types can implement to hook
       into general formatting via xfmt.Append()
    
    3. provides xfmt.Buffer which is []byte with syntatic sugar for
       formatting in a way similar to printf: For example if in fmt speak
       you have
    
    	s := fmt.Sprintf("hello %q %d %x", "world", 1, []byte("data"))
    
       xfmt analog would be
    
    	buf := xfmt.Buffer{}
    	buf .S("hello ") .Q("world") .C(' ') .D(1) .C(' ') .Xb([]byte("data"))
    	s := buf.Bytes()
    
       and xfmt.Buffer can be reused several times via Buffer.Reset() .
    
    The above xfmt.Buffer usage is more uglier than fmt.Printf but much less uglier
    than direct strconv.Append* and friends calls, and works faster and without
    allocations compared to fmt.Printf:
    
    	BenchmarkXFmt/%c(0x41)-4                20000000                65.4 ns/op             1 B/op          1 allocs/op
    	BenchmarkXFmt/.Cb(0x41)-4               200000000                5.96 ns/op            0 B/op          0 allocs/op
    	BenchmarkXFmt/%c(-1)-4                  20000000                70.1 ns/op             3 B/op          1 allocs/op
    	BenchmarkXFmt/.C(-1)-4                  100000000               12.9 ns/op             0 B/op          0 allocs/op
    	BenchmarkXFmt/%c(66)-4                  20000000                65.8 ns/op             1 B/op          1 allocs/op
    	BenchmarkXFmt/.C(66)-4                  100000000               12.7 ns/op             0 B/op          0 allocs/op
    	BenchmarkXFmt/%c(1080)-4                20000000                67.2 ns/op             2 B/op          1 allocs/op
    	BenchmarkXFmt/.C(1080)-4                100000000               12.8 ns/op             0 B/op          0 allocs/op
    	BenchmarkXFmt/%c(8364)-4                20000000                69.4 ns/op             3 B/op          1 allocs/op
    	BenchmarkXFmt/.C(8364)-4                100000000               13.8 ns/op             0 B/op          0 allocs/op
    	BenchmarkXFmt/%c(65537)-4               20000000                70.5 ns/op             4 B/op          1 allocs/op
    	BenchmarkXFmt/.C(65537)-4               100000000               14.3 ns/op             0 B/op          0 allocs/op
    	BenchmarkXFmt/%s("hello")-4             20000000                72.3 ns/op             5 B/op          1 allocs/op
    	BenchmarkXFmt/.S("hello")-4             200000000                9.40 ns/op            0 B/op          0 allocs/op
    	...
    1aa677c8
fmt.go 4.93 KB