Skip to content

perf: optimize viewport to skip redundant refreshes#300

Open
rm-hull wants to merge 6 commits intomainfrom
fix/virtual-optimization
Open

perf: optimize viewport to skip redundant refreshes#300
rm-hull wants to merge 6 commits intomainfrom
fix/virtual-optimization

Conversation

@rm-hull
Copy link
Owner

@rm-hull rm-hull commented Feb 1, 2026

Introduces a _dirty flag and an optional force parameter to the viewport.refresh method. This ensures that the underlying device's display method is only invoked when content has actually changed, reducing unnecessary I/O and CPU usage.

Changes include:

  • Tracking a _dirty state when hotspots are removed.
  • Adding a force flag to refresh() to allow explicit overrides.
  • Updating display() and set_position() to trigger a forced refresh.
  • Skipping the device.display() call if no redraw is required and no state changes are detected.
sequenceDiagram
    participant App
    participant Viewport
    participant Hotspot
    participant Device

    App->>Viewport: refresh(force)
    Viewport->>Hotspot: should_redraw()
    Hotspot-->>Viewport: redraw_needed

    alt force or redraw_needed or dirty
        Viewport->>Device: display(image)
        Note over Viewport: Set dirty = False
    else no changes
        Note over Viewport: Skip display update
    end
Loading

Fixes #280

Introduces a `_dirty` flag and an optional `force` parameter to the
`viewport.refresh` method. This ensures that the underlying device's
`display` method is only invoked when content has actually changed,
reducing unnecessary I/O and CPU usage.

Changes include:
- Tracking a `_dirty` state when hotspots are removed.
- Adding a `force` flag to `refresh()` to allow explicit overrides.
- Updating `display()` and `set_position()` to trigger a forced
  refresh.
- Skipping the `device.display()` call if no redraw is required and no
  state changes are detected.

```mermaid
sequenceDiagram
    participant App
    participant Viewport
    participant Hotspot
    participant Device

    App->>Viewport: refresh(force)
    Viewport->>Hotspot: should_redraw()
    Hotspot-->>Viewport: redraw_needed

    alt force or redraw_needed or dirty
        Viewport->>Device: display(image)
        Note over Viewport: Set dirty = False
    else no changes
        Note over Viewport: Skip display update
    end
```

Fixes #280
@gemini-code-assist
Copy link

Summary of Changes

Hello @rm-hull, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the performance of the viewport component by introducing an intelligent refresh mechanism. By tracking content changes with a _dirty flag and allowing explicit forced refreshes, it ensures that the display is only updated when absolutely necessary, thereby reducing unnecessary I/O and CPU cycles and addressing a known issue with redundant refreshes.

Highlights

  • Optimized Viewport Refresh Logic: Implemented a _dirty flag and a force parameter in the viewport.refresh method to prevent redundant calls to the underlying device's display method, significantly reducing unnecessary I/O and CPU usage.
  • Explicit Refresh Triggers: Modified display() and set_position() methods to explicitly call refresh(force=True), ensuring updates are propagated to the device when the content or position of the viewport changes.
  • Hotspot Removal Tracking: The remove_hotspot() method now sets the _dirty flag, indicating that the viewport content has changed and requires a refresh on the next refresh() call.
  • New Test Case for Optimization: Added tests/test_issue_280.py to thoroughly validate the new refresh optimization logic, covering various scenarios like initial state, forced updates, and hotspot interactions to ensure correctness and prevent regressions.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a well-thought-out performance optimization for the viewport class by skipping redundant refreshes. The introduction of a _dirty flag and a force parameter in the refresh method is implemented correctly and logically. The changes ensure that the underlying device's display method is invoked only when necessary, which should reduce I/O and CPU usage as intended. The new test file provides excellent coverage, validating the behavior in various scenarios, including initial state, forced updates, and state changes from hotspot management. The code is clean, and the changes are effective. Overall, this is a high-quality contribution.

Set environment variables to force colored output for tools and disable
the interactive `tox` spinner. This improves the readability and
cleanliness of the GitHub Actions logs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant