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

[trace] Reduce code bloat of on_xxx function #342 #363

Closed
wants to merge 1 commit into from

Conversation

erebe
Copy link
Contributor

@erebe erebe commented Apr 20, 2023

Not completly fixing #342 but greatly reduce the pressure.

At the expanse of losing fractional precision when a latency of second is requested, this PR reduce the code generated by on_xxx function by removing the cardinality of selecting the latency unit in the match expression.

Before the LatencyUnit::Seconds was displayed with xxx.as_secs_f64(), with this patch it is displayed as xxx.as_secs() to be able to cast it to u128 and have an homogenous return type. This allows to remove a cardinality from the huge match we are creating to call the correct log.

before:

❯ cargo bloat --feature trace
0.2%   2.5% 243.4KiB         tower_http <tower_http::trace::on_response::DefaultOnResponse as tower_http::trace::on_response::OnResponse<B,A>>::on_response
0.1%   1.2% 112.6KiB         tower_http <tower_http::trace::on_eos::DefaultOnEos as tower_http::trace::on_eos::OnEos<ReqBody>>::on_eos
0.1%   0.7%  70.4KiB         tower_http <tower_http::trace::on_failure::DefaultOnFailure as tower_http::trace::on_failure::OnFailure<FailureClass,ReqBody>>::on_failure

after this patch:

❯ cargo bloat --feature trace
0.1%   0.7%  64.7KiB         tower_http <tower_http::trace::on_response::DefaultOnResponse as tower_http::trace::on_response::OnResponse<B,A>>::on_response
0.0%   0.3%  30.1KiB         tower_http <tower_http::trace::make_span::DefaultMakeSpan as tower_http::trace::make_span::MakeSpan<B>>::make_span
0.0%   0.3%  30.0KiB         tower_http <tower_http::trace::on_eos::DefaultOnEos as tower_http::trace::on_eos::OnEos<ReqBody>>::on_eos

At the expanse of losing fractional precesion when
a latency of second is requested, this PR reduce the
code generated by on_xxx function by removing a cardinality
in th match expression.

before:
0.2%   2.5% 243.4KiB         tower_http <tower_http::trace::on_response::DefaultOnResponse as tower_http::trace::on_response::OnResponse<B,A>>::on_response
0.1%   1.2% 112.6KiB         tower_http <tower_http::trace::on_eos::DefaultOnEos as tower_http::trace::on_eos::OnEos<ReqBody>>::on_eos
0.1%   0.7%  70.4KiB         tower_http <tower_http::trace::on_failure::DefaultOnFailure as tower_http::trace::on_failure::OnFailure<FailureClass,ReqBody>>::on_failure

after this patch:
0.1%   0.7%  64.7KiB         tower_http <tower_http::trace::on_response::DefaultOnResponse as tower_http::trace::on_response::OnResponse<B,A>>::on_response
0.0%   0.3%  30.1KiB         tower_http <tower_http::trace::make_span::DefaultMakeSpan as tower_http::trace::make_span::MakeSpan<B>>::make_span
0.0%   0.3%  30.0KiB         tower_http <tower_http::trace::on_eos::DefaultOnEos as tower_http::trace::on_eos::OnEos<ReqBody>>::on_eos
@jplatte
Copy link
Collaborator

jplatte commented Jun 26, 2023

Hey @erebe, can you check if #380 gives the same code size gains? That one doesn't change the formatting for the seconds case.

@erebe
Copy link
Contributor Author

erebe commented Jun 27, 2023

Hello @jplatte,

It is much better than my version, the only bloat that remains with your patch is

0.0%   0.3%  31.2KiB         tower_http <tower_http::trace::make_span::DefaultMakeSpan as tower_http::trace::make_span::MakeSpan<B>>::make_span

the other ones disappeared from the report. 👍

Thank you for having taking a shot at it :)

P.s: I am going into vacation, so may not be super re-active

@erebe
Copy link
Contributor Author

erebe commented Jun 27, 2023

Closing as #380 is a better solution.

@erebe erebe closed this Jun 27, 2023
@erebe erebe deleted the issue_342 branch June 27, 2023 07:36
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