Skip to content

Commit

Permalink
2024-05-19 16:33
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverRainZ committed May 19, 2024
1 parent d59a522 commit 6933d9c
Show file tree
Hide file tree
Showing 16 changed files with 560 additions and 0 deletions.
9 changes: 9 additions & 0 deletions _assets/bsconv/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -x

for f in maplookup_test.go equal_test.go; do
go test -bench . -benchmem $f > ${f%_test.go}.txt
done

./bench_zerocopy.sh
./bench_strconv.sh
9 changes: 9 additions & 0 deletions _assets/bsconv/bench_strconv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -x

echo '// go1.22' > strconv.txt
go test -gcflags='-m' -bench . -benchmem ./strconv_test.go \
1>>strconv.txt 2>strconv122.log
echo '// go1.19' >> strconv.txt
go1.19 test -gcflags='-m' -bench . -benchmem ./strconv_test.go \
1>>strconv.txt 2>strconv119.log
6 changes: 6 additions & 0 deletions _assets/bsconv/bench_zerocopy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
echo '// zerocopy=1' > zerocopy.txt
go test -gcflags='-d zerocopy=1 -m' -bench . -benchmem ./zerocopy_test.go \
1>>zerocopy.txt 2>zerocopy1.log
echo '// zerocopy=0' >> zerocopy.txt
go test -gcflags='-d zerocopy=0 -m' -bench . -benchmem ./zerocopy_test.go \
1>>zerocopy.txt 2>zerocopy0.log
9 changes: 9 additions & 0 deletions _assets/bsconv/equal.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkEqual/Optimized-8 1000000000 0.2547 ns/op 0 B/op 0 allocs/op
BenchmarkEqual/NotOptimized-8 26003533 46.33 ns/op 160 B/op 2 allocs/op
BenchmarkEqual/Optimized2-8 382775796 3.174 ns/op 0 B/op 0 allocs/op
BenchmarkEqual/bytes.Equal-8 1000000000 0.2310 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 3.328s
34 changes: 34 additions & 0 deletions _assets/bsconv/equal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"bytes"
"testing"
)

func BenchmarkEqual(b *testing.B) {
bs1 := []byte("a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string")
bs2 := []byte("a looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong string2")

b.Run("Optimized", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = string(bs1) == string(bs2)
}
})
b.Run("NotOptimized", func(b *testing.B) {
for i := 0; i < b.N; i++ {
s1 := string(bs1)
s2 := string(bs2)
_ = s1 == s2
}
})
b.Run("Optimized2", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = string(bs1) >= string(bs2)
}
})
b.Run("bytes.Equal", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = bytes.Equal(bs1, bs2)
}
})
}
7 changes: 7 additions & 0 deletions _assets/bsconv/maplookup.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkMapLookup/Optimized-8 222915559 5.483 ns/op 0 B/op 0 allocs/op
BenchmarkMapLookup/NotOptimized-8 49391640 33.78 ns/op 48 B/op 1 allocs/op
PASS
ok command-line-arguments 3.465s
24 changes: 24 additions & 0 deletions _assets/bsconv/maplookup_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import "testing"

func BenchmarkMapLookup(b *testing.B) {
m := map[string]string{
"foo": "foo",
"bar": "bar",
"a log string": "a log string",
"another log string": "another log string",
}
var k = []byte("a looooooooooooooooooooong string")
b.Run("Optimized", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = m[string(k)]
}
})
b.Run("NotOptimized", func(b *testing.B) {
for i := 0; i < b.N; i++ {
sk := string(k)
_ = m[sk]
}
})
}
14 changes: 14 additions & 0 deletions _assets/bsconv/strconv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// go1.22
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkStrconv-8 86147509 12.70 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 1.111s
// go1.19
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkStrconv-8 51321450 21.73 ns/op 8 B/op 1 allocs/op
PASS
ok command-line-arguments 1.142s
9 changes: 9 additions & 0 deletions _assets/bsconv/strconv119.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# command-line-arguments [command-line-arguments.test]
./strconv_test.go:8:23: b does not escape
./strconv_test.go:9:13: []byte{...} does not escape
./strconv_test.go:11:33: string(s) escapes to heap
# command-line-arguments.test
_testmain.go:37:6: can inline init.0
_testmain.go:45:24: inlining call to testing.MainStart
_testmain.go:45:42: testdeps.TestDeps{} escapes to heap
_testmain.go:45:24: &testing.M{...} escapes to heap
9 changes: 9 additions & 0 deletions _assets/bsconv/strconv122.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# command-line-arguments [command-line-arguments.test]
./strconv_test.go:8:23: b does not escape
./strconv_test.go:9:13: []byte{...} does not escape
./strconv_test.go:11:34: string(s) does not escape
# command-line-arguments.test
_testmain.go:37:6: can inline init.0
_testmain.go:45:24: inlining call to testing.MainStart
_testmain.go:45:42: testdeps.TestDeps{} escapes to heap
_testmain.go:45:24: &testing.M{...} escapes to heap
13 changes: 13 additions & 0 deletions _assets/bsconv/strconv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"strconv"
"testing"
)

func BenchmarkStrconv(b *testing.B) {
s := []byte{50, 48, 50, 52, 48, 53}
for i := 0; i < b.N; i++ {
_, _ = strconv.ParseInt(string(s), 10, 0)
}
}
14 changes: 14 additions & 0 deletions _assets/bsconv/zerocopy.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// zerocopy=1
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkZeroCopy-8 1000000000 0.2157 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 0.241s
// zerocopy=0
goos: linux
goarch: amd64
cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
BenchmarkZeroCopy-8 418946652 3.056 ns/op 0 B/op 0 allocs/op
PASS
ok command-line-arguments 1.573s
9 changes: 9 additions & 0 deletions _assets/bsconv/zerocopy0.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# command-line-arguments [command-line-arguments.test]
./zerocopy_test.go:7:6: can inline BenchmarkZeroCopy
./zerocopy_test.go:7:24: b does not escape
./zerocopy_test.go:10:14: ([]byte)(s) does not escape
# command-line-arguments.test
_testmain.go:37:6: can inline init.0
_testmain.go:45:24: inlining call to testing.MainStart
_testmain.go:45:42: testdeps.TestDeps{} escapes to heap
_testmain.go:45:24: &testing.M{...} escapes to heap
10 changes: 10 additions & 0 deletions _assets/bsconv/zerocopy1.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# command-line-arguments [command-line-arguments.test]
./zerocopy_test.go:7:6: can inline BenchmarkZeroCopy
./zerocopy_test.go:7:24: b does not escape
./zerocopy_test.go:10:14: ([]byte)(s) does not escape
./zerocopy_test.go:10:14: zero-copy string->[]byte conversion
# command-line-arguments.test
_testmain.go:37:6: can inline init.0
_testmain.go:45:24: inlining call to testing.MainStart
_testmain.go:45:42: testdeps.TestDeps{} escapes to heap
_testmain.go:45:24: &testing.M{...} escapes to heap
12 changes: 12 additions & 0 deletions _assets/bsconv/zerocopy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package main

import (
"testing"
)

func BenchmarkZeroCopy(b *testing.B) {
s := "202405"
for i := 0; i < b.N; i++ {
_ = []byte(s)
}
}
Loading

0 comments on commit 6933d9c

Please sign in to comment.