From c3ac38cec5fabce81473dd60511aedd813806248 Mon Sep 17 00:00:00 2001 From: guonaihong Date: Sun, 19 May 2024 20:18:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ebig=20pool=EF=BC=8C=20?= =?UTF-8?q?=E5=90=8E=E7=BB=AD=E7=89=88=E6=9C=AC=E4=BC=98=E5=8C=96=E4=B8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bytespool/bytes_big_buf.go | 39 ++++++++++++++++ bytespool/bytes_pool_noarena_test.go | 66 ---------------------------- 2 files changed, 39 insertions(+), 66 deletions(-) create mode 100644 bytespool/bytes_big_buf.go delete mode 100644 bytespool/bytes_pool_noarena_test.go diff --git a/bytespool/bytes_big_buf.go b/bytespool/bytes_big_buf.go new file mode 100644 index 0000000..2f38f69 --- /dev/null +++ b/bytespool/bytes_big_buf.go @@ -0,0 +1,39 @@ +// Copyright 2023-2024 antlabs. All rights reserved. +// +// 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 bytespool + +import "sync" + +// TODO: 启用, 目前暂未使用 +var bigPool = sync.Pool{ + New: func() interface{} { + buf := make([]byte, 0, 1024*256) + return &buf + }, +} + +func getBigPayload(n int) (rv *[]byte) { + rv = bigPool.Get().(*[]byte) + if cap(*rv) < n { + tmp := make([]byte, n) + *rv = tmp + } else { + *rv = (*rv)[:n] + } + return rv +} + +func putBigPayload(buf *[]byte) { + bigPool.Put(buf) +} diff --git a/bytespool/bytes_pool_noarena_test.go b/bytespool/bytes_pool_noarena_test.go deleted file mode 100644 index e7184e0..0000000 --- a/bytespool/bytes_pool_noarena_test.go +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2021-2023 antlabs. All rights reserved. -// -// 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 !goexperiment.arenas - -package bytespool - -import ( - "testing" - "unsafe" - - "github.com/antlabs/wsutil/enum" -) - -func Test_Index(t *testing.T) { - for i := 0; i <= 1024+enum.MaxFrameHeaderSize; i++ { - i2 := i - if i2 >= enum.MaxFrameHeaderSize { - i2 -= (enum.MaxFrameHeaderSize + 1) - } - index := selectIndex(i2) - if index != 0 { - t.Fatal("index error") - } - - } - - for i := 1024 + enum.MaxFrameHeaderSize + 1; i <= 2*1024+enum.MaxFrameHeaderSize; i++ { - i2 := i - i2 -= (enum.MaxFrameHeaderSize + 1) - index := selectIndex(i2) - if index != 1 { - t.Fatal("index error") - } - } - - for i := 1024*2 + enum.MaxFrameHeaderSize + 1; i <= 3*1024+enum.MaxFrameHeaderSize; i++ { - i2 := i - i2 -= (enum.MaxFrameHeaderSize + 1) - index := selectIndex(i2) - if index != 2 { - t.Fatal("index error") - } - } -} - -func Test_GetBytes_Address(t *testing.T) { - var m map[unsafe.Pointer]bool - for i := 0; i < 10; i++ { - p := GetBytes(1) - if m[unsafe.Pointer(p)] { - t.Fatal("duplicate pointer") - } - } -}