-
Notifications
You must be signed in to change notification settings - Fork 488
fix(segmentation): reuse VTK actors on scroll instead of destroy/recreate #2676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pedrokohler
wants to merge
4
commits into
cornerstonejs:main
Choose a base branch
from
pedrokohler:fix/memory-leak-vtk-actor-reuse-on-scroll
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+296
−17
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
79a708e
fix(segmentation): reuse VTK actors on scroll instead of destroy/recr…
pedrokohler 9b23684
fix(segmentation): prevent actor theft when reusing actors for overla…
pedrokohler b4809ed
fix(tools): exclude test files from ESM build tsconfig
pedrokohler 9a64cab
fix(segmentation): restore pertinent comment removed during refactor
pedrokohler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
239 changes: 239 additions & 0 deletions
239
packages/tools/src/eventListeners/segmentation/__tests__/imageChangeEventListener.spec.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import { SegmentationRepresentations } from '../../../enums'; | ||
|
|
||
| jest.mock('@cornerstonejs/core', () => ({ | ||
| BaseVolumeViewport: class BaseVolumeViewport {}, | ||
| getEnabledElement: jest.fn(), | ||
| getEnabledElementByIds: jest.fn(), | ||
| Enums: { | ||
| Events: { | ||
| PRE_STACK_NEW_IMAGE: 'PRE_STACK_NEW_IMAGE', | ||
| IMAGE_RENDERED: 'IMAGE_RENDERED', | ||
| }, | ||
| }, | ||
| cache: { getImage: jest.fn() }, | ||
| utilities: { updateVTKImageDataWithCornerstoneImage: jest.fn() }, | ||
| })); | ||
|
|
||
| jest.mock( | ||
| '../../../stateManagement/segmentation/SegmentationRenderingEngine', | ||
| () => ({ triggerSegmentationRender: jest.fn() }) | ||
| ); | ||
| jest.mock( | ||
| '../../../stateManagement/segmentation/updateLabelmapSegmentationImageReferences', | ||
| () => ({ updateLabelmapSegmentationImageReferences: jest.fn() }) | ||
| ); | ||
| jest.mock( | ||
| '../../../stateManagement/segmentation/getCurrentLabelmapImageIdForViewport', | ||
| () => ({ getCurrentLabelmapImageIdsForViewport: jest.fn() }) | ||
| ); | ||
| jest.mock( | ||
| '../../../stateManagement/segmentation/helpers/getSegmentationActor', | ||
| () => ({ getLabelmapActorEntries: jest.fn() }) | ||
| ); | ||
| jest.mock( | ||
| '../../../stateManagement/segmentation/getSegmentationRepresentation', | ||
| () => ({ getSegmentationRepresentations: jest.fn() }) | ||
| ); | ||
|
|
||
| const SEG_ID = 'seg-1'; | ||
| const LABELMAP = SegmentationRepresentations.Labelmap; | ||
|
|
||
| interface MockActor { | ||
| uid: string; | ||
| representationUID: string; | ||
| referencedId: string; | ||
| } | ||
|
|
||
| function makeActor( | ||
| uid: string, | ||
| representationUID: string, | ||
| referencedId: string | ||
| ): MockActor { | ||
| return { uid, representationUID, referencedId }; | ||
| } | ||
|
|
||
| /** | ||
| * Simulates the reusable-actor search WITH the consumedActorUIDs fix. | ||
| * Returns which actor UIDs were reused and how many fallback creations occurred. | ||
| */ | ||
| function simulateActorReuse( | ||
| allActors: MockActor[], | ||
| derivedImageIds: string[], | ||
| segmentationId: string | ||
| ) { | ||
| const consumedActorUIDs = new Set<string>(); | ||
| const reusedActorUids: string[] = []; | ||
| let fallbackCreations = 0; | ||
|
|
||
| derivedImageIds.forEach((derivedImageId) => { | ||
| const exactMatch = allActors.find((a) => a.referencedId === derivedImageId); | ||
|
|
||
| if (!exactMatch) { | ||
| const reusableEntry = allActors.find( | ||
| (a) => | ||
| !consumedActorUIDs.has(a.uid) && | ||
| a.representationUID?.startsWith(`${segmentationId}-${LABELMAP}`) && | ||
| a.referencedId !== derivedImageId | ||
| ); | ||
|
|
||
| if (reusableEntry) { | ||
| consumedActorUIDs.add(reusableEntry.uid); | ||
| reusedActorUids.push(reusableEntry.uid); | ||
| reusableEntry.referencedId = derivedImageId; | ||
| reusableEntry.representationUID = `${segmentationId}-${LABELMAP}-${derivedImageId}`; | ||
| } else { | ||
| fallbackCreations++; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return { reusedActorUids, fallbackCreations }; | ||
| } | ||
|
|
||
| /** | ||
| * Simulates the BUGGY path without consumedActorUIDs tracking. | ||
| */ | ||
| function simulateBuggyActorReuse( | ||
| allActors: MockActor[], | ||
| derivedImageIds: string[], | ||
| segmentationId: string | ||
| ) { | ||
| const reusedActorUids: string[] = []; | ||
|
|
||
| derivedImageIds.forEach((derivedImageId) => { | ||
| const exactMatch = allActors.find((a) => a.referencedId === derivedImageId); | ||
|
|
||
| if (!exactMatch) { | ||
| const reusableEntry = allActors.find( | ||
| (a) => | ||
| a.representationUID?.startsWith(`${segmentationId}-${LABELMAP}`) && | ||
| a.referencedId !== derivedImageId | ||
| ); | ||
|
|
||
| if (reusableEntry) { | ||
| reusedActorUids.push(reusableEntry.uid); | ||
| reusableEntry.referencedId = derivedImageId; | ||
| reusableEntry.representationUID = `${segmentationId}-${LABELMAP}-${derivedImageId}`; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return { reusedActorUids }; | ||
| } | ||
|
|
||
| describe('imageChangeEventListener actor reuse', () => { | ||
| describe('overlapping segments — actor theft prevention', () => { | ||
| it('assigns each actor to a different derived image when two groups exist', () => { | ||
| const actorA = makeActor( | ||
| 'actor-a', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group0`, | ||
| 'derived-slice5-group0' | ||
| ); | ||
| const actorB = makeActor( | ||
| 'actor-b', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group1`, | ||
| 'derived-slice5-group1' | ||
| ); | ||
|
|
||
| const { reusedActorUids } = simulateActorReuse( | ||
| [actorA, actorB], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(reusedActorUids).toHaveLength(2); | ||
| expect(reusedActorUids[0]).not.toBe(reusedActorUids[1]); | ||
| expect(reusedActorUids).toContain('actor-a'); | ||
| expect(reusedActorUids).toContain('actor-b'); | ||
|
|
||
| expect(actorA.referencedId).toBe('derived-slice6-group0'); | ||
| expect(actorB.referencedId).toBe('derived-slice6-group1'); | ||
| }); | ||
|
|
||
| it('demonstrates actor theft when consumedActorUIDs is not used', () => { | ||
| const actorA = makeActor( | ||
| 'actor-a', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group0`, | ||
| 'derived-slice5-group0' | ||
| ); | ||
| const actorB = makeActor( | ||
| 'actor-b', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group1`, | ||
| 'derived-slice5-group1' | ||
| ); | ||
|
|
||
| const { reusedActorUids } = simulateBuggyActorReuse( | ||
| [actorA, actorB], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| // actor-a is reused TWICE (the bug) | ||
| expect(reusedActorUids).toEqual(['actor-a', 'actor-a']); | ||
| // Actor A was stolen: ends up with group1's data instead of group0's | ||
| expect(actorA.referencedId).toBe('derived-slice6-group1'); | ||
| // Actor B was never touched | ||
| expect(actorB.referencedId).toBe('derived-slice5-group1'); | ||
| }); | ||
|
|
||
| it('handles single derived image (no overlapping segments)', () => { | ||
| const actorA = makeActor( | ||
| 'actor-a', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group0`, | ||
| 'derived-slice5-group0' | ||
| ); | ||
|
|
||
| const { reusedActorUids, fallbackCreations } = simulateActorReuse( | ||
| [actorA], | ||
| ['derived-slice6-group0'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(reusedActorUids).toEqual(['actor-a']); | ||
| expect(fallbackCreations).toBe(0); | ||
| expect(actorA.referencedId).toBe('derived-slice6-group0'); | ||
| }); | ||
|
|
||
| it('falls through to create new actor when all actors are consumed', () => { | ||
| const actorA = makeActor( | ||
| 'actor-a', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group0`, | ||
| 'derived-slice5-group0' | ||
| ); | ||
|
|
||
| const { reusedActorUids, fallbackCreations } = simulateActorReuse( | ||
| [actorA], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(reusedActorUids).toEqual(['actor-a']); | ||
| expect(fallbackCreations).toBe(1); | ||
| }); | ||
|
|
||
| it('does not reuse an actor that was already an exact match via consumed tracking', () => { | ||
| const actorA = makeActor( | ||
| 'actor-a', | ||
| `${SEG_ID}-${LABELMAP}-slice6_group0`, | ||
| 'derived-slice6-group0' | ||
| ); | ||
| const actorB = makeActor( | ||
| 'actor-b', | ||
| `${SEG_ID}-${LABELMAP}-slice5_group1`, | ||
| 'derived-slice5-group1' | ||
| ); | ||
|
|
||
| // group0 is an exact match for actorA (no reuse needed). | ||
| // group1 needs a reusable actor — actorA is first in the array and | ||
| // matches the prefix, so it gets reused (exact matches don't consume). | ||
| const { reusedActorUids, fallbackCreations } = simulateActorReuse( | ||
| [actorA, actorB], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(reusedActorUids).toEqual(['actor-a']); | ||
| expect(fallbackCreations).toBe(0); | ||
| }); | ||
| }); | ||
| }); |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember there was an actual reason for this, and i don't remember why. I let QA test it and maybe you fixed it or got fixed