Skip to content

Improve twilight_mention::fmt performance by ~10% #942

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
Jun 19, 2021
Merged

Improve twilight_mention::fmt performance by ~10% #942

merged 2 commits into from
Jun 19, 2021

Conversation

spring4175
Copy link
Contributor

@spring4175 spring4175 commented Jun 18, 2021

Improve the performance of twilight_mention::fmt by around 10% by calling Display::fmt and Formatter::write_str directly instead of using write! and format_args!, which are notoriously (1, 2) slow.

Benchmarks:

format channel id       time:   [30.245 ns 30.317 ns 30.393 ns]
                        change: [-9.1152% -8.6720% -8.2652%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild

format emoji id         time:   [31.604 ns 31.629 ns 31.658 ns]
                        change: [-11.386% -10.974% -10.517%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  1 (1.00%) low severe
  2 (2.00%) high mild
  5 (5.00%) high severe

format role id          time:   [31.130 ns 31.155 ns 31.186 ns]
                        change: [-11.698% -11.386% -11.081%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  2 (2.00%) low severe
  3 (3.00%) high mild
  3 (3.00%) high severe

format user id          time:   [29.444 ns 29.473 ns 29.507 ns]
                        change: [-12.385% -12.045% -11.657%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 16 outliers among 100 measurements (16.00%)
  6 (6.00%) low severe
  2 (2.00%) low mild
  5 (5.00%) high mild
  3 (3.00%) high severe

Improve the performance of `twilight_mention::fmt` by around 10% by
calling `Display::fmt` and `Formatter::write_str` directly instead of
using `write!` and `format_args!`, which are notoriously[1][2] slow.

Benchmarks:

```
format channel id       time:   [30.245 ns 30.317 ns 30.393 ns]
                        change: [-9.1152% -8.6720% -8.2652%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) low mild
  1 (1.00%) high mild

format emoji id         time:   [31.604 ns 31.629 ns 31.658 ns]
                        change: [-11.386% -10.974% -10.517%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  1 (1.00%) low severe
  2 (2.00%) high mild
  5 (5.00%) high severe

format role id          time:   [31.130 ns 31.155 ns 31.186 ns]
                        change: [-11.698% -11.386% -11.081%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
  2 (2.00%) low severe
  3 (3.00%) high mild
  3 (3.00%) high severe

format user id          time:   [29.444 ns 29.473 ns 29.507 ns]
                        change: [-12.385% -12.045% -11.657%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 16 outliers among 100 measurements (16.00%)
  6 (6.00%) low severe
  2 (2.00%) low mild
  5 (5.00%) high mild
  3 (3.00%) high severe
```

[1]: rust-lang/rust#76490
[2]: rust-lang/rust#10761

Signed-off-by: Vivian Hellyer <[email protected]>
@spring4175 spring4175 added c-mention Affects the mention crate t-refactor Refactors APIs or code. labels Jun 18, 2021
@spring4175
Copy link
Contributor Author

Fails due to Rust 1.53 Clippy. #941 separately fixes them.

@spring4175 spring4175 merged commit 1b62ec5 into twilight-rs:main Jun 19, 2021
@spring4175 spring4175 deleted the refactor/mention/fmt/perf branch June 19, 2021 16:30
spring4175 pushed a commit that referenced this pull request Jun 29, 2021
Instead of using the `write!` and `format_args!` macros use `Display`
and `Formatter` method calls. The motivation for doing this is shown in
PR #942.

This does not include the work required to replace `write!` calls in
`http/src/routing.rs`; this work will be done in a separate PR.
Additionally one call is left in
`http/src/request/channel/reaction/mod.rs` and will be resolved in
another PR extracted from further work on #923.

Relates to #943.
7596ff pushed a commit that referenced this pull request Jun 29, 2021
Instead of using the `write!` and `format_args!` macros use `Display`
and `Formatter` method calls. The motivation for doing this is shown in
PR #942.

This does not include the work required to replace `write!` calls in
`http/src/routing.rs`; this work will be done in a separate PR.
Additionally one call is left in
`http/src/request/channel/reaction/mod.rs` and will be resolved in
another PR extracted from further work on #923.

Relates to #943.
spring4175 added a commit that referenced this pull request Jul 2, 2021
Additions

Support the new timestamp (`<t:unixtimestamp:?style>`) format via the
new `timestamp` module by adding a formatter and parser implementation
([#945] - [@zeylahellyer]).

Enhancements

Improve the `Display` implementation performance of
`ParseMentionError`'s `Display` implementation by calling `Formatter`
methods directly instead of calling the `format_args!` and `write!`
macros ([#944] - [@zeylahellyer]).

Improve the performance of the `fmt` module's Display implementations by
~10% by directly writing to Formatters instead of calling the
`format_args!` macro ([#942] - [@zeylahellyer]).

[@zeylahellyer]: https://github.com/zeylahellyer
[#945]: #945
[#944]: #944
[#942]: #942

Signed-off-by: Zeyla Hellyer <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c-mention Affects the mention crate t-refactor Refactors APIs or code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants