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

axum-core/macros: Avoid double body_text() calls in rejection macros #3110

Merged
merged 1 commit into from
Dec 27, 2024

Conversation

Turbo87
Copy link
Collaborator

@Turbo87 Turbo87 commented Dec 26, 2024

Maybe I'm missing something, but it looks like the IntoResponse implementations for the generated rejection structs are currently executing the body_text() fn twice, which causes it to allocate memory twice. This PR extracts local variables in the into_response() implementations to share the status and body text with the __log_rejection!() call instead.

body_text = body_text,
status = status,
Copy link
Member

Choose a reason for hiding this comment

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

I think these macro args are just forwarded to tracing, right? In that case, we can replace x = x with just x for args.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes and no. the macro-by-example pattern still expects them with the x = x syntax unfortunately, and it didn't feel worth it to add a dedicated pattern case for the shorthand syntax.

@@ -49,12 +49,15 @@ macro_rules! __define_rejection {

impl $crate::response::IntoResponse for $name {
fn into_response(self) -> $crate::response::Response {
let status = self.status();
let body_text = self.body_text();
Copy link
Member

Choose a reason for hiding this comment

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

How is using self.body_text() better than using $body? I think the latter is always a string literal. It should probably be using the literal (lit?) macro fragment specifier instead of expr.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think I subconsciously aimed towards having the same into_response() content for both cases, but you're right that $body is better, because body_text() turns the &'static str into a String which isn't what we want here. I've reverted that part.

Copy link
Member

@jplatte jplatte left a comment

Choose a reason for hiding this comment

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

We could put the body string literal into a private associated constant for readability, I suppose. For the methods, I guess we could / should add #[inline] to fn status? It could also be a const fn. All of that isn't super important, and can be done in a separate PR though.

@Turbo87 Turbo87 merged commit 7c934f2 into tokio-rs:main Dec 27, 2024
18 checks passed
@Turbo87 Turbo87 deleted the macro-vars branch December 27, 2024 10:49
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