From b49ff9e5af6d6535b8c0a9b04f8f968d981af921 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 16 Aug 2025 20:15:52 +0200 Subject: [PATCH] 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 --- crates/radicle/src/cob/issue/cache.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/crates/radicle/src/cob/issue/cache.rs b/crates/radicle/src/cob/issue/cache.rs index 3e759982..7bd035b7 100644 --- a/crates/radicle/src/cob/issue/cache.rs +++ b/crates/radicle/src/cob/issue/cache.rs @@ -371,6 +371,10 @@ impl Iterator for NoCacheIter<'_> { fn next(&mut self) -> Option { self.inner.next() } + + fn size_hint(&self) -> (usize, Option) { + self.inner.size_hint() + } } impl Issues for Cache, 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) { + self.inner.size_hint() + } } impl Issues for Cache