Codebase list golang-github-vbauerster-mpb / d7d5bd1
size and percentage decorators share same pool of bytes This optimization should reduce overall number of allocations, especially with multiple bars. Vladimir Bauer 3 years ago
5 changed file(s) with 42 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
+0
-10
decor/optimistic_string_writer.go less more
0 package decor
1
2 import "io"
3
4 func mustWriteString(w io.Writer, s string) {
5 _, err := io.WriteString(w, s)
6 if err != nil {
7 panic(err)
8 }
9 }
2222 }
2323 }
2424
25 mustWriteString(st, strconv.FormatFloat(float64(s), 'f', prec, 64))
25 p := bytePool.Get().(*[]byte)
26 b := strconv.AppendFloat(*p, float64(s), 'f', prec, 64)
2627 if st.Flag(' ') {
27 mustWriteString(st, " ")
28 b = append(b, ' ', '%')
29 } else {
30 b = append(b, '%')
2831 }
29 mustWriteString(st, "%")
32 _, err := st.Write(b)
33 bytePool.Put(p)
34 if err != nil {
35 panic(err)
36 }
3037 }
3138
3239 // Percentage returns percentage decorator. It's a wrapper of NewPercentage.
0 package decor
1
2 import "sync"
3
4 var bytePool = sync.Pool{
5 New: func() interface{} {
6 b := make([]byte, 0, 16)
7 return &b
8 },
9 }
4848 unit = _iTiB
4949 }
5050
51 mustWriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
51 p := bytePool.Get().(*[]byte)
52 b := strconv.AppendFloat(*p, float64(self)/float64(unit), 'f', prec, 64)
5253 if st.Flag(' ') {
53 mustWriteString(st, " ")
54 b = append(b, ' ')
5455 }
55 mustWriteString(st, unit.String())
56 b = append(b, []byte(unit.String())...)
57 _, err := st.Write(b)
58 bytePool.Put(p)
59 if err != nil {
60 panic(err)
61 }
5662 }
5763
5864 const (
96102 unit = _TB
97103 }
98104
99 mustWriteString(st, strconv.FormatFloat(float64(self)/float64(unit), 'f', prec, 64))
105 p := bytePool.Get().(*[]byte)
106 b := strconv.AppendFloat(*p, float64(self)/float64(unit), 'f', prec, 64)
100107 if st.Flag(' ') {
101 mustWriteString(st, " ")
108 b = append(b, ' ')
102109 }
103 mustWriteString(st, unit.String())
110 b = append(b, []byte(unit.String())...)
111 _, err := st.Write(b)
112 bytePool.Put(p)
113 if err != nil {
114 panic(err)
115 }
104116 }
2121
2222 func (self *speedFormatter) Format(st fmt.State, verb rune) {
2323 self.Formatter.Format(st, verb)
24 mustWriteString(st, "/s")
24 _, err := st.Write([]byte("/s"))
25 if err != nil {
26 panic(err)
27 }
2528 }
2629
2730 // EwmaSpeed exponential-weighted-moving-average based speed decorator.