good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
websocket
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
github
nhooyr
websocket
Commits
e9d8945f
Unverified
Commit
e9d8945f
authored
Jun 2, 2019
by
Anmol Sethi
Browse files
Options
Downloads
Patches
Plain Diff
Pool buffers in wspb and wsjson
Closes #71
parent
631c152c
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
internal/bpool/bpool.go
+24
-0
24 additions, 0 deletions
internal/bpool/bpool.go
internal/bpool/bpool_test.go
+47
-0
47 additions, 0 deletions
internal/bpool/bpool_test.go
wsjson/wsjson.go
+13
-2
13 additions, 2 deletions
wsjson/wsjson.go
wspb/wspb.go
+25
-4
25 additions, 4 deletions
wspb/wspb.go
with
109 additions
and
6 deletions
internal/bpool/bpool.go
0 → 100644
+
24
−
0
View file @
e9d8945f
package
bpool
import
(
"bytes"
"sync"
)
var
bpool
sync
.
Pool
// Get returns a buffer from the pool or creates a new one if
// the pool is empty.
func
Get
()
*
bytes
.
Buffer
{
b
,
ok
:=
bpool
.
Get
()
.
(
*
bytes
.
Buffer
)
if
!
ok
{
b
=
&
bytes
.
Buffer
{}
}
return
b
}
// Put returns a buffer into the pool.
func
Put
(
b
*
bytes
.
Buffer
)
{
b
.
Reset
()
bpool
.
Put
(
b
)
}
This diff is collapsed.
Click to expand it.
internal/bpool/bpool_test.go
0 → 100644
+
47
−
0
View file @
e9d8945f
package
bpool
import
(
"strconv"
"sync"
"testing"
)
func
BenchmarkSyncPool
(
b
*
testing
.
B
)
{
sizes
:=
[]
int
{
2
,
16
,
32
,
64
,
128
,
256
,
512
,
4096
,
16384
,
}
for
_
,
size
:=
range
sizes
{
b
.
Run
(
strconv
.
Itoa
(
size
),
func
(
b
*
testing
.
B
)
{
b
.
Run
(
"allocate"
,
func
(
b
*
testing
.
B
)
{
b
.
ReportAllocs
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
buf
:=
make
([]
byte
,
size
)
_
=
buf
}
})
b
.
Run
(
"pool"
,
func
(
b
*
testing
.
B
)
{
b
.
ReportAllocs
()
p
:=
sync
.
Pool
{}
b
.
ResetTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
buf
:=
p
.
Get
()
if
buf
==
nil
{
buf
=
make
([]
byte
,
size
)
}
p
.
Put
(
buf
)
}
})
})
}
}
This diff is collapsed.
Click to expand it.
wsjson/wsjson.go
+
13
−
2
View file @
e9d8945f
...
...
@@ -8,6 +8,7 @@ import (
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/internal/bpool"
)
// Read reads a json message from c into v.
...
...
@@ -22,7 +23,7 @@ func Read(ctx context.Context, c *websocket.Conn, v interface{}) error {
}
func
read
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
,
v
interface
{})
error
{
typ
,
b
,
err
:=
c
.
Read
(
ctx
)
typ
,
r
,
err
:=
c
.
Read
er
(
ctx
)
if
err
!=
nil
{
return
err
}
...
...
@@ -32,7 +33,17 @@ func read(ctx context.Context, c *websocket.Conn, v interface{}) error {
return
xerrors
.
Errorf
(
"unexpected frame type for json (expected %v): %v"
,
websocket
.
MessageText
,
typ
)
}
err
=
json
.
Unmarshal
(
b
,
v
)
b
:=
bpool
.
Get
()
defer
func
()
{
bpool
.
Put
(
b
)
}()
_
,
err
=
b
.
ReadFrom
(
r
)
if
err
!=
nil
{
return
err
}
err
=
json
.
Unmarshal
(
b
.
Bytes
(),
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to unmarshal json: %w"
,
err
)
}
...
...
This diff is collapsed.
Click to expand it.
wspb/wspb.go
+
25
−
4
View file @
e9d8945f
...
...
@@ -2,12 +2,15 @@
package
wspb
import
(
"bytes"
"context"
"sync"
"github.com/golang/protobuf/proto"
"golang.org/x/xerrors"
"nhooyr.io/websocket"
"nhooyr.io/websocket/internal/bpool"
)
// Read reads a protobuf message from c into v.
...
...
@@ -21,7 +24,7 @@ func Read(ctx context.Context, c *websocket.Conn, v proto.Message) error {
}
func
read
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
,
v
proto
.
Message
)
error
{
typ
,
b
,
err
:=
c
.
Read
(
ctx
)
typ
,
r
,
err
:=
c
.
Read
er
(
ctx
)
if
err
!=
nil
{
return
err
}
...
...
@@ -31,7 +34,17 @@ func read(ctx context.Context, c *websocket.Conn, v proto.Message) error {
return
xerrors
.
Errorf
(
"unexpected frame type for protobuf (expected %v): %v"
,
websocket
.
MessageBinary
,
typ
)
}
err
=
proto
.
Unmarshal
(
b
,
v
)
b
:=
bpool
.
Get
()
defer
func
()
{
bpool
.
Put
(
b
)
}()
_
,
err
=
b
.
ReadFrom
(
r
)
if
err
!=
nil
{
return
err
}
err
=
proto
.
Unmarshal
(
b
.
Bytes
(),
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to unmarshal protobuf: %w"
,
err
)
}
...
...
@@ -49,11 +62,19 @@ func Write(ctx context.Context, c *websocket.Conn, v proto.Message) error {
return
nil
}
var
writeBufPool
sync
.
Pool
func
write
(
ctx
context
.
Context
,
c
*
websocket
.
Conn
,
v
proto
.
Message
)
error
{
b
,
err
:=
proto
.
Marshal
(
v
)
b
:=
bpool
.
Get
()
pb
:=
proto
.
NewBuffer
(
b
.
Bytes
())
defer
func
()
{
bpool
.
Put
(
bytes
.
NewBuffer
(
pb
.
Bytes
()))
}()
err
:=
pb
.
Marshal
(
v
)
if
err
!=
nil
{
return
xerrors
.
Errorf
(
"failed to marshal protobuf: %w"
,
err
)
}
return
c
.
Write
(
ctx
,
websocket
.
MessageBinary
,
b
)
return
c
.
Write
(
ctx
,
websocket
.
MessageBinary
,
pb
.
Bytes
()
)
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment