diff --git a/pkg/util/localpool/BUILD.bazel b/pkg/util/localpool/BUILD.bazel deleted file mode 100644 index 9affa37f9b86b..0000000000000 --- a/pkg/util/localpool/BUILD.bazel +++ /dev/null @@ -1,29 +0,0 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") - -go_library( - name = "localpool", - srcs = [ - "localpool.go", - "localpool_norace.go", - "localpool_race.go", - ], - importpath = "github.com/pingcap/tidb/pkg/util/localpool", - visibility = ["//visibility:public"], -) - -go_test( - name = "localpool_test", - timeout = "short", - srcs = [ - "localpool_test.go", - "main_test.go", - ], - embed = [":localpool"], - flaky = True, - deps = [ - "//pkg/testkit/testsetup", - "//pkg/util", - "@com_github_stretchr_testify//require", - "@org_uber_go_goleak//:goleak", - ], -) diff --git a/pkg/util/localpool/localpool.go b/pkg/util/localpool/localpool.go deleted file mode 100644 index c538bff63df4e..0000000000000 --- a/pkg/util/localpool/localpool.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2020 PingCAP, Inc. -// -// 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. - -package localpool - -import ( - "runtime" - _ "unsafe" // required by go:linkname -) - -// LocalPool is an thread local object pool. -// It's similar to sync.Pool but has some difference -// - It can define the size of the pool. -// - It never get GCed. -type LocalPool struct { - newFn func() any - resetFn func(obj any) - slots []*slot - sizePerProc int -} - -type slot struct { - objs []any - getHit int - getMiss int - putHit int - putMiss int -} - -// NewLocalPool creates a pool. -// The sizePerProc is pool size for each PROC, so total pool size is (GOMAXPROCS * sizePerProc) -// It can only be used when the GOMAXPROCS never change after the pool created. -// newFn is the function to create a new object. -// resetFn is the function called before put back to the pool, it can be nil. -func NewLocalPool(sizePerProc int, newFn func() any, resetFn func(obj any)) *LocalPool { - slots := make([]*slot, runtime.GOMAXPROCS(0)) - for i := 0; i < len(slots); i++ { - slots[i] = &slot{ - objs: make([]any, 0, sizePerProc), - } - } - return &LocalPool{ - sizePerProc: sizePerProc, - slots: slots, - newFn: newFn, - resetFn: resetFn, - } -} - -//go:linkname procPin runtime.procPin -func procPin() int - -//go:linkname procUnpin runtime.procUnpin -func procUnpin() int diff --git a/pkg/util/localpool/localpool_norace.go b/pkg/util/localpool/localpool_norace.go deleted file mode 100644 index 5a40667fa123f..0000000000000 --- a/pkg/util/localpool/localpool_norace.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2020 PingCAP, Inc. -// -// 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 !race - -package localpool - -// Get gets an object from the pool. -func (p *LocalPool) Get() any { - pid := procPin() - slot := p.slots[pid] - objLen := len(slot.objs) - var result any - if objLen > 0 { - lastIdx := objLen - 1 - result = slot.objs[lastIdx] - slot.objs[lastIdx] = nil - slot.objs = slot.objs[:lastIdx] - slot.getHit++ - } else { - slot.getMiss++ - } - procUnpin() - if result == nil { - result = p.newFn() - } - return result -} - -// Put puts an object back to the pool. -// It returns true if the pool is not full and the obj is successfully put into the pool. -func (p *LocalPool) Put(obj any) bool { - if p.resetFn != nil { - p.resetFn(obj) - } - var ok bool - pid := procPin() - slot := p.slots[pid] - if len(slot.objs) < p.sizePerProc { - slot.objs = append(slot.objs, obj) - slot.putHit++ - ok = true - } else { - slot.putMiss++ - } - procUnpin() - return ok -} diff --git a/pkg/util/localpool/localpool_race.go b/pkg/util/localpool/localpool_race.go deleted file mode 100644 index 8f4a189c5faea..0000000000000 --- a/pkg/util/localpool/localpool_race.go +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright 2020 PingCAP, Inc. -// -// 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 race - -package localpool - -// Get gets an object from the pool. -func (p *LocalPool) Get() any { - return p.newFn() -} - -// Put puts an object back to the pool. -func (p *LocalPool) Put(obj any) bool { - return false -} diff --git a/pkg/util/localpool/localpool_test.go b/pkg/util/localpool/localpool_test.go deleted file mode 100644 index 4e69c6e3121fe..0000000000000 --- a/pkg/util/localpool/localpool_test.go +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2020 PingCAP, Inc. -// -// 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 !race - -package localpool - -import ( - "math/rand" - "runtime" - "testing" - - "github.com/pingcap/tidb/pkg/util" - "github.com/stretchr/testify/require" -) - -type Obj struct { - // nolint:unused - val int64 // nolint:structcheck // Dummy field to make it non-empty. -} - -func TestPool(t *testing.T) { - numWorkers := runtime.GOMAXPROCS(0) - wg := new(util.WaitGroupWrapper) - pool := NewLocalPool(16, func() any { - return new(Obj) - }, nil) - n := 1000 - for i := 0; i < numWorkers; i++ { - wg.Run(func() { - for j := 0; j < n; j++ { - obj := pool.Get().(*Obj) - obj.val = rand.Int63() - pool.Put(obj) - } - }) - } - wg.Wait() - var getHit, getMiss, putHit, putMiss int - for _, slot := range pool.slots { - getHit += slot.getHit - getMiss += slot.getMiss - putHit += slot.putHit - putMiss += slot.putMiss - } - require.Greater(t, getHit, getMiss) - require.Greater(t, putHit, putMiss) -} diff --git a/pkg/util/localpool/main_test.go b/pkg/util/localpool/main_test.go deleted file mode 100644 index cabecfab09ba7..0000000000000 --- a/pkg/util/localpool/main_test.go +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2021 PingCAP, Inc. -// -// 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. - -package localpool - -import ( - "testing" - - "github.com/pingcap/tidb/pkg/testkit/testsetup" - "go.uber.org/goleak" -) - -func TestMain(m *testing.M) { - testsetup.SetupForCommonTest() - opts := []goleak.Option{ - goleak.IgnoreTopFunction("github.com/golang/glog.(*fileSink).flushDaemon"), - goleak.IgnoreTopFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"), - goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"), - goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"), - } - goleak.VerifyTestMain(m, opts...) -}