Skip to content

Commit

Permalink
cleanup overlays in the worker listener callback instead
Browse files Browse the repository at this point in the history
  • Loading branch information
sashankaryal committed Dec 12, 2024
1 parent d3bf174 commit 4f76488
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 38 deletions.
13 changes: 9 additions & 4 deletions app/packages/looker/src/lookers/abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,6 @@ export abstract class AbstractLooker<
const arrayBuffers: ArrayBuffer[] = [];

for (const overlay of this.pluckedOverlays ?? []) {
// we paint overlays again, so cleanup the old ones
// this helps prevent memory leaks from, for instance, dangling ImageBitmaps
overlay.cleanup();

let overlayData: LabelMask = null;

if ("mask" in overlay.label) {
Expand Down Expand Up @@ -739,13 +735,22 @@ export abstract class AbstractLooker<
);
}

protected cleanOverlays() {
for (const overlay of this.sampleOverlays ?? []) {
overlay.cleanup();
}
}

private loadSample(sample: Sample, transfer: Transferable[] = []) {
const messageUUID = uuid();

const labelsWorker = getLabelsWorker();

const listener = ({ data: { sample, coloring, uuid } }) => {
if (uuid === messageUUID) {
// we paint overlays again, so cleanup the old ones
// this helps prevent memory leaks from, for instance, dangling ImageBitmaps
this.cleanOverlays();
this.sample = sample;
this.state.options.coloring = coloring;
this.loadOverlays(sample);
Expand Down
1 change: 0 additions & 1 deletion app/packages/looker/src/overlays/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export interface SelectData {

export type LabelMask = {
bitmap?: ImageBitmap;
closedBitmapDims?: { width: number; height: number };
data?: OverlayMask;
};

Expand Down
12 changes: 2 additions & 10 deletions app/packages/looker/src/overlays/detection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,8 @@ export default class DetectionOverlay<
}

public cleanup(): void {
if (this.label.mask?.bitmap) {
// store height and width in bitmap object since it might be used again
const height = this.label.mask.bitmap.height;
const width = this.label.mask.bitmap.width;

this.label.mask?.bitmap.close();
this.label.mask.bitmap = null;

this.label.mask.closedBitmapDims = { width, height };
}
this.label.mask?.bitmap?.close();
this.label.mask.bitmap = null;
}
}

Expand Down
12 changes: 2 additions & 10 deletions app/packages/looker/src/overlays/heatmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,16 +207,8 @@ export default class HeatmapOverlay<State extends BaseState>
}

public cleanup(): void {
if (this.label.map?.bitmap) {
// store height and width in bitmap object since it might be used again
const height = this.label.map.bitmap.height;
const width = this.label.map.bitmap.width;

this.label.map?.bitmap.close();
this.label.map.bitmap = null;

this.label.map.closedBitmapDims = { width, height };
}
this.label.map?.bitmap?.close();
this.label.map.bitmap = null;
}
}

Expand Down
12 changes: 2 additions & 10 deletions app/packages/looker/src/overlays/segmentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,8 @@ export default class SegmentationOverlay<State extends BaseState>
}

public cleanup(): void {
if (this.label.mask?.bitmap) {
// store height and width in bitmap object since it might be used again
const height = this.label.mask.bitmap.height;
const width = this.label.mask.bitmap.width;

this.label.mask?.bitmap.close();
this.label.mask.bitmap = null;

this.label.mask.closedBitmapDims = { width, height };
}
this.label.mask?.bitmap?.close();
this.label.mask.bitmap = null;
}
}

Expand Down
11 changes: 8 additions & 3 deletions app/packages/looker/src/worker/disk-overlay-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@ export const decodeOverlayOnDisk = async (
// it's possible we're just re-coloring, in which case re-init mask image and set bitmap to null
if (
label[overlayField] &&
label[overlayField].closedBitmapDims &&
label[overlayField].bitmap &&
!label[overlayField].image
) {
const height = label[overlayField].closedBitmapDims.height;
const width = label[overlayField].closedBitmapDims.width;
const height = label[overlayField].bitmap.height;
const width = label[overlayField].bitmap.width;

// close the copied bitmap
label[overlayField].bitmap.close();
label[overlayField].bitmap = null;

label[overlayField].image = new ArrayBuffer(height * width * 4);
label[overlayField].bitmap.close();
label[overlayField].bitmap = null;
Expand Down

0 comments on commit 4f76488

Please sign in to comment.