Skip to content
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

Bring back old async support PR #205

Merged
merged 7 commits into from
May 23, 2021
Merged

Bring back old async support PR #205

merged 7 commits into from
May 23, 2021

Conversation

damusix
Copy link
Contributor

@damusix damusix commented Dec 22, 2020

Rebased with hapijs/vision master. References #177

@damusix damusix changed the title Bring back async support. Bring back old async support PR Dec 22, 2020
@wswoodruff wswoodruff mentioned this pull request Dec 22, 2020
lib/manager.js Outdated
return this._render(compiled, context, request);

return await this._render(compiled, context, request);

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

test/manager.js Outdated Show resolved Hide resolved
test/manager.js Outdated Show resolved Hide resolved
const compiled = await this._prepare(filename, options);
return this._render(compiled, context, request);

return await this._render(compiled, context, request);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return await this._render(compiled, context, request);
return this._render(compiled, context, request);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

lol ahhhhhh! #205 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

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

Oops.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually this one is still valid, return await is an anti-pattern.

Copy link
Member

@devinivy devinivy Apr 24, 2021

Choose a reason for hiding this comment

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

As I see it, it's not necessarily an anti-pattern, as there are benefits and tradeoffs of return await. The downside is that it's not necessary to the code and you can expect to allocate an extra promise. The upside is that it enables correct async stack traces and is slightly easier to refactor around. My understanding is that the bulk of the performance downsides were largely addressed internally to V8 for node v12, though you still might omit the await in hot paths to squeeze out a little optimization if you're willing to sacrifice the line in the async stack trace. I don't have strong feelings in this case, but if there's a precedent set elsewhere in vision I might suggest we follow that.

Copy link
Contributor

Choose a reason for hiding this comment

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

@devinivy I don't see any optimization related to that kind of pattern, it still creates extra unnecessary promises. A quick test doesn't show a stack difference either. Do you have a snippet that would show a difference?

Copy link
Member

Choose a reason for hiding this comment

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

@Marsup interestingly in that example we don't receive an async stack track, possibly because no full ticks complete before the error is thrown. If you add in any amount of waiting before throwing, you'll see the difference: https://runkit.com/devinivy/60914a9d4b20c6001afb3aa3. The optimization I was referencing applies generally to await— it used to cost two promises and three microticks, and now it costs just one promise and one microtick, so this optimization of avoiding an await bears less fruit than it used to when async/await originally landed.

As far as this PR goes, my recommendation would be to stick with whatever convention is used within vision if there is one, and otherwise either way will be just fine (and we can always change it). Perhaps @Marsup, @damusic, or @wswoodruff has a sense for whether the optimization or the stacktraces are more valuable in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'd say in visions case, I favor the stacktrace

test/manager.js Outdated Show resolved Hide resolved
lib/manager.js Outdated Show resolved Hide resolved
Plus some style fixes mentioned in PR convo
@damusix
Copy link
Contributor Author

damusix commented Apr 19, 2021

@Marsup @wswoodruff done!

Copy link
Contributor

@wswoodruff wswoodruff left a comment

Choose a reason for hiding this comment

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

Looks good! — thank you for your work on this!

@Marsup Marsup merged commit 8bf34db into hapijs:master May 23, 2021
@Nargonath Nargonath added this to the 6.1.0 milestone May 25, 2021
@Nargonath Nargonath added the feature New functionality or improvement label May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants