script: Do not iterate through all image frames when advancing animated images (#38857)

Instead of iterating through all frames every time we are looking for a
particular frame, just index into the `Vec` of frames directly. This
should do less work on every frame transition.

Testing: This is just a small optimization, so shouldn't change
observable
behavior.

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
Martin Robinson 2025-08-22 10:44:57 -07:00 committed by GitHub
parent fd3ea8854f
commit 2f44ba5168
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 7 deletions

View file

@ -311,18 +311,21 @@ impl RasterImage {
self.frames.len() > 1
}
pub fn frames(&self) -> impl Iterator<Item = ImageFrameView<'_>> {
self.frames.iter().map(|frame| ImageFrameView {
fn frame_view<'image>(&'image self, frame: &ImageFrame) -> ImageFrameView<'image> {
ImageFrameView {
delay: frame.delay,
bytes: self.bytes.get(frame.byte_range.clone()).unwrap(),
width: frame.width,
height: frame.height,
})
}
}
pub fn frame(&self, index: usize) -> Option<ImageFrameView<'_>> {
self.frames.get(index).map(|frame| self.frame_view(frame))
}
pub fn first_frame(&self) -> ImageFrameView<'_> {
self.frames()
.next()
self.frame(0)
.expect("All images should have at least one frame")
}
}

View file

@ -72,8 +72,7 @@ impl ImageAnimationManager {
let image = &state.image;
let frame = image
.frames()
.nth(state.active_frame)
.frame(state.active_frame)
.expect("active_frame should within range of frames");
if let Some(node) = rooted_nodes.get(&NoTrace(*node)) {