Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
61e1fa01
Commit
61e1fa01
authored
Jun 29, 2019
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rft: object pool
parent
d38547d0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
10 deletions
+44
-10
bytes_buffer_pool.go
bytes/bytes_buffer_pool.go
+28
-10
bytes_buffer_pool_test.go
bytes/bytes_buffer_pool_test.go
+7
-0
slice_pool_test.go
bytes/slice_pool_test.go
+9
-0
No files found.
bytes/bytes_buffer_pool.go
View file @
61e1fa01
...
...
@@ -23,12 +23,18 @@ import (
)
var
(
defaultPool
BytesBuffer
Pool
defaultPool
*
Object
Pool
)
func
init
()
{
defaultPool
=
NewObjectPool
(
func
()
PoolObject
{
return
new
(
bytes
.
Buffer
)
})
}
// GetBytesBuffer returns bytes.Buffer from pool
func
GetBytesBuffer
()
*
bytes
.
Buffer
{
return
defaultPool
.
Get
()
return
defaultPool
.
Get
()
.
(
*
bytes
.
Buffer
)
}
// PutIoBuffer returns IoBuffer to pool
...
...
@@ -36,23 +42,35 @@ func PutBytesBuffer(buf *bytes.Buffer) {
defaultPool
.
Put
(
buf
)
}
// BytesBufferPool is bytes.Buffer Pool
type
BytesBufferPool
struct
{
// Pool object
type
PoolObject
interface
{
Reset
()
}
type
New
func
()
PoolObject
// Pool is bytes.Buffer Pool
type
ObjectPool
struct
{
New
New
pool
sync
.
Pool
}
func
NewObjectPool
(
n
New
)
*
ObjectPool
{
return
&
ObjectPool
{
New
:
n
}
}
// take returns *bytes.Buffer from Pool
func
(
p
*
BytesBufferPool
)
Get
()
*
bytes
.
Buffer
{
func
(
p
*
ObjectPool
)
Get
()
PoolObject
{
v
:=
p
.
pool
.
Get
()
if
v
==
nil
{
return
new
(
bytes
.
Buffer
)
return
p
.
New
(
)
}
return
v
.
(
*
bytes
.
Buffer
)
return
v
.
(
PoolObject
)
}
// give returns *byes.Buffer to Pool
func
(
p
*
BytesBufferPool
)
Put
(
buf
*
bytes
.
Buffer
)
{
buf
.
Reset
()
p
.
pool
.
Put
(
buf
)
func
(
p
*
ObjectPool
)
Put
(
o
PoolObject
)
{
o
.
Reset
()
p
.
pool
.
Put
(
o
)
}
bytes/bytes_buffer_pool_test.go
View file @
61e1fa01
...
...
@@ -12,4 +12,11 @@ func TestBytesBufferPool(t *testing.T) {
t
.
Error
(
"iobuffer len not match write bytes' size"
)
}
PutBytesBuffer
(
buf
)
buf2
:=
GetBytesBuffer
()
// https://go-review.googlesource.com/c/go/+/162919/
// before go 1.13, sync.Pool just reserves some objs before every gc and will be cleanup by gc.
// after Go 1.13, maybe there are many reserved objs after gc.
if
buf
!=
buf2
{
t
.
Errorf
(
"buf pointer %p != buf2 pointer %p"
,
buf
,
buf2
)
}
}
bytes/slice_pool_test.go
View file @
61e1fa01
...
...
@@ -18,6 +18,15 @@ func intN(n int) int {
return
rand
.
Intn
(
n
)
+
1
}
func
ExampleGetBytes
()
{
// Obtain a buffer from the pool.
bufPtr
:=
GetBytes
(
16
)
defer
PutBytes
(
bufPtr
)
buf
:=
*
bufPtr
copy
(
buf
,
[]
byte
(
"hello, world"
))
println
(
string
(
buf
))
}
func
TestSlicePoolSmallBytes
(
t
*
testing
.
T
)
{
pool
:=
NewSlicePool
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment