Skip to content

Commit

Permalink
opt: bump up minimum required Go version to 1.20
Browse files Browse the repository at this point in the history
Employ the standard approach of string<->bytes conversion
  • Loading branch information
panjf2000 committed Sep 21, 2024
1 parent 18f54e3 commit 1f96187
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cross-compile-bsd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test_gc_opt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test_poll_opt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test_poll_opt_gc_opt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 }}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a title="Build Status" target="_blank" href="https://github.com/panjf2000/gnet/actions?query=workflow%3ATests"><img src="https://img.shields.io/github/actions/workflow/status/panjf2000/gnet/test.yml?branch=dev&style=flat-square&logo=github-actions" /></a>
<a title="Codecov" target="_blank" href="https://codecov.io/gh/panjf2000/gnet"><img src="https://img.shields.io/codecov/c/github/panjf2000/gnet?style=flat-square&logo=codecov" /></a>
<a title="Supported Platforms" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20*BSD%20%7C%20Windows-549688?style=flat-square&logo=launchpad" /></a>
<a title="Require Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.17-30dff3?style=flat-square&logo=go" /></a>
<a title="Minimum Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.20-30dff3?style=flat-square&logo=go" /></a>
<br />
<a title="Go Report Card" target="_blank" href="https://goreportcard.com/report/github.com/panjf2000/gnet"><img src="https://goreportcard.com/badge/github.com/panjf2000/gnet?style=flat-square" /></a>
<a title="Doc for gnet" target="_blank" href="https://pkg.go.dev/github.com/panjf2000/gnet/v2#section-documentation"><img src="https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs" /></a>
Expand Down
2 changes: 1 addition & 1 deletion README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<a title="Build Status" target="_blank" href="https://github.com/panjf2000/gnet/actions?query=workflow%3ATests"><img src="https://img.shields.io/github/actions/workflow/status/panjf2000/gnet/test.yml?branch=dev&style=flat-square&logo=github-actions" /></a>
<a title="Codecov" target="_blank" href="https://codecov.io/gh/panjf2000/gnet"><img src="https://img.shields.io/codecov/c/github/panjf2000/gnet?style=flat-square&logo=codecov" /></a>
<a title="Supported Platforms" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/platform-Linux%20%7C%20macOS%20%7C%20*BSD%20%7C%20Windows-549688?style=flat-square&logo=launchpad" /></a>
<a title="Require Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.17-30dff3?style=flat-square&logo=go" /></a>
<a title="Minimum Go Version" target="_blank" href="https://github.com/panjf2000/gnet"><img src="https://img.shields.io/badge/go-%3E%3D1.20-30dff3?style=flat-square&logo=go" /></a>
<br />
<a title="Go Report Card" target="_blank" href="https://goreportcard.com/report/github.com/panjf2000/gnet"><img src="https://goreportcard.com/badge/github.com/panjf2000/gnet?style=flat-square" /></a>
<a title="Doc for gnet" target="_blank" href="https://pkg.go.dev/github.com/panjf2000/gnet/v2#section-documentation"><img src="https://img.shields.io/badge/go.dev-doc-007d9c?style=flat-square&logo=read-the-docs" /></a>
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ require (
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.17
go 1.20
26 changes: 5 additions & 21 deletions internal/bs/bs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
17 changes: 5 additions & 12 deletions pkg/pool/byteslice/byteslice.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package byteslice
import (
"math"
"math/bits"
"reflect"
"runtime"
"sync"
"unsafe"
)
Expand All @@ -41,24 +39,19 @@ 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
}
if size > math.MaxInt32 {
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<<idx)[:size]
return make([]byte, size, 1<<idx)
}
sh := (*reflect.SliceHeader)(unsafe.Pointer(&buf)) //nolint:staticcheck
sh.Data = uintptr(ptr)
sh.Len = size
sh.Cap = 1 << idx
runtime.KeepAlive(ptr)
return
return unsafe.Slice(ptr, 1<<idx)[:size]
}

// Put returns the byte slice to the pool.
Expand All @@ -72,7 +65,7 @@ func (p *Pool) Put(buf []byte) {
idx--
}
// array pointer
p.pools[idx].Put(unsafe.Pointer(&buf[:1][0]))
p.pools[idx].Put(unsafe.SliceData(buf))
}

func index(n uint32) uint32 {
Expand Down

0 comments on commit 1f96187

Please sign in to comment.