radicle: Implement Iterator::size_hint() for optimizations

This patch adds an implementation of the Iterator::size_hint() trait
function so that the compiler can optimize allocations on the collecting
side of the iterator better.

From the trait function docs:

> size_hint() is primarily intended to be used for optimizations such
> as reserving space for the elements of the iterator, but must not be
> trusted to e.g., omit bounds checks in unsafe code. An incorrect
> implementation of size_hint() should not lead to memory safety
> violations.

Source: https://doc.rust-lang.org/stable/std/iter/trait.Iterator.html#method.size_hint

And:

> Vec may use any or none of the following strategies, depending on the
> supplied iterator:
>
>  *  preallocate based on Iterator::size_hint() and panic if the number
>     of items is outside the provided lower/upper bounds

Source: https://doc.rust-lang.org/stable/std/iter/trait.FromIterator.html

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2025-08-16 20:15:52 +02:00 committed by Lorenz Leutgeb
parent 1e66c57643
commit b49ff9e5af
1 changed files with 8 additions and 0 deletions

View File

@ -371,6 +371,10 @@ impl Iterator for NoCacheIter<'_> {
fn next(&mut self) -> Option<Self::Item> {
self.inner.next()
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<R> Issues for Cache<super::Issues<'_, R>, cache::NoCache>
@ -449,6 +453,10 @@ impl Iterator for IssuesIter<'_> {
let row = self.inner.next()?;
Some(row.map_err(Error::from).and_then(IssuesIter::parse_row))
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.inner.size_hint()
}
}
impl<R> Issues for Cache<R, StoreWriter>