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
a57121fa
Commit
a57121fa
authored
Jan 04, 2020
by
huiren
Committed by
望哥
Jan 04, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bytes_pools with buf ptr (#19)
parent
e1a65e20
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
16 additions
and
13 deletions
+16
-13
bytes_pool.go
bytes/bytes_pool.go
+13
-9
slice_pool.go
bytes/slice_pool.go
+3
-4
No files found.
bytes/bytes_pool.go
View file @
a57121fa
...
...
@@ -40,7 +40,8 @@ func NewBytesPool(slotSize []int) *BytesPool {
for
i
,
size
:=
range
bp
.
sizes
{
size
:=
size
bp
.
slots
[
i
]
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
return
make
([]
byte
,
0
,
size
)
buf
:=
make
([]
byte
,
0
,
size
)
return
&
buf
}}
}
return
bp
...
...
@@ -61,28 +62,31 @@ func (bp *BytesPool) findIndex(size int) int {
}
// AcquireBytes get specific make([]byte, 0, size)
func
(
bp
*
BytesPool
)
AcquireBytes
(
size
int
)
[]
byte
{
func
(
bp
*
BytesPool
)
AcquireBytes
(
size
int
)
*
[]
byte
{
idx
:=
bp
.
findIndex
(
size
)
if
idx
>=
bp
.
length
{
return
make
([]
byte
,
0
,
size
)
buf
:=
make
([]
byte
,
0
,
size
)
return
&
buf
}
return
bp
.
slots
[
idx
]
.
Get
()
.
([]
byte
)[
:
size
]
bufp
:=
bp
.
slots
[
idx
]
.
Get
()
.
(
*
[]
byte
)
buf
:=
(
*
bufp
)[
:
size
]
return
&
buf
}
// ReleaseBytes ...
func
(
bp
*
BytesPool
)
ReleaseBytes
(
buf
[]
byte
)
{
bufCap
:=
cap
(
buf
)
func
(
bp
*
BytesPool
)
ReleaseBytes
(
buf
p
*
[]
byte
)
{
bufCap
:=
cap
(
*
bufp
)
idx
:=
bp
.
findIndex
(
bufCap
)
if
idx
>=
bp
.
length
||
bp
.
sizes
[
idx
]
!=
bufCap
{
return
}
bp
.
slots
[
idx
]
.
Put
(
buf
)
bp
.
slots
[
idx
]
.
Put
(
buf
p
)
}
// AcquireBytes called by defaultBytesPool
func
AcquireBytes
(
size
int
)
[]
byte
{
return
defaultBytesPool
.
AcquireBytes
(
size
)
}
func
AcquireBytes
(
size
int
)
*
[]
byte
{
return
defaultBytesPool
.
AcquireBytes
(
size
)
}
// ReleaseBytes called by defaultBytesPool
func
ReleaseBytes
(
buf
[]
byte
)
{
defaultBytesPool
.
ReleaseBytes
(
buf
)
}
func
ReleaseBytes
(
buf
p
*
[]
byte
)
{
defaultBytesPool
.
ReleaseBytes
(
bufp
)
}
bytes/slice_pool.go
View file @
a57121fa
...
...
@@ -52,13 +52,12 @@ func NewSlicePool() *SlicePool {
// Get returns *[]byte from SlicePool
func
(
p
*
SlicePool
)
Get
(
size
int
)
*
[]
byte
{
b
:=
p
.
AcquireBytes
(
size
)
return
&
b
return
p
.
AcquireBytes
(
size
)
}
// Put returns *[]byte to SlicePool
func
(
p
*
SlicePool
)
Put
(
buf
*
[]
byte
)
{
p
.
ReleaseBytes
(
*
buf
)
func
(
p
*
SlicePool
)
Put
(
buf
p
*
[]
byte
)
{
p
.
ReleaseBytes
(
bufp
)
}
// GetBytes returns *[]byte from SlicePool
...
...
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