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
dms3
cbor-gen
Commits
4fed7092
Unverified
Commit
4fed7092
authored
Aug 11, 2020
by
Whyrusleeping
Committed by
GitHub
Aug 11, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #36 from Stebalien/feat/optimize-discard
Optimize discarding in ScanForLinks
parents
d9232750
92f079f4
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
1 deletion
+28
-1
utils.go
utils.go
+28
-1
No files found.
utils.go
View file @
4fed7092
...
...
@@ -16,6 +16,33 @@ import (
const
maxCidLength
=
100
const
maxHeaderSize
=
9
// discard is a helper function to discard data from a reader, special-casing
// the most common readers we encounter in this library for a significant
// performance boost.
func
discard
(
br
io
.
Reader
,
n
int
)
error
{
switch
r
:=
br
.
(
type
)
{
case
*
bytes
.
Buffer
:
buf
:=
r
.
Next
(
n
)
if
len
(
buf
)
<
n
{
return
io
.
ErrUnexpectedEOF
}
return
nil
case
*
bytes
.
Reader
:
if
r
.
Len
()
<
n
{
_
,
_
=
r
.
Seek
(
0
,
io
.
SeekEnd
)
return
io
.
ErrUnexpectedEOF
}
_
,
err
:=
r
.
Seek
(
int64
(
n
),
io
.
SeekCurrent
)
return
err
case
*
bufio
.
Reader
:
_
,
err
:=
r
.
Discard
(
n
)
return
err
default
:
_
,
err
:=
io
.
CopyN
(
ioutil
.
Discard
,
br
,
int64
(
n
))
return
err
}
}
func
ScanForLinks
(
br
io
.
Reader
,
cb
func
(
cid
.
Cid
))
error
{
scratch
:=
make
([]
byte
,
maxCidLength
)
for
remaining
:=
uint64
(
1
);
remaining
>
0
;
remaining
--
{
...
...
@@ -27,7 +54,7 @@ func ScanForLinks(br io.Reader, cb func(cid.Cid)) error {
switch
maj
{
case
MajUnsignedInt
,
MajNegativeInt
,
MajOther
:
case
MajByteString
,
MajTextString
:
_
,
err
:=
io
.
CopyN
(
ioutil
.
D
iscard
,
br
,
int
64
(
extra
))
err
:=
d
iscard
(
br
,
int
(
extra
))
if
err
!=
nil
{
return
err
}
...
...
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