Skip to content

Conversation

@stepancheg
Copy link
Contributor

Summary

When requested amount is large but, but iterator is small, avoid allocating too large vec.

Motivation

If there's wrapper of such API (e.g. remote API), and it has parameter "sample size", because it is external API, user may pass very large value there, and if a user does that, it is better not to crash.

Details

This change is no-op where iterator lower bound is equal to iterator size.

  • Added a CHANGELOG.md entry

@stepancheg stepancheg force-pushed the iter-sample branch 2 times, most recently from eb80734 to cf6ba84 Compare December 20, 2025 07:51
@dhardy
Copy link
Member

dhardy commented Dec 22, 2025

From a look at the Vec::extend code I think we don't need to reserve anything at all.

@stepancheg
Copy link
Contributor Author

Yes, I think we should just avoid guessing capacity, because in many cases iterator should provide lower bound, and when it is not, we will do a few reallocations, not a big deal.

I updated the PR.

Copy link
Member

@dhardy dhardy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR

@dhardy dhardy merged commit ff07ec2 into rust-random:master Dec 28, 2025
15 checks passed
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