Optimize addImage for image streaming: fast path bypasses full pipeline#4524
Open
physwkim wants to merge 4 commits intosilx-kit:mainfrom
Open
Optimize addImage for image streaming: fast path bypasses full pipeline#4524physwkim wants to merge 4 commits intosilx-kit:mainfrom
physwkim wants to merge 4 commits intosilx-kit:mainfrom
Conversation
When updating an existing image with same-shape data and the backend renderer supports direct updates (updateData), bypass the full item dirty/remove/add cycle and update the renderer directly. Fast path conditions: - Backend renderer exists and has updateData method - Data shape is unchanged from previous frame - No alternative/alpha images provided (ImageData) Changes: - ImageData.setData: early fast path before data copy - ImageDataBase.setData: direct renderer update when shape unchanged - PlotWidget.addImage: skip redundant setActiveImage on same image - Fix double copy in ImageData.setData (copy=False to super)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
While testing large image handling for another PR
#4520
I noticed a performance bottleneck when streaming large images (~4096×4096) using
addImage(). Each frame update goes through the full pipeline:For streaming scenarios where the image shape does not change, this introduces unnecessary overhead (renderer recreation, colormap recomputation, and extra data copies).
This PR introduces a fast path allowing the backend renderer to update image data directly when possible.
Changes
Renderer fast path
If the following conditions are met:
updateDataalternativeoralphaimagethen
ImageDataBase.setData()directly calls:This bypasses the remove/add renderer cycle.
Avoid redundant
setActiveImage()callsPlotWidget.addImage()now avoids callingsetActiveImage()if the image is already active, preventing unnecessary signal disconnect/reconnect during streaming.Fix unnecessary double copy
ImageData.setData()previously triggered an additional copy through:This PR changes it to:
File:
This avoids an extra ~64MB memory copy per frame for a 4096×4096 float32 image.
Performance (4096×4096 float32)
copy=True)copy=False)Notes
This optimization was discovered while investigating performance issues during large image testing in PR #4520.
The fast path is conservative and activates only when safe, without introducing new public APIs.