Skip to content

Commit 0776adb

Browse files
committed
reduce allocations in From from 64 to 8 via preallocating the numBuffer
This change improves the runtime by around 5-7% across the board After the change: goos: linux goarch: amd64 pkg: github.com/xnacly/go-iso8601-duration cpu: AMD Ryzen 7 3700X 8-Core Processor BenchmarkDuration/P0D-16 24741985 48.10 ns/op 8 B/op 1 allocs/op BenchmarkDuration/PT15H-16 18533790 63.26 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P1W-16 24164163 48.64 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P15W-16 20793126 58.09 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P1Y15W-16 13904265 84.21 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P15Y-16 19943671 59.35 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P15Y3M-16 14227842 83.24 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P15Y3M41D-16 10262575 115.6 ns/op 8 B/op 1 allocs/op BenchmarkDuration/PT15M-16 18228138 63.90 ns/op 8 B/op 1 allocs/op BenchmarkDuration/PT15M10S-16 12289743 95.84 ns/op 8 B/op 1 allocs/op BenchmarkDuration/P3Y6M4DT12H30M5S-16 6758479 177.3 ns/op 8 B/op 1 allocs/op PASS ok github.com/xnacly/go-iso8601-duration 13.952s Before the change: goos: linux goarch: amd64 pkg: github.com/xnacly/go-iso8601-duration cpu: AMD Ryzen 7 3700X 8-Core Processor BenchmarkDuration/P0D-16 18941151 64.26 ns/op 64 B/op 1 allocs/op BenchmarkDuration/PT15H-16 15597309 76.69 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P1W-16 18366788 64.10 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P15W-16 15964773 74.35 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P1Y15W-16 12367951 95.39 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P15Y-16 15903548 74.44 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P15Y3M-16 12397192 96.33 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P15Y3M41D-16 9608250 123.7 ns/op 64 B/op 1 allocs/op BenchmarkDuration/PT15M-16 15343340 76.61 ns/op 64 B/op 1 allocs/op BenchmarkDuration/PT15M10S-16 11336277 104.8 ns/op 64 B/op 1 allocs/op BenchmarkDuration/P3Y6M4DT12H30M5S-16 6286371 188.7 ns/op 64 B/op 1 allocs/op PASS ok github.com/xnacly/go-iso8601-duration 14.159s
1 parent f2e22cc commit 0776adb

2 files changed

Lines changed: 11 additions & 3 deletions

File tree

duration.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,7 @@ func From(s string) (Duration, error) {
128128

129129
curState := stateStart
130130
var col uint8
131-
numBuf := bytes.Buffer{}
132-
131+
numBuf := *bytes.NewBuffer(make([]byte, 0, 8))
133132
r := strings.NewReader(s)
134133

135134
for {

duration_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ var testcases = []struct {
6363
}{
6464
{"P0D", Duration{}},
6565
{"PT15H", Duration{hour: 15}},
66-
{"PT15H", Duration{hour: 15}},
6766
{"P1W", Duration{week: 1}},
6867
{"P15W", Duration{week: 15}},
6968
{"P1Y15W", Duration{year: 1, week: 15}},
@@ -85,6 +84,16 @@ var testcases = []struct {
8584
},
8685
}
8786

87+
func BenchmarkDuration(b *testing.B) {
88+
for _, i := range testcases {
89+
b.Run(i.str, func(b *testing.B) {
90+
for n := 0; n < b.N; n++ {
91+
_, _ = From(i.str)
92+
}
93+
})
94+
}
95+
}
96+
8897
func TestDurationStringer(t *testing.T) {
8998
for _, i := range testcases {
9099
t.Run(i.str, func(t *testing.T) {

0 commit comments

Comments
 (0)