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

chore: where possible replace async-trait with native async trait support in 1.75+ #197

Merged
merged 6 commits into from
Oct 14, 2024

Conversation

39555
Copy link
Contributor

@39555 39555 commented Oct 10, 2024

As of Rust 1.75, the async_fn_in_trait feature has been merged (see: rust-lang/rust#115822), meaning the async-trait crate is now only required for dynamic dispatch (dyn Trait). Since brush doesn’t use dyn Command, I decided to try removing the dependency. The modules arithmetic.rs and interp.rs are most affected, as they use async recursion, which requires futures to be boxed. I’ve wrapped all recursive functions in async move { .. }.boxed().

Copy link

github-actions bot commented Oct 10, 2024

Performance Benchmark Report

Benchmark name Baseline (μs) Test/PR (μs) Delta (μs) Delta %
expand_one_string 3.33 μs 3.33 μs -0.00 μs ⚪ Unchanged
instantiate_shell 60.20 μs 59.96 μs -0.24 μs ⚪ Unchanged
instantiate_shell_with_init_scripts 29796.80 μs 29501.12 μs -295.69 μs ⚪ Unchanged
parse_bash_completion 2725.56 μs 2713.36 μs -12.20 μs ⚪ Unchanged
parse_sample_script 4.02 μs 4.03 μs 0.01 μs ⚪ Unchanged
run_echo_builtin_command 88.94 μs 90.27 μs 1.33 μs ⚪ Unchanged
run_one_builtin_command 107.75 μs 109.15 μs 1.40 μs ⚪ Unchanged
run_one_external_command 2047.21 μs 1967.89 μs -79.32 μs 🟢 -3.87%
run_one_external_command_directly 1013.85 μs 1014.20 μs 0.35 μs ⚪ Unchanged

Code Coverage Report: Only Changed Files listed

Package Base Coverage New Coverage Difference
brush-core/src/builtins/alias.rs 🟠 66.67% 🟠 70.37% 🟢 3.7%
brush-core/src/builtins/break_.rs 🟢 77.78% 🟢 84.62% 🟢 6.84%
brush-core/src/builtins/builtin_.rs 🟢 94.12% 🟢 95% 🟢 0.88%
brush-core/src/builtins/cd.rs 🟠 75% 🟢 78.57% 🟢 3.57%
brush-core/src/builtins/command.rs 🟢 91.67% 🟢 91.95% 🟢 0.28%
brush-core/src/builtins/complete.rs 🔴 21.85% 🔴 22.11% 🟢 0.26%
brush-core/src/builtins/continue_.rs 🟢 81.82% 🟢 86.67% 🟢 4.85%
brush-core/src/builtins/declare.rs 🟢 81.64% 🟢 81.79% 🟢 0.15%
brush-core/src/builtins/dirs.rs 🟢 84.38% 🟢 85.71% 🟢 1.33%
brush-core/src/builtins/dot.rs 🟢 90% 🟢 91.67% 🟢 1.67%
brush-core/src/builtins/echo.rs 🟢 86.05% 🟢 86.96% 🟢 0.91%
brush-core/src/builtins/enable.rs 🟠 71.74% 🟠 73.47% 🟢 1.73%
brush-core/src/builtins/eval.rs 🟢 90.91% 🟢 92.86% 🟢 1.95%
brush-core/src/builtins/exec.rs 🟢 85.19% 🟢 86.67% 🟢 1.48%
brush-core/src/builtins/export.rs 🟠 72.55% 🟠 74.07% 🟢 1.52%
brush-core/src/builtins/getopts.rs 🟢 90.79% 🟢 91.14% 🟢 0.35%
brush-core/src/builtins/help.rs 🟢 89.02% 🟢 89.41% 🟢 0.39%
brush-core/src/builtins/jobs.rs 🔴 25.64% 🔴 30.95% 🟢 5.31%
brush-core/src/builtins/let_.rs 🟢 88.89% 🟢 90.48% 🟢 1.59%
brush-core/src/builtins/popd.rs 🟢 91.67% 🟢 93.33% 🟢 1.66%
brush-core/src/builtins/printf.rs 🟢 91.3% 🟢 91.84% 🟢 0.54%
brush-core/src/builtins/pushd.rs 🟢 88.89% 🟢 90.48% 🟢 1.59%
brush-core/src/builtins/pwd.rs 🟠 69.23% 🟢 80.95% 🟢 11.72%
brush-core/src/builtins/read.rs 🟠 64.33% 🟠 65% 🟢 0.67%
brush-core/src/builtins/return_.rs 🟠 75% 🟢 81.82% 🟢 6.82%
brush-core/src/builtins/set.rs 🟠 68.47% 🟠 69.3% 🟢 0.83%
brush-core/src/builtins/shift.rs 🟢 94.44% 🟢 95.24% 🟢 0.8%
brush-core/src/builtins/shopt.rs 🟠 69.05% 🟠 70.11% 🟢 1.06%
brush-core/src/builtins/test.rs 🟢 85% 🟢 86.96% 🟢 1.96%
brush-core/src/builtins/trap.rs 🟠 67.9% 🟠 69.05% 🟢 1.15%
brush-core/src/builtins/type_.rs 🟢 79.01% 🟢 79.76% 🟢 0.75%
brush-core/src/builtins/unalias.rs 🟢 85% 🟢 86.96% 🟢 1.96%
brush-core/src/builtins/unset.rs 🟢 80% 🟢 82.61% 🟢 2.61%
brush-core/src/builtins/wait.rs 🟠 65% 🟠 69.57% 🟢 4.57%
brush-core/src/interp.rs 🟢 90.21% 🟢 90.19% 🔴 -0.02%
brush-interactive/src/interactive_shell.rs 🟠 68.25% 🟠 71.43% 🟢 3.18%
brush-shell/src/brushctl.rs 🔴 17.14% 🔴 15.38% 🔴 -1.76%
brush-shell/src/shell_factory.rs 🔴 23.08% 🔴 34.78% 🟢 11.7%
Overall Coverage 🟢 72.24% 🟢 72.33% 🟢 0.09%

Minimum allowed coverage is 70%, this run produced 72.33%

@reubeno
Copy link
Owner

reubeno commented Oct 11, 2024

Thanks for looking at this.

Do you know if there's a way to keep the simpler return types (using the 'async' keyword's syntactic sugar)? At least for the simpler cases.

Also, for recursive cases, would be interesting to know if there are any crates with macros that are 1.75+ but "do the boxing for us" under the hood. This would also be mainly to keep the code slightly simpler to read/maintain.

@39555
Copy link
Contributor Author

39555 commented Oct 11, 2024

That's what async_trait or async_recursion do, but async_recursion does not work with an empty trait function body. I will revert the recursive functions since async_trait is really nice, and I forgot Pin<..> anyway. At least other traits without recursion would benefit from the native async though.

@reubeno
Copy link
Owner

reubeno commented Oct 12, 2024

That sounds like a better direction. Let's remove unnecessary #[async_trait::async_trait] attributes where it should now "just work natively". For other places where we'd have to make the code less readable, let's hold off for now.

@39555 39555 force-pushed the remove-async-trait branch 2 times, most recently from 4dfa2d6 to 55dc085 Compare October 13, 2024 07:15
@39555 39555 force-pushed the remove-async-trait branch 2 times, most recently from a0a3f7e to 94202e3 Compare October 13, 2024 07:22
@39555
Copy link
Contributor Author

39555 commented Oct 13, 2024

@reubeno done

@39555 39555 force-pushed the remove-async-trait branch 3 times, most recently from d36894f to 2fe6ba7 Compare October 13, 2024 08:20
@reubeno
Copy link
Owner

reubeno commented Oct 14, 2024

LGTM.

Thanks also for addressing some warnings along the way. I'll get this merged, but if you find additional warnings like these that aren't being caught by the GitHub PR workflows, then it would be interesting to figure out how we could tweak things to catch them in the future.

@reubeno reubeno changed the title experiment: remove async-trait chore: where possible replace async-trait with native async trait support in 1.75+ Oct 14, 2024
@reubeno reubeno merged commit 4266e9f into reubeno:main Oct 14, 2024
11 checks passed
@39555 39555 deleted the remove-async-trait branch October 14, 2024 18:57
39555 added a commit to 39555/brush that referenced this pull request Oct 22, 2024
…upport in 1.75+ (reubeno#197)

Reduce usage of the async-trait crate where possible, using the async_fn_in_trait native feature in 1.75+.
39555 added a commit to 39555/brush that referenced this pull request Oct 24, 2024
…upport in 1.75+ (reubeno#197)

Reduce usage of the async-trait crate where possible, using the async_fn_in_trait native feature in 1.75+.
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