You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, the Srender and Sprint method of some components uses "+=" to add the result. The string returned by Srender would be very long, and using "+=" will waste some memory.
strings.Builder is specially designed to handle such scenarios. strings.Builder was introduced in Go 1.10, and its doc says "A Builder is used to efficiently build a string".
I did a simple benchmark using Barchart as an example:
$ go test -run='^$' -bench='BenchmarkBarchart' -benchmem -count=10 > old.bench
# change `var ret string` to `var ret strings.Builder`
$ go test -run='^$' -bench='BenchmarkBarchart' -benchmem -count=10 > new.bench
$ benchstat old.bench new.bench
goos: windows
goarch: amd64
pkg: github.com/pterm/pterm
cpu: Intel(R) Core(TM) i5-10200H CPU @ 2.40GHz
│ old.bench │ new.bench │
│ sec/op │ sec/op vs base │
Barchart-8 97.84m ± 7% 94.15m ± 4% ~ (p=0.123 n=10)
│ old.bench │ new.bench │
│ B/op │ B/op vs base │
Barchart-8 5.803Mi ± 8% 4.179Mi ± 7% -27.99% (p=0.000 n=10)
│ old.bench │ new.bench │
│ allocs/op │ allocs/op vs base │
Barchart-8 9.902k ± 0% 9.501k ± 0% -4.05% (p=0.000 n=10)
Using strings.Buildercan save 28% of memory and reduce 4% allocations. There is no obvious change in the speed of Srender (in fact, the speed can become faster after reducing the number of memory allocations)
The changes of code is also quite simple:
func (p BarChartPrinter) Srender() (string, error) {
- var ret string+ var ret strings.Builder
...
for i := 0; i <= maxBarHeight; i++ {
for _, barString := range renderedBars {
...
- ret += barLine+ ret.WriteString(barLine)
}
- ret += "\n"+ ret.WriteByte('\n')
}
- return ret, nil+ return ret.String(), nil
}
Code that needs to be modified: Srender and Sprint methods that require a lot of string concatenation. All modifications can be seen in the related PR submitted later.
These modifications will not change the existing APIs. It is a simple and harmless refactoring.
I hope you would take the time to review this proposal.
The text was updated successfully, but these errors were encountered:
Currently, the
Srender
andSprint
method of some components uses "+=" to add the result. The string returned bySrender
would be very long, and using "+=" will waste some memory.strings.Builder
is specially designed to handle such scenarios.strings.Builder
was introduced in Go 1.10, and its doc says "A Builder is used to efficiently build a string".I did a simple benchmark using Barchart as an example:
and this is the result:
Using
strings.Builder
can save 28% of memory and reduce 4% allocations. There is no obvious change in the speed ofSrender
(in fact, the speed can become faster after reducing the number of memory allocations)The changes of code is also quite simple:
Code that needs to be modified:
Srender
andSprint
methods that require a lot of string concatenation. All modifications can be seen in the related PR submitted later.These modifications will not change the existing APIs. It is a simple and harmless refactoring.
I hope you would take the time to review this proposal.
The text was updated successfully, but these errors were encountered: