Skip to content

Conversation

@c410-f3r
Copy link
Contributor

ToString::to_string makes a heap allocation and such a thing can cause a negative performance impact if used in a hot path. For example:

fn foo(dec: &mut Decimal) {
  loop {
    // stuff
    let string = dec.to_string();
    // more stuff
  }
}

One of the reasons why serialization was slow was also due to two heap allocations (#296). Therefore, it would be nice to expose to_str_internal.

The only pseudo-disadvantage is hard coding arrayvec into public interfaces but this can be mitigated in the future with a custom home-made ArrayString that returns ([u8; MAX_STR_BUFFER_SIZE], u8).

@Tony-Samuels
Copy link
Collaborator

Maybe I'm misunderstanding the proposal, but to_str_internal is used inside our implementation of std::fmt::Display, so wouldn't write! or similar work here?

@c410-f3r
Copy link
Contributor Author

c410-f3r commented Nov 17, 2025

The machinery behind formatting stuff is notoriously slow and such a thing also requires an additional copy to the desired container.

rust-lang/rust#10761
rust-lang/rust#76490

One of the reasons is the internal use of dynamic dispatch, see https://blog.m-ou.se/format-args.

I didn't profile Display but a direct exposure of the underlying array will likely be faster than write!.

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