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

Conversation

twoeths
Copy link
Contributor

@twoeths twoeths commented Nov 30, 2021

Motivation

Screen Shot 2021-11-30 at 15 03 19

  • after consuming the whole length, _buffer has 0 length so don't want to bother varint

@twoeths
Copy link
Contributor Author

twoeths commented Dec 3, 2021

@vasco-santos could you have a look at this PR? It's quite important for lodestar as it causes a performance issue. Thanks!

@achingbrain
Copy link
Member

@tuyennhv is it possible to add a test for this so a regression will not be caused?

@achingbrain
Copy link
Member

Merging master into this branch will get you gh actions and remove travis too for more reliable CI.

@lidel lidel added this to In Review in Maintenance Priorities - JS via automation Dec 3, 2021
@twoeths
Copy link
Contributor Author

twoeths commented Dec 4, 2021

@tuyennhv is it possible to add a test for this so a regression will not be caused?

so I looked into coder.spec.js, it does the job of scanning through the change and make sure it does not break anything. Instead of reaching the try/catch {break}, it always jumps into if() {break} instead.

I can't think of a good way to test the change unless:

  • we publish the Decoder class
  • add something like sinon to count and confirm how many varint.decode() calls we have

let me know if it's worth to do that for this simple change or if there's any better ways to test the change

@twoeths
Copy link
Contributor Author

twoeths commented Dec 4, 2021

@achingbrain also please help approve the gh workflow, thanks.

Comment on lines 35 to 39
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 !

@achingbrain achingbrain changed the title Do not call varint.decode() if 0 length perf: do not call varint.decode() if buffer has 0 length Dec 7, 2021
@achingbrain achingbrain merged commit 92f1727 into libp2p:master Dec 7, 2021
Maintenance Priorities - JS automation moved this from In Review to Done Dec 7, 2021
@achingbrain
Copy link
Member

Shipped in libp2p-mplex@0.10.5

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants