GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — net,http2: refactor _write and _writev in net.Socket and http2.Http2Stream by ryzokuken · Pull Request #20643 · nodejs/node

Conversation

@ryzokuken

@ryzokuken ryzokuken commented May 9, 2018 •
edited by BridgeAR
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
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

Roadmap

  • Remove inconsistencies between http2.Http2Stream._write and http2.Http2Stream._writev
  • Move all logic into a new method http2.Http2Stream._writeGeneric
  • Remove inconsistencies between net.Socket._writeGeneric and http2.Http2Stream._writeGeneric
  • Refactor common logic into a method in the base class/method, possibly writeGeneric from internal/stream_base_commons.

/cc @addaleax @nodejs/http2 @nodejs/net

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ryzokuken ryzokuken self-assigned this May 9, 2018
@nodejs-github-bot nodejs-github-bot added dont-land-on-v4.x http2 Issues and PRs related to the http2 subsystem. labels May 9, 2018
@addaleax addaleax added wip Issues and PRs that are still a work in progress. net Issues and PRs related to the net subsystem. labels May 9, 2018
@mcollina

mcollina commented May 9, 2018

Copy link
Copy Markdown
Member

I'm concerned about making the writeGeneric having two different signatures (with and without the encoding). Supporting two different signature would force V8 to introduce a trampoline, which will slow things down as that is one of the hottest function in core

That logic needs tidy up anyway, so let's find a way to keep the number of arguments consistent.

jasnell and ronkorving reacted with thumbs up emoji
All reactions
  • 👍 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

@mcollina You mean _writeGeneric? I tried to keep the signature exactly the same as the one that already existed in net.Socket for consistency and also because I thought that everyone is onboard with that pattern. If net.Socket's _writeGeneric seems like an anti-pattern, I could try to do better but it does seem good enough given that it's actually taking the same arguments just that the encoding variable is set to an empty string.

That said, please share whatever you have in mind and hopefully we would be able to make it a tad faster and simpler.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mcollina

mcollina commented May 10, 2018

Copy link
Copy Markdown
Member

My bad, I misread a writeGeneric call with a writevGeneric.

ryzokuken reacted with laugh emoji
All reactions
  • 😄 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

That's what I did the first time around too 😅, but yeah. They're different functions, so I hope everything is fine, right? Should I proceed?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

A few questions:

  1. Why are we calling this._writeGeneric.bind(this, writev, data, encoding, cb) and not () => this._writeGeneric(writev, data, encoding, cb) in Http2Stream._writeGeneric unlike Socket._writeGeneric? Because we're calling this's _writeGeneric, won't this retain the same value? Why did we explicitly bind this? Am I missing something?

-- more to come --

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

okay, tests pass so I'm keeping it here until someone asks me to remove it.

One more thing:

Why does Socket._writeGeneric has:

this.once('connect', function connect() {
  this._writeGeneric(writev, data, encoding, cb);
});

even though normal functions have their own this? Does this depend on that behavior, or should an arrow function suffice? Let me try it out.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

Tests pass again, I'm either making progress, or we have weak test cases. Two things next:

  1. The _unrefTimer call in Socket comes before the destroyed check, as opposed to Http2Stream, which checks first. I believe we should go with the http2 way, trying this.

  2. In the Socket check, the socket is being destroyed (this.destroy(new ERR_SOCKET_CLOSED(), cb), but the Http2Stream version just drops the data on the floor and hopes for the best. This might take more time getting streamlined.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

Okay, when it comes to the actual logic for refreshing timers, they're both quite similar.

In net.Socket, we have a method called _unrefTimer which goes like:

Socket.prototype._unrefTimer = function _unrefTimer() {
  for (var s = this; s !== null; s = s._parent) {
    if (s[kTimeout])
      s[kTimeout][refreshFnSymbol]();
  }
};

and in http2.Http2Stream, we just call this[kUpdateTimer](), which goes like:

  [kUpdateTimer]() {
    if (this.destroyed)
      return;
    if (this[kTimeout])
      this[kTimeout][refreshFnSymbol]();
    if (this[kSession])
      this[kSession][kUpdateTimer]();
  }

for now, the least I think I could do is switch from the iterative approach in Socket to the recursive approach as in Http2Stream. After that, perhaps the code could be refactored to be a bit more similar?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 10, 2018

Copy link
Copy Markdown
Contributor Author

Okay, it wasn't recursing, but rather refreshing the associated session. That said, the code still looks a tad similar this way. Should I keep it in?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken ryzokuken mentioned this pull request May 10, 2018
timers: make timeout.refresh() a public API #20298
Closed
4 tasks done
@ryzokuken
ryzokuken force-pushed the refactor-stream-write branch 2 times, most recently from 1732c4f to 1257d56 Compare May 10, 2018 20:36
@ryzokuken

ryzokuken commented May 10, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Current roadblocks (inconsistencies):

  • this._pendingData and this._pendingEncoding.
  • Explicitly destroying the Socket using this.destroy(new ERR_SOCKET_CLOSED(), cb)
  • if (!this.headersSent) this[kProceed](); in Http2Stream
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 11, 2018

Copy link
Copy Markdown
Contributor Author

Ping @mcollina @addaleax

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@addaleax

addaleax commented May 14, 2018

Copy link
Copy Markdown
Member

this._pendingData and this._pendingEncoding.

Maybe something that we also want in HTTP/2?

Anyway, this LGTM as is and I’d be okay with landing this as-is and iterating onwards from there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented May 14, 2018

Copy link
Copy Markdown
Contributor

I haven't had a chance to fully review but 2 things off the top:

  • _writeGeneric shouldn't just be exposed on http2 like that. It should either be a helper function or on a Symbol.
  • I'm not sure how I feel about changing how timeouts are handled in net. That code is performance sensitive, it runs every single time that any action happens on the socket.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 14, 2018

Copy link
Copy Markdown
Contributor Author

@addaleax

Maybe something that we also want in HTTP/2?

While we may not need the properties yet, I believe they could prove helpful while fleshing out future behavior. Anyway, let me know if the added cruft of two simple properties would be worth a little consistency and nifty properties that could be used in the future (or perhaps even right now).

@apapirovski

_writeGeneric shouldn't just be exposed on http2 like that. It should either be a helper function or on a Symbol.

While I think I could just move to a Symbol pretty easily, I don't think it should be a problem because:

  1. _writeGeneric is also exposed for net.Socket?
  2. _write and _writev are exposed for http2.Http2Stream anyway?

That said, if you insist, I will make that change.

I'm not sure how I feel about changing how timeouts are handled in net.

Again, I made the change only to make things a tad more consistent, and you have a much better idea that me how this could affect speed in the long term. If you say, I could revert that particular part back to the original.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented May 15, 2018

Copy link
Copy Markdown
Contributor

While I think I could just move to a Symbol pretty easily, I don't think it should be a problem because:

The fact that _writeGeneric is exposed on the Socket is just bad behaviour we're stuck with — it comes from a time when we didn't have many options for hiding private properties and methods. Now we do and since it's a private implementation detail, it shouldn't leak to the user. As for _write and _writev, they are a part of the streams contract.

Again, I made the change only to make things a tad more consistent, and you have a much better idea that me how this could affect speed in the long term. If you say, I could revert that particular part back to the original.

I don't think things need to be more consistent between those two functions. I get the point of making _write and _writev more consistent, I don't think any unrelated functions need to be.

As far as how it's slower: calling a function (adding it to the stack) is slower than just a loop and since it's now recursive there's no way it can get inlined by V8.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 15, 2018

Copy link
Copy Markdown
Contributor Author

As far as how it's slower: calling a function (adding it to the stack) is slower than just a loop and since it's now recursive there's no way it can get inlined by V8.

Get it, loop would definitely be faster. That said, I'm thinking: as long as the two classes each have an exposed function (maybe called _refreshTimer) which could be called from _writeGeneric, it won't really matter how it's implemented. Therefore, reverting the function back to original and adding a _refreshTimer function for http2 seems appropriate.

The fact that _writeGeneric is exposed on the Socket is just bad behaviour we're stuck with

It seems that'll no longer be a problem once I'm done with this (same with it being exposed on Http2Stream), because the final goal of the PR would be to move the function to a base class.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented May 15, 2018

Copy link
Copy Markdown
Contributor

Keep in mind that there would be the same issue with exposing _refreshTimer as with exposing _writeGeneric.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 15, 2018 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@apapirovski could we not expose it but still call it from a base class, somehow?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 16, 2018

Copy link
Copy Markdown
Contributor Author

@apapirovski @addaleax This looks better, I suppose?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@apapirovski

apapirovski commented May 16, 2018

Copy link
Copy Markdown
Contributor

LGTM. Could also be a separate function to avoid the symbol lookup when calling it but this is fine as the first version.

_unrefTimer still needs to be reverted. Then we could likely land this and iterate from there.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 16, 2018

Copy link
Copy Markdown
Contributor Author

@apapirovski reverting _unrefTimer in a second.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 18, 2018

Copy link
Copy Markdown
Contributor Author

@BridgeAR okay! In that case, this is ready to merge.

Awaiting @apapirovski's explicit approval before landing.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

apapirovski
apapirovski approved these changes May 18, 2018
View reviewed changes

@apapirovski apapirovski left a comment

Copy link
Copy Markdown
Contributor

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.

LGTM. Thanks for working on this!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@ryzokuken

ryzokuken commented May 18, 2018

Copy link
Copy Markdown
Contributor Author

@apapirovski Thanks for all the constructive feedback and guidance! Will land this today.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 18, 2018

Copy link
Copy Markdown
Contributor Author

Landing this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken
net,http2: refactor _write and _writev
334f044
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.
@ryzokuken

ryzokuken commented May 19, 2018

Copy link
Copy Markdown
Contributor Author

Squashing and running CI, will land in the morning.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken
ryzokuken force-pushed the refactor-stream-write branch from 9cf4830 to 334f044 Compare May 19, 2018 18:55
@ryzokuken

ryzokuken commented May 19, 2018

Copy link
Copy Markdown
Contributor Author

CI: https://ci.nodejs.org/job/node-test-pull-request/14976/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken

ryzokuken commented May 19, 2018

Copy link
Copy Markdown
Contributor Author

Landed in a7fa0db

addaleax reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ryzokuken ryzokuken closed this May 19, 2018
ryzokuken added a commit that referenced this pull request May 19, 2018
@ryzokuken
net,http2: refactor _write and _writev
a7fa0db
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

PR-URL: #20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
MylesBorins pushed a commit that referenced this pull request May 22, 2018
@ryzokuken @MylesBorins
net,http2: refactor _write and _writev
d614511
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

PR-URL: #20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@addaleax addaleax mentioned this pull request May 22, 2018
v10.2.0 proposal #20724
Merged
kjin pushed a commit to kjin/node that referenced this pull request Aug 23, 2018
@ryzokuken @kjin
net,http2: refactor _write and _writev
da3aaf4
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

PR-URL: nodejs#20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
kjin pushed a commit to kjin/node that referenced this pull request Sep 25, 2018
@ryzokuken @kjin
net,http2: refactor _write and _writev
f2b38b9
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

PR-URL: nodejs#20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
kjin pushed a commit to kjin/node that referenced this pull request Oct 16, 2018
@ryzokuken @kjin
net,http2: refactor _write and _writev
c9f8d6f
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

PR-URL: nodejs#20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
BethGriggs pushed a commit that referenced this pull request Oct 17, 2018
@ryzokuken @BethGriggs
net,http2: refactor _write and _writev
b0e3ce9
Refactor writable part (the _write and _writev functions) in net.Socket
and http2.Http2Stream classes.
Also involves adding a generic "WriteGeneric" method to the Http2Stream
class based on net.Socket._writeGeneric, but behind a symbol.

Backport-PR-URL: #22850
PR-URL: #20643
Reviewed-By: Anatoli Papirovski <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@BethGriggs BethGriggs mentioned this pull request Oct 30, 2018
v8.13.0 proposal #23974
Merged
@beevelop beevelop mentioned this pull request Nov 20, 2018
2018-11-20 Version 8.13.0 'Carbon' (LTS) @MylesBorins prepared by @BethGriggs beevelop/docker-android-nodejs#250
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@addaleax addaleax addaleax left review comments
@BridgeAR BridgeAR BridgeAR approved these changes
+1 more reviewer
@apapirovski apapirovski apapirovski approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@ryzokuken ryzokuken

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. http2 Issues and PRs related to the http2 subsystem. net Issues and PRs related to the net subsystem.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@ryzokuken @mcollina @addaleax @apapirovski @BridgeAR @nodejs-github-bot
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.