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-msgio
Commits
90cce793
Unverified
Commit
90cce793
authored
Jun 06, 2019
by
Steven Allen
Committed by
GitHub
Jun 06, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #13 from libp2p/feat/allocate-less
avoid allocating unless we need to
parents
b47fa43a
538b3e28
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
7 additions
and
16 deletions
+7
-16
varint.go
varint.go
+7
-16
No files found.
varint.go
View file @
90cce793
...
@@ -12,8 +12,8 @@ import (
...
@@ -12,8 +12,8 @@ import (
type
varintWriter
struct
{
type
varintWriter
struct
{
W
io
.
Writer
W
io
.
Writer
lbuf
[]
byte
// for encoding varints
lbuf
[
binary
.
MaxVarintLen64
]
byte
// for encoding varints
lock
sync
.
Locker
// for threadsafe writes
lock
sync
.
Mutex
// for threadsafe writes
}
}
// NewVarintWriter wraps an io.Writer with a varint msgio framed writer.
// NewVarintWriter wraps an io.Writer with a varint msgio framed writer.
...
@@ -21,9 +21,7 @@ type varintWriter struct {
...
@@ -21,9 +21,7 @@ type varintWriter struct {
// as a varint, using https://golang.org/pkg/encoding/binary/#PutUvarint
// as a varint, using https://golang.org/pkg/encoding/binary/#PutUvarint
func
NewVarintWriter
(
w
io
.
Writer
)
WriteCloser
{
func
NewVarintWriter
(
w
io
.
Writer
)
WriteCloser
{
return
&
varintWriter
{
return
&
varintWriter
{
W
:
w
,
W
:
w
,
lbuf
:
make
([]
byte
,
binary
.
MaxVarintLen64
),
lock
:
new
(
sync
.
Mutex
),
}
}
}
}
...
@@ -40,7 +38,7 @@ func (s *varintWriter) WriteMsg(msg []byte) error {
...
@@ -40,7 +38,7 @@ func (s *varintWriter) WriteMsg(msg []byte) error {
defer
s
.
lock
.
Unlock
()
defer
s
.
lock
.
Unlock
()
length
:=
uint64
(
len
(
msg
))
length
:=
uint64
(
len
(
msg
))
n
:=
binary
.
PutUvarint
(
s
.
lbuf
,
length
)
n
:=
binary
.
PutUvarint
(
s
.
lbuf
[
:
]
,
length
)
if
_
,
err
:=
s
.
W
.
Write
(
s
.
lbuf
[
:
n
]);
err
!=
nil
{
if
_
,
err
:=
s
.
W
.
Write
(
s
.
lbuf
[
:
n
]);
err
!=
nil
{
return
err
return
err
}
}
...
@@ -60,10 +58,9 @@ type varintReader struct {
...
@@ -60,10 +58,9 @@ type varintReader struct {
R
io
.
Reader
R
io
.
Reader
br
io
.
ByteReader
// for reading varints.
br
io
.
ByteReader
// for reading varints.
lbuf
[]
byte
next
int
next
int
pool
*
pool
.
BufferPool
pool
*
pool
.
BufferPool
lock
sync
.
Locker
lock
sync
.
Mutex
max
int
// the maximal message size (in bytes) this reader handles
max
int
// the maximal message size (in bytes) this reader handles
}
}
...
@@ -87,10 +84,8 @@ func NewVarintReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
...
@@ -87,10 +84,8 @@ func NewVarintReaderWithPool(r io.Reader, p *pool.BufferPool) ReadCloser {
return
&
varintReader
{
return
&
varintReader
{
R
:
r
,
R
:
r
,
br
:
&
simpleByteReader
{
R
:
r
},
br
:
&
simpleByteReader
{
R
:
r
},
lbuf
:
make
([]
byte
,
binary
.
MaxVarintLen64
),
next
:
-
1
,
next
:
-
1
,
pool
:
p
,
pool
:
p
,
lock
:
new
(
sync
.
Mutex
),
max
:
defaultMaxSize
,
max
:
defaultMaxSize
,
}
}
}
}
...
@@ -168,15 +163,11 @@ func (s *varintReader) Close() error {
...
@@ -168,15 +163,11 @@ func (s *varintReader) Close() error {
type
simpleByteReader
struct
{
type
simpleByteReader
struct
{
R
io
.
Reader
R
io
.
Reader
buf
[]
byte
buf
[
1
]
byte
}
}
func
(
r
*
simpleByteReader
)
ReadByte
()
(
c
byte
,
err
error
)
{
func
(
r
*
simpleByteReader
)
ReadByte
()
(
c
byte
,
err
error
)
{
if
r
.
buf
==
nil
{
if
_
,
err
:=
io
.
ReadFull
(
r
.
R
,
r
.
buf
[
:
]);
err
!=
nil
{
r
.
buf
=
make
([]
byte
,
1
)
}
if
_
,
err
:=
io
.
ReadFull
(
r
.
R
,
r
.
buf
);
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
return
r
.
buf
[
0
],
nil
return
r
.
buf
[
0
],
nil
...
...
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