Skip to content

Improve extension_trait! #1005

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

Merged
merged 2 commits into from
Mar 10, 2022

Conversation

nnethercote
Copy link
Contributor

The extension_trait! macro is an example of a "push-down accumulation" macro. Unfortunately, such macros are inherently quadratic to run, because the accumulated tokens must be reparsed for every recursive invocation. Some of the extension_trait! inputs are large -- the one in src/stream/stream/mod.rs is over 2,000 lines. As a result, the compile time of async-std is dominated by macro expansion.

This PR adjusts extension_trait! in a way that reduces its compile time without affecting its functionality. For a cargo check build of the leaf crate on my Linux box I saw compile time reductions of about 25%. The two commits both have roughly equal effect on compile time.

They never run because they are subsumed by the two rules immediately
above.
That way, when the `-> impl Future` rules fail (which is most of the
time), the cost of reparsing the accumulated tokens is avoided.
@nnethercote
Copy link
Contributor Author

Even with these changes the amount of compile time for extension_trait! is still significant. If it could be eliminated altogether, I estimate cargo check build times would drop by a similar amount again.

Copy link
Contributor

@yoshuawuyts yoshuawuyts left a comment

Choose a reason for hiding this comment

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

This is fantastic, thank you!

Even with these changes the amount of compile time for extension_trait! is still significant. If it could be eliminated altogether, I estimate cargo check build times would drop by a similar amount again.

I would actually be open to this change as well. The reason why we introduced this macro in the first place was because we were keen on showing that an "async version of std" was actually possible and nearly 1:1 translatable.

It's been nearly 3 years since we first showed that to the world though, and the idea that that's indeed possible is mainstream now. In that regard the macro approach has served its purpose, and I think we could move off it now if we wanted to. Because the macro only ever changed docs, and not actual reachable paths, I don't think we have to consider it a breaking change even.

Either way, that's for the future. I appreciate you benchmarking the impact of the macro, and helping us improve compile times!

@yoshuawuyts yoshuawuyts merged commit 8b812d5 into async-rs:master Mar 10, 2022
@nnethercote nnethercote deleted the improve-extension_trait branch March 10, 2022 19:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants