From 9d7ec93d2867a3ebd39ba10f237452ae49ff2b34 Mon Sep 17 00:00:00 2001 From: Jim Blandy Date: Thu, 6 Feb 2025 17:41:55 -0800 Subject: [PATCH] [naga] Simplify iterator construction in `type_expression_tandem`. (#7070) Rather than reversing two iterators and then zipping them, zip them first and then reverse the result. However, zipped iterators are only reversible if the inputs implement `ExactSizeIterator`, so make `UniqueArena::iter` promise that as well. For consistency, make `Arena::iter` also promise to return an `ExactSizeIterator`. --- naga/src/arena/mod.rs | 2 +- naga/src/arena/unique_arena.rs | 2 +- naga/src/compact/mod.rs | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/naga/src/arena/mod.rs b/naga/src/arena/mod.rs index 8bd3fb84cf..014c5167c6 100644 --- a/naga/src/arena/mod.rs +++ b/naga/src/arena/mod.rs @@ -94,7 +94,7 @@ impl Arena { /// Returns an iterator over the items stored in this arena, returning both /// the item's handle and a reference to it. - pub fn iter(&self) -> impl DoubleEndedIterator, &T)> { + pub fn iter(&self) -> impl DoubleEndedIterator, &T)> + ExactSizeIterator { self.data .iter() .enumerate() diff --git a/naga/src/arena/unique_arena.rs b/naga/src/arena/unique_arena.rs index c64bb302eb..9f5e26df11 100644 --- a/naga/src/arena/unique_arena.rs +++ b/naga/src/arena/unique_arena.rs @@ -108,7 +108,7 @@ impl Iterator for UniqueArenaDrain<'_, T> { impl UniqueArena { /// Returns an iterator over the items stored in this arena, returning both /// the item's handle and a reference to it. - pub fn iter(&self) -> impl DoubleEndedIterator, &T)> { + pub fn iter(&self) -> impl DoubleEndedIterator, &T)> + ExactSizeIterator { self.set.iter().enumerate().map(|(i, v)| { let index = unsafe { Index::new_unchecked(i as u32) }; (Handle::new(index), v) diff --git a/naga/src/compact/mod.rs b/naga/src/compact/mod.rs index ef41a611cb..929888b7e5 100644 --- a/naga/src/compact/mod.rs +++ b/naga/src/compact/mod.rs @@ -303,8 +303,9 @@ impl<'module> ModuleTracer<'module> { // as used by the time we visit it is genuinely unused, and can be // ignored. let mut exprs = self.module.global_expressions.iter().rev().peekable(); - for ((ty_handle, ty), dep) in self.module.types.iter().rev().zip(max_dep.iter().rev()) { - while let Some((expr_handle, expr)) = exprs.next_if(|&(h, _)| Some(h) > *dep) { + + for ((ty_handle, ty), dep) in self.module.types.iter().zip(max_dep).rev() { + while let Some((expr_handle, expr)) = exprs.next_if(|&(h, _)| Some(h) > dep) { if self.global_expressions_used.contains(expr_handle) { self.as_const_expression().trace_expression(expr); }