Skip to content
This repository has been archived by the owner on Jul 21, 2023. It is now read-only.

perf: do not call varint.decode() if buffer has 0 length #125

Merged
merged 3 commits into from
Dec 7, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/coder/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Decoder {
const msgs = []

while (true) {
if (!this._buffer.length) {
// after consuming the whole length, _buffer has 0 length so don't want to bother varint
break
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
while (true) {
if (!this._buffer.length) {
// after consuming the whole length, _buffer has 0 length so don't want to bother varint
break
}
while (this._buffer.length) {

Suggested refactor - this way there's no new branch added so no need to add a test to ensure it's being hit.

Please can you check this against your CPU profiling to ensure it gives you the same performance improvement?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah this is great, should be the same with no new branch added 👍 . Thanks @achingbrain !

if (!this._headerInfo) {
try {
this._headerInfo = this._decodeHeader(this._bufferProxy)
Expand Down