# Is .size\_hint().1 ever used?

**URL:** https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187
**Category:** libs
**Created:** [August 13, 2018, 5:54am UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187 "2018-08-13T05:54:14Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)
#### Post date: [August 13, 2018, 5:54am UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187/1 "2018-08-13T05:54:14Z")

</div>

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

> <https://github.com/rust-lang/rust/pull/53086>

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

> <https://github.com/rust-lang/rust/blob/ab93561b5fa54954159480ddc10bbb69f015e539/src/liballoc/collections/binary_heap.rs#L1180-L1182>

> <https://github.com/rust-lang/rust/blob/ab93561b5fa54954159480ddc10bbb69f015e539/src/librustc_data_structures/small_vec.rs#L172-L174>

> <https://github.com/rust-lang/rust/blob/ab93561b5fa54954159480ddc10bbb69f015e539/src/liballoc/vec.rs#L2639-L2641>

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?

---

<div class="post-metadata">

### Author: ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)
#### Post date: [August 14, 2018, 12:09pm UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187/2 "2018-08-14T12:09:02Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![withoutboats](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/withoutboats/32/4560_2.png) [@withoutboats](https://internals.rust-lang.org/u/withoutboats)
#### Post date: [August 14, 2018, 1:09pm UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187/3 "2018-08-14T13:09:25Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)
#### Post date: [August 14, 2018, 10:06pm UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187/4 "2018-08-14T22:06:04Z")

</div>

> [@kornel](#):
>
> Using something like `upper_bound.min(lower_bound * 2)` as the initial capacity for collect could save a reallocation.

Yup, tried that 🙂

> <https://github.com/scottmcm/rust/blob/aa080f40413693e9bb8dd55e645c47984952a517/src/liballoc/vec.rs#L1864-L1868>

The [perf results](https://perf.rust-lang.org/compare.html?start=26d7b64237c6daa66064c8583b7aecf534c2a9ae&end=68f12661b56cdca600c5eca7158eb2ba4026e7f3) weren't a clear win, though.

> [@withoutboats](#):
>
> The higher bound is used in `TrustedLen` , but then, its the same value as the lower bound in that case.

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

> <https://github.com/rust-lang/rust/blob/c0955a34bcb17f0b31d7b86522a520ebe7fa93ac/src/libcore/iter/traits.rs#L691-L695>

---

<div class="post-metadata">

### Author: ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)
#### Post date: [March 25, 2019, 8:30am UTC](https://internals.rust-lang.org/t/is-size-hint-1-ever-used/8187/5 "2019-03-25T08:30:41Z")

</div>

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