Skip to content

stream: remove unreachable code - #18239

Closed
lpinca wants to merge 2 commits into
nodejs:masterfrom
lpinca:remove/unreachable-code
Closed

stream: remove unreachable code #18239
lpinca wants to merge 2 commits into
nodejs:masterfrom
lpinca:remove/unreachable-code

Conversation

@lpinca

Copy link
Copy Markdown
Member

First commit remove some unreachable code. BufferList.prototype.concat() is not called when there is only a buffer in the list.

Second commit fixes a test.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

stream, test

To avoid a function call `BufferList.prototype.concat()` is not called
when there is only a buffer in the list. That buffer is instead
accessed directly.
The `n` argument of `BufferList.prototype.concat()` is not the number
of `Buffer` instances in the list, but their total length when
concatenated.
@lpincalpinca added stream Issues and PRs related to the stream subsystem. test Issues and PRs related to the tests. labels Jan 18, 2018
@MoonBall

MoonBall commented Jan 20, 2018

Copy link
Copy Markdown
Member

It's better to modify function fromList(n, state) in lib/_stream_readable.js by deleting the code, because BufferList is a independent module.

@lpinca

Copy link
Copy Markdown
MemberAuthor

@MoonBallBufferList is an internal module so it doesn't really matter and I guess it was done like this to avoid the function call.

The _stream_readable module is plenty of code that use internal details of BufferList.

@MoonBall

Copy link
Copy Markdown
Member

ok. I agree.

@targos

Copy link
Copy Markdown
Member

/cc @nodejs/streams

@mcollina

Copy link
Copy Markdown
Member

I'm not super comfortable with this change.
What would happen if it happens? Any error would be good.

@lpinca

Copy link
Copy Markdown
MemberAuthor

What would happen if it happens?

Do you mean if BufferList#concat() is called when there is only one element in the list and this patch is applied? If so, nothing, it will still work as expected just without the fast path (which is already here).

@mcollina

Copy link
Copy Markdown
Member

Can you restore the test then?


const copy = list.concat(3);

assert.strictEqual(list.concat(1), 'foo');

@lpincalpincaJan 20, 2018

Copy link
Copy Markdown
MemberAuthor

Choose a reason for hiding this comment

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

@mcollina this is wrong whether or not the change in BufferList.js is applied.

  • The test uses a string, probably for simplicity, but concat() only works with buffers.
  • The n argument (1) was wrong and irrelevant. You could have used any argument and the test would have still passed.

@lpinca

Copy link
Copy Markdown
MemberAuthor

FWIW the fixed test actually tests the impossible case where BufferList#concat() is called and there is only one buffer in the list. Unlike the fast path, it creates a copy.

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM, perfect

@lpinca

Copy link
Copy Markdown
MemberAuthor

lpinca added a commit that referenced this pull request Jan 24, 2018
To avoid a function call `BufferList.prototype.concat()` is not called
when there is only a buffer in the list. That buffer is instead
accessed directly.
PR-URL: #18239
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
lpinca added a commit that referenced this pull request Jan 24, 2018
The `n` argument of `BufferList.prototype.concat()` is not the number
of `Buffer` instances in the list, but their total length when
concatenated.
PR-URL: #18239
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
@lpinca

Copy link
Copy Markdown
MemberAuthor

Landed in e65a6e8...2313424.

@lpincalpinca closed this Jan 24, 2018
@lpinca
lpinca deleted the remove/unreachable-code branch January 24, 2018 15:32
@evanlucasevanlucas mentioned this pull request Jan 30, 2018
evanlucas pushed a commit that referenced this pull request Jan 30, 2018
To avoid a function call `BufferList.prototype.concat()` is not called
when there is only a buffer in the list. That buffer is instead
accessed directly.
PR-URL: #18239
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
evanlucas pushed a commit that referenced this pull request Jan 30, 2018
The `n` argument of `BufferList.prototype.concat()` is not the number
of `Buffer` instances in the list, but their total length when
concatenated.
PR-URL: #18239
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

streamIssues and PRs related to the stream subsystem.testIssues and PRs related to the tests.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@lpinca@MoonBall@targos@mcollina@MylesBorins