Skip to content

Commit 895f934

Browse files
committed
perf(segmentation): stop allocating a closure per claimed voxel
masksHolding and masksClearing.claim swept the reaching masks with some/forEach, so every voxel a brush or polygon fill walked allocated a fresh callback capturing i, j and k. Both now sweep the reaching masks with a plain indexed loop, so the per-voxel path allocates nothing. The answers are unchanged.
1 parent ae2448d commit 895f934

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

‎src/segmentation/masks/overlap.ts‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,26 @@ const masksReaching = (masks: BoundedScalars[], within: Extent3D) =>
4848
/**
4949
* Whether any of these masks holds the voxel at PARENT indices i, j, k, over
5050
* the box the caller is about to walk. Absent when no mask reaches that box.
51+
*
52+
* The answer is asked once per voxel the caller walks, so the sweep over the
53+
* reaching masks is a plain indexed loop: a callback taking i, j, k would be a
54+
* fresh closure per voxel.
5155
*/
5256
export function masksHolding(masks: BoundedScalars[], within: Extent3D) {
5357
const reaching = masksReaching(masks, within);
5458
if (reaching.length === 0) return undefined;
55-
return (i: number, j: number, k: number) =>
56-
reaching.some(
57-
(bounded) =>
59+
return (i: number, j: number, k: number) => {
60+
for (let index = 0; index < reaching.length; index += 1) {
61+
const bounded = reaching[index];
62+
if (
5863
extentContainsIndex(bounded.extent, i, j, k) &&
5964
bounded.scalars[maskOffset(bounded, i, j, k)] !==
6065
LABELMAP_BACKGROUND_VALUE
61-
);
66+
)
67+
return true;
68+
}
69+
return false;
70+
};
6271
}
6372

6473
/**
@@ -73,14 +82,19 @@ export function masksClearing(masks: BoundedScalars[], within: Extent3D) {
7382
const reaching = masksReaching(masks, within);
7483
if (reaching.length === 0) return undefined;
7584
const changed = new Set<vtkLabelMap>();
85+
// Indexed loop, as in masksHolding: claim runs once per voxel the caller
86+
// walks, and a callback over the reaching masks would allocate per voxel.
7687
const claim = (i: number, j: number, k: number) => {
77-
reaching.forEach((bounded) => {
78-
if (!extentContainsIndex(bounded.extent, i, j, k)) return;
79-
const offset = maskOffset(bounded, i, j, k);
80-
if (bounded.scalars[offset] === LABELMAP_BACKGROUND_VALUE) return;
81-
bounded.scalars[offset] = LABELMAP_BACKGROUND_VALUE;
82-
changed.add(bounded.mask);
83-
});
88+
for (let index = 0; index < reaching.length; index += 1) {
89+
const bounded = reaching[index];
90+
if (extentContainsIndex(bounded.extent, i, j, k)) {
91+
const offset = maskOffset(bounded, i, j, k);
92+
if (bounded.scalars[offset] !== LABELMAP_BACKGROUND_VALUE) {
93+
bounded.scalars[offset] = LABELMAP_BACKGROUND_VALUE;
94+
changed.add(bounded.mask);
95+
}
96+
}
97+
}
8498
return true;
8599
};
86100
return {

0 commit comments

Comments
 (0)