`size_hint` for `Display` and other `fmt` traits?

I tried; it turned out not to help. See https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187?u=scottmcm.

I didn't look deeply into it, but it might be that all the calculations for the max optimize away when they're unused. So the work for all that code might not be worth occasionally saving one doubling.

That's probably a bad idea, because a filter has a size_hint of (0, Some(n)), but reserving the whole space is often going to be a huge overallocation.

I would definitely like to see something like Iterator::reserve_suggestion that's just a single value where it's not a logic error for it to be wrong, just a perf/memory issue if it's inappropriate somehow -- basically exactly that comment on estimated_capacity.

7 Likes