Skip to content

fix(segmentation): replace unbounded render queue with coalescing Set#2674

Draft
pedrokohler wants to merge 1 commit into
cornerstonejs:mainfrom
pedrokohler:fix/unbounded-segmentation-render-queue
Draft

fix(segmentation): replace unbounded render queue with coalescing Set#2674
pedrokohler wants to merge 1 commit into
cornerstonejs:mainfrom
pedrokohler:fix/unbounded-segmentation-render-queue

Conversation

@pedrokohler

Copy link
Copy Markdown
Contributor

Context

SegmentationRenderingEngine._pendingRenderQueue is a string[][] that grows
without bound. Every scroll event on a StackViewport with segmentations appends a
new viewport-ID array to the queue. The queue is drained one entry per animation
frame via shift(), but entries arrive far faster than they are consumed.

After 310 scroll events on a viewport with 13 labelmap segmentations, the queue
held 4,077 entries — each an array of 13+ viewport IDs with associated
closures and promise chains — consuming significant memory and CPU.

Changes & Results

Replace _pendingRenderQueue: string[][] = [] with
_pendingRender: Set<string> = new Set().

  • Duplicate viewport IDs are coalesced into a single entry in the Set.
  • When the animation frame fires, all pending IDs are flushed in one batch and the
    Set is cleared.
  • Queue size is now bounded to O(viewports) instead of O(scroll events).

Before: queue grows linearly with scroll events (4,077 entries after 310 scrolls).
After: pending set stays bounded at the number of unique viewport IDs (e.g. 13).

Testing

  1. Open a StackViewport with 10+ labelmap segmentations.
  2. Scroll rapidly through 300+ slices.
  3. Observe stable memory — no unbounded growth in the render queue.

Checklist

PR

  • My Pull Request title is descriptive, accurate and follows the
    semantic-release format and guidelines.

Code

  • My code has been well-documented (function documentation, inline comments,
    etc.)

Public Documentation Updates

  • The documentation page has been updated as necessary for any public API
    additions or removals.

Made with Cursor

@pedrokohler pedrokohler marked this pull request as draft March 25, 2026 22:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant