-
Notifications
You must be signed in to change notification settings - Fork 503
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
Closed
pedrokohler
wants to merge
2
commits into
cornerstonejs:main
from
pedrokohler:fix/memory-leak-vtk-actor-reuse-on-scroll
Closed
Changes from all commits
Commits
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
195 changes: 195 additions & 0 deletions
195
packages/tools/src/tools/displayTools/Labelmap/syncStackLabelmapActors.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,195 @@ | ||
| import { SegmentationRepresentations } from '../../../enums'; | ||
|
|
||
| const SEG_ID = 'seg-1'; | ||
| const LABELMAP = SegmentationRepresentations.Labelmap; | ||
|
|
||
| interface MockActorEntry { | ||
| uid: string; | ||
| representationUID: string; | ||
| referencedId: string; | ||
| } | ||
|
|
||
| function makeActor( | ||
| uid: string, | ||
| referencedId: string | ||
| ): MockActorEntry { | ||
| return { | ||
| uid, | ||
| referencedId, | ||
| representationUID: `${SEG_ID}-${LABELMAP}-${referencedId}`, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Mirrors the in-place actor reuse algorithm implemented by | ||
| * {@link syncStackLabelmapActors} for the legacy StackViewport path: | ||
| * | ||
| * - actors whose referencedId is no longer among the current slice's derived | ||
| * image ids form a reuse pool; | ||
| * - each derived image id reuses (consumes) one pooled actor at most once so | ||
| * overlapping segment groups cannot steal one another's actor; | ||
| * - when the pool is empty a new actor is created; | ||
| * - pooled actors that are never reused are removed. | ||
| */ | ||
| function simulateSync( | ||
| existingActors: MockActorEntry[], | ||
| derivedImageIds: string[], | ||
| segmentationId: string | ||
| ) { | ||
| const derivedImageIdSet = new Set(derivedImageIds); | ||
| const reusablePool = existingActors.filter( | ||
| (actor) => !derivedImageIdSet.has(actor.referencedId) | ||
| ); | ||
|
|
||
| const updatedInPlaceUids: string[] = []; | ||
| const reusedActorUids: string[] = []; | ||
| let createdCount = 0; | ||
|
|
||
| derivedImageIds.forEach((derivedImageId) => { | ||
| const exactMatch = existingActors.find( | ||
| (actor) => actor.referencedId === derivedImageId | ||
| ); | ||
|
|
||
| if (exactMatch) { | ||
| updatedInPlaceUids.push(exactMatch.uid); | ||
| return; | ||
| } | ||
|
|
||
| const reusable = reusablePool.shift(); | ||
|
|
||
| if (reusable) { | ||
| reusedActorUids.push(reusable.uid); | ||
| reusable.referencedId = derivedImageId; | ||
| reusable.representationUID = `${segmentationId}-${LABELMAP}-${derivedImageId}`; | ||
| return; | ||
| } | ||
|
|
||
| createdCount++; | ||
| }); | ||
|
|
||
| const removedActorUids = reusablePool.map((actor) => actor.uid); | ||
|
|
||
| return { updatedInPlaceUids, reusedActorUids, createdCount, removedActorUids }; | ||
| } | ||
|
|
||
| /** | ||
| * The pre-fix path: it picked a reusable actor from the pool without consuming | ||
| * it, so two derived images (overlapping segment groups) could resolve to the | ||
| * same actor (actor theft), leaving the other group invisible. | ||
| */ | ||
| function simulateBuggyReuse( | ||
| existingActors: MockActorEntry[], | ||
| derivedImageIds: string[] | ||
| ) { | ||
| const derivedImageIdSet = new Set(derivedImageIds); | ||
| const reusablePool = existingActors.filter( | ||
| (actor) => !derivedImageIdSet.has(actor.referencedId) | ||
| ); | ||
| const reusedActorUids: string[] = []; | ||
|
|
||
| derivedImageIds.forEach((derivedImageId) => { | ||
| const exactMatch = existingActors.find( | ||
| (actor) => actor.referencedId === derivedImageId | ||
| ); | ||
|
|
||
| if (exactMatch) { | ||
| return; | ||
| } | ||
|
|
||
| const reusable = reusablePool[0]; // BUG: pool entry is never consumed | ||
|
|
||
| if (reusable) { | ||
| reusedActorUids.push(reusable.uid); | ||
| } | ||
| }); | ||
|
|
||
| return { reusedActorUids }; | ||
| } | ||
|
|
||
| describe('syncStackLabelmapActors in-place actor reuse', () => { | ||
| it('reuses a single actor on a normal scroll (no create, no remove)', () => { | ||
| const actorA = makeActor('actor-a', 'derived-slice5'); | ||
|
|
||
| const result = simulateSync([actorA], ['derived-slice6'], SEG_ID); | ||
|
|
||
| expect(result.reusedActorUids).toEqual(['actor-a']); | ||
| expect(result.createdCount).toBe(0); | ||
| expect(result.removedActorUids).toEqual([]); | ||
| expect(actorA.referencedId).toBe('derived-slice6'); | ||
| }); | ||
|
|
||
| it('assigns a distinct actor to each overlapping segment group', () => { | ||
| const actorA = makeActor('actor-a', 'derived-slice5-group0'); | ||
| const actorB = makeActor('actor-b', 'derived-slice5-group1'); | ||
|
|
||
| const result = simulateSync( | ||
| [actorA, actorB], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(result.reusedActorUids).toHaveLength(2); | ||
| expect(result.reusedActorUids[0]).not.toBe(result.reusedActorUids[1]); | ||
| expect(result.reusedActorUids).toEqual(['actor-a', 'actor-b']); | ||
| expect(actorA.referencedId).toBe('derived-slice6-group0'); | ||
| expect(actorB.referencedId).toBe('derived-slice6-group1'); | ||
| expect(result.createdCount).toBe(0); | ||
| expect(result.removedActorUids).toEqual([]); | ||
| }); | ||
|
|
||
| it('creates a new actor only once the reuse pool is exhausted', () => { | ||
| const actorA = makeActor('actor-a', 'derived-slice5-group0'); | ||
|
|
||
| const result = simulateSync( | ||
| [actorA], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(result.reusedActorUids).toEqual(['actor-a']); | ||
| expect(result.createdCount).toBe(1); | ||
| expect(result.removedActorUids).toEqual([]); | ||
| }); | ||
|
|
||
| it('removes pooled actors that are not reused when groups shrink', () => { | ||
| const actorA = makeActor('actor-a', 'derived-slice5-group0'); | ||
| const actorB = makeActor('actor-b', 'derived-slice5-group1'); | ||
|
|
||
| const result = simulateSync([actorA, actorB], ['derived-slice6'], SEG_ID); | ||
|
|
||
| expect(result.reusedActorUids).toEqual(['actor-a']); | ||
| expect(result.removedActorUids).toEqual(['actor-b']); | ||
| expect(result.createdCount).toBe(0); | ||
| }); | ||
|
|
||
| it('updates an exact-match actor in place without consuming the reuse pool', () => { | ||
| // group0 already references the current derived image (exact match); | ||
| // group1 needs a reusable actor from the pool. | ||
| const actorExact = makeActor('actor-exact', 'derived-slice6-group0'); | ||
| const actorStale = makeActor('actor-stale', 'derived-slice5-group1'); | ||
|
|
||
| const result = simulateSync( | ||
| [actorExact, actorStale], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'], | ||
| SEG_ID | ||
| ); | ||
|
|
||
| expect(result.updatedInPlaceUids).toEqual(['actor-exact']); | ||
| expect(result.reusedActorUids).toEqual(['actor-stale']); | ||
| expect(result.createdCount).toBe(0); | ||
| expect(result.removedActorUids).toEqual([]); | ||
| }); | ||
|
|
||
| it('demonstrates the actor theft that consumption prevents', () => { | ||
| const actorA = makeActor('actor-a', 'derived-slice5-group0'); | ||
| const actorB = makeActor('actor-b', 'derived-slice5-group1'); | ||
|
|
||
| const { reusedActorUids } = simulateBuggyReuse( | ||
| [actorA, actorB], | ||
| ['derived-slice6-group0', 'derived-slice6-group1'] | ||
| ); | ||
|
|
||
| // actor-a is stolen for both groups; actor-b is never used. | ||
| expect(reusedActorUids).toEqual(['actor-a', 'actor-a']); | ||
| }); | ||
| }); | ||
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.
📐 Maintainability & Code Quality | 🟠 Major | 🏗️ Heavy lift
Exercise
syncStackLabelmapActorsdirectly in this spec.simulateSyncandsimulateBuggyReusereimplement the intended algorithm instead of calling the production function, so this suite can stay green even ifsyncStackLabelmapActorsregresses in actor mutation, pool cleanup, or viewport interaction. Please rewrite these as tests around the real function with mocked viewport/actor state.🤖 Prompt for AI Agents