Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
p2p
go-buffer-pool
Commits
e0f6ad1e
Unverified
Commit
e0f6ad1e
authored
Oct 05, 2018
by
Steven Allen
Committed by
GitHub
Oct 05, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from libp2p/fix/interface-ptr
pool pointers to pointers
parents
8adc4b47
c136f72a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
35 additions
and
3 deletions
+35
-3
pool.go
pool.go
+19
-3
pool_test.go
pool_test.go
+16
-0
No files found.
pool.go
View file @
e0f6ad1e
...
...
@@ -41,6 +41,11 @@ const MaxLength = math.MaxInt32
// You MUST NOT copy Pool after using.
type
BufferPool
struct
{
pools
[
32
]
sync
.
Pool
// a list of singlePools
ptrs
sync
.
Pool
}
type
bufp
struct
{
buf
[]
byte
}
// Get retrieves a buffer of the appropriate length from the buffer pool or
...
...
@@ -57,8 +62,12 @@ func (p *BufferPool) Get(length int) []byte {
return
make
([]
byte
,
length
)
}
idx
:=
nextLogBase2
(
uint32
(
length
))
if
buf
:=
p
.
pools
[
idx
]
.
Get
();
buf
!=
nil
{
return
buf
.
([]
byte
)[
:
uint32
(
length
)]
if
ptr
:=
p
.
pools
[
idx
]
.
Get
();
ptr
!=
nil
{
bp
:=
ptr
.
(
*
bufp
)
buf
:=
bp
.
buf
[
:
uint32
(
length
)]
bp
.
buf
=
nil
p
.
ptrs
.
Put
(
ptr
)
return
buf
}
return
make
([]
byte
,
1
<<
idx
)[
:
uint32
(
length
)]
}
...
...
@@ -70,7 +79,14 @@ func (p *BufferPool) Put(buf []byte) {
return
// drop it
}
idx
:=
prevLogBase2
(
uint32
(
capacity
))
p
.
pools
[
idx
]
.
Put
(
buf
)
var
bp
*
bufp
if
ptr
:=
p
.
ptrs
.
Get
();
ptr
!=
nil
{
bp
=
ptr
.
(
*
bufp
)
}
else
{
bp
=
new
(
bufp
)
}
bp
.
buf
=
buf
p
.
pools
[
idx
]
.
Put
(
bp
)
}
// Get retrieves a buffer of the appropriate length from the global buffer pool
...
...
pool_test.go
View file @
e0f6ad1e
...
...
@@ -16,6 +16,22 @@ import (
"testing"
)
func
TestAllocations
(
t
*
testing
.
T
)
{
var
m1
,
m2
runtime
.
MemStats
runtime
.
ReadMemStats
(
&
m1
)
runtime
.
GC
()
for
i
:=
0
;
i
<
10000
;
i
++
{
b
:=
Get
(
1010
)
Put
(
b
)
}
runtime
.
GC
()
runtime
.
ReadMemStats
(
&
m2
)
frees
:=
m2
.
Frees
-
m1
.
Frees
if
frees
>
100
{
t
.
Fatalf
(
"expected less than 100 frees after GC, got %d"
,
frees
)
}
}
func
TestPool
(
t
*
testing
.
T
)
{
// disable GC so we can control when it happens.
defer
debug
.
SetGCPercent
(
debug
.
SetGCPercent
(
-
1
))
...
...
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