-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: mitigate big map issue of connections when garbage collecting (#460
) goos: darwin goarch: arm64 pkg: github.com/panjf2000/gnet/v2 β old β new β β sec/op β sec/op vs base β GC4El100k/Run-4-eventloop-100000-10 30.74m Β± 3% 19.68m Β± 10% -35.98% (p=0.000 n=10) GC4El200k/Run-4-eventloop-200000-10 63.64m Β± 3% 38.16m Β± 11% -40.04% (p=0.000 n=10) GC4El500k/Run-4-eventloop-500000-10 177.28m Β± 8% 95.21m Β± 4% -46.29% (p=0.000 n=10) geomean 70.26m 41.51m -40.92% β old β new β β B/op β B/op vs base β GC4El100k/Run-4-eventloop-100000-10 27.50 Β± 35% 25.50 Β± 33% ~ (p=0.423 n=10) GC4El200k/Run-4-eventloop-200000-10 27.50 Β± 53% 20.50 Β± 66% ~ (p=0.642 n=10) GC4El500k/Run-4-eventloop-500000-10 16.00 Β± ? 18.00 Β± ? ~ (p=0.357 n=10) geomean 22.96 21.11 -8.04% β old β new β β allocs/op β allocs/op vs base β GC4El100k/Run-4-eventloop-100000-10 0.000 Β± 0% 0.000 Β± 0% ~ (p=1.000 n=10) ΒΉ GC4El200k/Run-4-eventloop-200000-10 0.000 Β± 0% 0.000 Β± 0% ~ (p=1.000 n=10) ΒΉ GC4El500k/Run-4-eventloop-500000-10 0.000 Β± 0% 0.000 Β± 0% ~ (p=1.000 n=10) ΒΉ geomean Β² +0.00% Β² ΒΉ all samples are equal Β² summaries must be >0 to compute geomean Fixes #334 --------- Co-authored-by: Andy Pan <[email protected]>
- Loading branch information
1 parent
94412fb
commit dd48bac
Showing
27 changed files
with
1,475 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
name: Run tests with -tags=gc_opt | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- dev | ||
- 1.x | ||
paths-ignore: | ||
- '**.md' | ||
pull_request: | ||
branches: | ||
- master | ||
- dev | ||
- 1.x | ||
paths-ignore: | ||
- '**.md' | ||
|
||
env: | ||
GO111MODULE: on | ||
GOPROXY: "https://proxy.golang.org" | ||
|
||
jobs: | ||
lint: | ||
strategy: | ||
matrix: | ||
os: | ||
- ubuntu-latest | ||
- macos-latest | ||
#- windows-latest | ||
name: Run golangci-lint | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.17' | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup and run golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.52.2 | ||
args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot --timeout 5m | ||
test: | ||
needs: lint | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go: ['1.17', '1.18', '1.19', '1.20'] | ||
os: | ||
- ubuntu-latest | ||
- macos-latest | ||
- windows-latest | ||
name: Go ${{ matrix.go }} @ ${{ matrix.os }} | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: ${{ matrix.go }} | ||
|
||
- name: Print Go environment | ||
id: go-env | ||
run: | | ||
printf "Using go at: $(which go)\n" | ||
printf "Go version: $(go version)\n" | ||
printf "\n\nGo environment:\n\n" | ||
go env | ||
printf "\n\nSystem environment:\n\n" | ||
env | ||
# Calculate the short SHA1 hash of the git commit | ||
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | ||
echo "GO_CACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
- name: Cache go modules | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
${{ steps.go-env.outputs.GO_CACHE }} | ||
~/go/pkg/mod | ||
key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-${{ matrix.go }}-go-ci | ||
- name: Run unit tests for packages | ||
run: go test $(go list ./... | tail -n +2) | ||
|
||
- name: Run integration tests | ||
run: go test -v -race -tags=gc_opt -coverprofile="codecov.report" -covermode=atomic -timeout 10m -failfast | ||
|
||
- name: Upload the code coverage report to codecov.io | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
files: ./codecov.report | ||
flags: unittests | ||
name: codecov-gnet | ||
fail_ci_if_error: true | ||
verbose: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
name: Run tests with -tags=poll_opt,gc_opt | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
- dev | ||
- 1.x | ||
paths-ignore: | ||
- '**.md' | ||
pull_request: | ||
branches: | ||
- master | ||
- dev | ||
- 1.x | ||
paths-ignore: | ||
- '**.md' | ||
|
||
env: | ||
GO111MODULE: on | ||
GOPROXY: "https://proxy.golang.org" | ||
|
||
jobs: | ||
lint: | ||
strategy: | ||
matrix: | ||
os: | ||
- ubuntu-latest | ||
- macos-latest | ||
name: Run golangci-lint | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.17' | ||
|
||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
|
||
- name: Setup and run golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.52.2 | ||
args: -v -E gofumpt -E gocritic -E misspell -E revive -E godot | ||
test: | ||
needs: lint | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
go: ['1.17', '1.18', '1.19', '1.20'] | ||
os: [ubuntu-latest, macos-latest] | ||
name: Go ${{ matrix.go }} @ ${{ matrix.os }} | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v3 | ||
with: | ||
ref: ${{ github.ref }} | ||
|
||
- name: Setup Go | ||
uses: actions/setup-go@v3 | ||
with: | ||
go-version: '^1.17' | ||
|
||
- name: Print Go environment | ||
id: go-env | ||
run: | | ||
printf "Using go at: $(which go)\n" | ||
printf "Go version: $(go version)\n" | ||
printf "\n\nGo environment:\n\n" | ||
go env | ||
printf "\n\nSystem environment:\n\n" | ||
env | ||
# Calculate the short SHA1 hash of the git commit | ||
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT | ||
echo "GO_CACHE=$(go env GOCACHE)" >> $GITHUB_OUTPUT | ||
- name: Cache go modules | ||
uses: actions/cache@v3 | ||
with: | ||
path: | | ||
${{ steps.go-env.outputs.GO_CACHE }} | ||
~/go/pkg/mod | ||
key: ${{ runner.os }}-${{ matrix.go }}-go-ci-${{ hashFiles('**/go.sum') }} | ||
restore-keys: | | ||
${{ runner.os }}-${{ matrix.go }}-go-ci | ||
- name: Run unit tests for packages | ||
run: go test $(go list ./... | tail -n +2) | ||
|
||
- name: Run integration tests with -tags=poll_opt | ||
run: go test -v -tags=poll_opt,gc_opt -coverprofile="codecov.report" -covermode=atomic -timeout 10m -failfast | ||
|
||
- name: Upload the code coverage report to codecov.io | ||
uses: codecov/codecov-action@v3 | ||
with: | ||
files: ./codecov.report | ||
flags: unittests | ||
name: codecov-gnet-poll_opt | ||
fail_ci_if_error: true | ||
verbose: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Copyright (c) 2023 Andy Pan. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build (linux || freebsd || dragonfly || darwin) && !gc_opt | ||
// +build linux freebsd dragonfly darwin | ||
// +build !gc_opt | ||
|
||
package gnet | ||
|
||
import ( | ||
"sync/atomic" | ||
|
||
"github.com/panjf2000/gnet/v2/internal/gfd" | ||
) | ||
|
||
type connMatrix struct { | ||
connCount int32 | ||
connMap map[int]*conn | ||
} | ||
|
||
func (cm *connMatrix) init() { | ||
cm.connMap = make(map[int]*conn) | ||
} | ||
|
||
func (cm *connMatrix) iterate(f func(*conn) bool) { | ||
for _, c := range cm.connMap { | ||
if c != nil { | ||
if !f(c) { | ||
return | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (cm *connMatrix) incCount(_ int, delta int32) { | ||
atomic.AddInt32(&cm.connCount, delta) | ||
} | ||
|
||
func (cm *connMatrix) loadCount() (n int32) { | ||
return atomic.LoadInt32(&cm.connCount) | ||
} | ||
|
||
func (cm *connMatrix) addConn(c *conn, index int) { | ||
c.gfd = gfd.NewGFD(c.fd, index, 0, 0) | ||
cm.connMap[c.fd] = c | ||
cm.incCount(0, 1) | ||
} | ||
|
||
func (cm *connMatrix) delConn(c *conn) { | ||
delete(cm.connMap, c.fd) | ||
cm.incCount(0, -1) | ||
} | ||
|
||
func (cm *connMatrix) getConn(fd int) *conn { | ||
return cm.connMap[fd] | ||
} | ||
|
||
/* | ||
func (cm *connMatrix) getConnByGFD(fd gfd.GFD) *conn { | ||
return cm.connMap[fd.Fd()] | ||
} | ||
*/ |
Oops, something went wrong.