console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.21k stars 238 forks source link

`{msg}` `.set_message(Cow<'static, str>)` design unnecessarily requires allocation #615

Open JulianKnodt opened 6 months ago

JulianKnodt commented 6 months ago

Currently I want to display a float adjacent to the progress bar (for reference it's essentially an optimization loss).

Right now, I have to do {msg} in the style, and later .set_message(format!("{:.03e}", float)). It's a hyper optimization, but because I'm calling format! every iteration, I'm allocating a String every loop. Is it possible to have a function that instead takes an Arguments struct, thus (I think) removing the allocation?

Willing to figure out how to submit a PR if so

djc commented 6 months ago

So are you updating the message on every iteration of a hot loop? Maybe it's feasible to use a ProgressTracker impl instead?

JulianKnodt commented 6 months ago

Right now I'm displaying [Elapsed/ETA] {progress bar} {current iter}/{total iters}{msg}. I now only update the msg every Nth iteration, which is alright.

I don't see a ton of docs for the progress tracker (I didn't even know it existed). If I want to use that, do I have to drop a lot of existing functionality?

djc commented 6 months ago

Uh, no? The ProgressTracker can be used to define a custom key for use in a format string, so you can do whatever you need with your float and only have it format whenever indicatif is drawing.