Is .size_hint().1 ever used?

I tried to make use of it in this PR, but was unsuccessful:

Out of curiosity, I started looking through rust to see if it’s ever actually used. And I found lots of things like

But not a single case of something actually using the upper bound. (There were lots of passing it along or validity-checking it, but never doing something useful with it.)

Have you ever found a use for it?

12 Likes

That’s a shame it isn’t used.

Using something like upper_bound.min(lower_bound * 2) as the initial capacity for collect could save a reallocation.

1 Like

The lower bound is used to guide allocations for collecting. I don’t know why this is, I’d guess to avoid massive overallocations when the upper bound is far too vague.

The higher bound is used in TrustedLen, but then, its the same value as the lower bound in that case.

2 Likes

Yup, tried that :slight_smile:

The perf results weren't a clear win, though.

Yeah, ExactSizeIterator has a similar check, which of course isn't really much of a use:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.