From 29dca4f6d71e293e84bd9adea919548ce72918ce Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 22 Sep 2024 00:00:24 +0800 Subject: [PATCH] opt: bump up minimum required Go version to 1.20 (#638) Employ the standard approach of string<->bytes conversion --- .github/workflows/cross-compile-bsd.yml | 2 +- .github/workflows/test.yml | 4 ++-- .github/workflows/test_gc_opt.yml | 4 ++-- .github/workflows/test_poll_opt.yml | 6 ++--- .github/workflows/test_poll_opt_gc_opt.yml | 6 ++--- README.md | 2 +- README_ZH.md | 2 +- go.mod | 6 ++--- go.sum | 10 ++++----- internal/bs/bs.go | 26 +++++----------------- pkg/pool/byteslice/byteslice.go | 20 ++++++----------- 11 files changed, 32 insertions(+), 56 deletions(-) diff --git a/.github/workflows/cross-compile-bsd.yml b/.github/workflows/cross-compile-bsd.yml index 0e782a704..e91a22672 100644 --- a/.github/workflows/cross-compile-bsd.yml +++ b/.github/workflows/cross-compile-bsd.yml @@ -31,7 +31,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest name: Go ${{ matrix.go }} @ ${{ matrix.os }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 752c62135..3e964a415 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -43,7 +43,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint @@ -56,7 +56,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_gc_opt.yml b/.github/workflows/test_gc_opt.yml index a494a1589..df99ae8e2 100644 --- a/.github/workflows/test_gc_opt.yml +++ b/.github/workflows/test_gc_opt.yml @@ -43,7 +43,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint @@ -56,7 +56,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: - ubuntu-latest - macos-latest diff --git a/.github/workflows/test_poll_opt.yml b/.github/workflows/test_poll_opt.yml index 22991b967..5d5ae19fc 100644 --- a/.github/workflows/test_poll_opt.yml +++ b/.github/workflows/test_poll_opt.yml @@ -42,7 +42,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint @@ -55,7 +55,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -68,7 +68,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' - name: Print Go environment id: go-env diff --git a/.github/workflows/test_poll_opt_gc_opt.yml b/.github/workflows/test_poll_opt_gc_opt.yml index 1d658f449..a5aef4062 100644 --- a/.github/workflows/test_poll_opt_gc_opt.yml +++ b/.github/workflows/test_poll_opt_gc_opt.yml @@ -42,7 +42,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' cache: false - name: Setup and run golangci-lint @@ -55,7 +55,7 @@ jobs: strategy: fail-fast: false matrix: - go: ['1.17', '1.22'] + go: ['1.20', '1.23'] os: [ubuntu-latest, macos-latest] name: Go ${{ matrix.go }} @ ${{ matrix.os }} runs-on: ${{ matrix.os }} @@ -68,7 +68,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v5 with: - go-version: '^1.17' + go-version: '^1.20' - name: Print Go environment id: go-env diff --git a/README.md b/README.md index bdba1a6f3..8c85967b3 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - +
diff --git a/README_ZH.md b/README_ZH.md index fe5a4f28f..d166d1b76 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -4,7 +4,7 @@ - +
diff --git a/go.mod b/go.mod index 178d229c9..0ab63f761 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ require ( github.com/stretchr/testify v1.9.0 github.com/valyala/bytebufferpool v1.0.0 go.uber.org/zap v1.21.0 // don't upgrade this one - golang.org/x/sync v0.7.0 - golang.org/x/sys v0.21.0 + golang.org/x/sync v0.8.0 + golang.org/x/sys v0.25.0 gopkg.in/natefinch/lumberjack.v2 v2.2.1 ) @@ -18,4 +18,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect ) -go 1.17 +go 1.20 diff --git a/go.sum b/go.sum index 7ba46041c..6c36aa9be 100644 --- a/go.sum +++ b/go.sum @@ -17,13 +17,11 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -48,15 +46,15 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= +golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= -golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34= +golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= diff --git a/internal/bs/bs.go b/internal/bs/bs.go index 16b3861f7..7afb5136e 100644 --- a/internal/bs/bs.go +++ b/internal/bs/bs.go @@ -15,31 +15,15 @@ package bs import ( - "reflect" "unsafe" ) -// TODO(panjf2000): rework the implementation of BytesToString and StringToBytes by -// using unsafe.String/unsafe.StringData and unsafe.Slice/unsafe.SliceData when we -// bump up the minimum required Go version to 1.20. - -// BytesToString converts byte slice to a string without memory allocation. -// -// Note it may break if the implementation of string or slice header changes in the future go versions. +// BytesToString converts byte slice to a string without any memory allocation. func BytesToString(b []byte) string { - /* #nosec G103 */ - return *(*string)(unsafe.Pointer(&b)) + return unsafe.String(unsafe.SliceData(b), len(b)) } -// StringToBytes converts string to a byte slice without memory allocation. -// -// Note it may break if the implementation of string or slice header changes in the future go versions. -func StringToBytes(s string) (b []byte) { - /* #nosec G103 */ - sh := (*reflect.StringHeader)(unsafe.Pointer(&s)) //nolint:staticcheck - /* #nosec G103 */ - bh := (*reflect.SliceHeader)(unsafe.Pointer(&b)) //nolint:staticcheck - - bh.Data, bh.Len, bh.Cap = sh.Data, sh.Len, sh.Len - return b +// StringToBytes converts string to a byte slice without any memory allocation. +func StringToBytes(s string) []byte { + return unsafe.Slice(unsafe.StringData(s), len(s)) } diff --git a/pkg/pool/byteslice/byteslice.go b/pkg/pool/byteslice/byteslice.go index efd063375..53397dac6 100644 --- a/pkg/pool/byteslice/byteslice.go +++ b/pkg/pool/byteslice/byteslice.go @@ -17,8 +17,6 @@ package byteslice import ( "math" "math/bits" - "reflect" - "runtime" "sync" "unsafe" ) @@ -41,7 +39,7 @@ func Put(buf []byte) { } // Get retrieves a byte slice of the length requested by the caller from pool or allocates a new one. -func (p *Pool) Get(size int) (buf []byte) { +func (p *Pool) Get(size int) []byte { if size <= 0 { return nil } @@ -49,16 +47,11 @@ func (p *Pool) Get(size int) (buf []byte) { return make([]byte, size) } idx := index(uint32(size)) - ptr, _ := p.pools[idx].Get().(unsafe.Pointer) + ptr, _ := p.pools[idx].Get().(*byte) if ptr == nil { - return make([]byte, 1<