-
Notifications
You must be signed in to change notification settings - Fork 903
GODRIVER-3533 Optimize value reader and writer #2022
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prestonvasquez
wants to merge
7
commits into
mongodb:master
Choose a base branch
from
prestonvasquez:GODRIVER-3533
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+82
−13
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
70c4fbb
GODRIVER-3533 Optimize value reader and writer
prestonvasquez 20145ad
GODRIVER-3533 Remove uncessary additions to vr
prestonvasquez e0f81a0
GODRIVER-3533 Account for full buffer in appending next ele
prestonvasquez 8848df3
GODRIVER-3533 Revert valueReader struct changes
prestonvasquez 3afc6f8
GODRIVER-3533 Use document-specific pool for encoding
prestonvasquez ecb98ce
GODRIVER-3533 Remove mem.perf file
prestonvasquez 2797876
GODRIVER-3533 Use vaueWriter constructor
prestonvasquez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ import ( | |
"fmt" | ||
"io" | ||
"math" | ||
"sync" | ||
) | ||
|
||
var _ ValueReader = &valueReader{} | ||
|
@@ -29,6 +30,20 @@ type vrState struct { | |
end int64 | ||
} | ||
|
||
var bufioReaderPool = sync.Pool{ | ||
New: func() interface{} { | ||
return bufio.NewReader(nil) | ||
}, | ||
} | ||
|
||
var vrPool = sync.Pool{ | ||
New: func() interface{} { | ||
return &valueReader{ | ||
stack: make([]vrState, 1, 5), | ||
} | ||
}, | ||
} | ||
|
||
// valueReader is for reading BSON values. | ||
type valueReader struct { | ||
r *bufio.Reader | ||
|
@@ -57,14 +72,26 @@ func newValueReader(t Type, r io.Reader) ValueReader { | |
} | ||
|
||
func newDocumentReader(r io.Reader) *valueReader { | ||
stack := make([]vrState, 1, 5) | ||
stack[0] = vrState{ | ||
mode: mTopLevel, | ||
} | ||
return &valueReader{ | ||
r: bufio.NewReader(r), | ||
stack: stack, | ||
} | ||
vr := vrPool.Get().(*valueReader) | ||
|
||
vr.offset = 0 | ||
vr.frame = 0 | ||
|
||
vr.stack = vr.stack[:1] | ||
vr.stack[0].mode = mTopLevel | ||
|
||
br := bufioReaderPool.Get().(*bufio.Reader) | ||
br.Reset(r) | ||
vr.r = br | ||
|
||
return vr | ||
} | ||
|
||
func releaseDocumentReader(vr *valueReader) { | ||
qingyang-hu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bufioReaderPool.Put(vr.r) | ||
vr.r = nil | ||
|
||
vrPool.Put(vr) | ||
} | ||
|
||
func (vr *valueReader) advanceFrame() { | ||
|
@@ -253,14 +280,28 @@ func (vr *valueReader) appendNextElement(dst []byte) ([]byte, error) { | |
return nil, err | ||
} | ||
|
||
buf := make([]byte, length) | ||
_, err = io.ReadFull(vr.r, buf) | ||
buf, err := vr.r.Peek(int(length)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Peek only allocates once for the bufio's internal buffer, we can borrow views of it with Peek without touching the heap. And append makes the copy. |
||
if err != nil { | ||
if err == bufio.ErrBufferFull { | ||
temp := make([]byte, length) | ||
if _, err = io.ReadFull(vr.r, temp); err != nil { | ||
return nil, err | ||
} | ||
dst = append(dst, temp...) | ||
vr.offset += int64(len(temp)) | ||
return dst, nil | ||
} | ||
|
||
return nil, err | ||
} | ||
|
||
dst = append(dst, buf...) | ||
vr.offset += int64(len(buf)) | ||
return dst, err | ||
if _, err = vr.r.Discard(int(length)); err != nil { | ||
return nil, err | ||
} | ||
|
||
vr.offset += int64(length) | ||
return dst, nil | ||
} | ||
|
||
func (vr *valueReader) readValueBytes(dst []byte) (Type, []byte, error) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newDocumentReader()
is also called wherereleaseDocumentReader()
is not called. It seems we will have to reversenewDocumentReader()
and add areset()
forvalueReader
like what we did forvalueWriter
.