Skip to content

Commit

Permalink
perf: ⚡️ Fix precaching issues
Browse files Browse the repository at this point in the history
perf: ⚡️ Fix precaching issues
  • Loading branch information
JamesAPetts authored Sep 24, 2020
2 parents 832a82c + 417c14b commit 557594d
Showing 1 changed file with 4 additions and 93 deletions.
97 changes: 4 additions & 93 deletions src/CornerstoneViewport/CornerstoneViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,11 @@ class CornerstoneViewport extends Component {
const image = await cornerstone.loadAndCacheImage(imageId);

// Display

cornerstone.displayImage(this.element, image);

if (isStackPrefetchEnabled) {
_enableStackPrefetching(this.element);
cornerstoneTools.stackPrefetch.enable(this.element);
}

if (isPlaying) {
Expand Down Expand Up @@ -254,10 +255,7 @@ class CornerstoneViewport extends Component {

// Need to stop/start to pickup stack changes in prefetcher
if (shouldStopStartStackPrefetch) {
const clear = true;

_enableStackPrefetching(this.element, clear);
_enableStackPrefetching(this.element);
cornerstoneTools.stackPrefetch.enable(this.element);
}

// ~~ ACTIVE TOOL
Expand Down Expand Up @@ -319,7 +317,7 @@ class CornerstoneViewport extends Component {
this._setupLoadHandlers(clear);

if (this.props.isStackPrefetchEnabled) {
_enableStackPrefetching(this.element, clear);
cornerstoneTools.stackPrefetch.disable(this.element);
}

cornerstoneTools.clearToolState(this.element, 'stackPrefetch');
Expand Down Expand Up @@ -714,85 +712,6 @@ class CornerstoneViewport extends Component {
this.setViewportActive();

scrollToIndex(this.element, value);

this.resetPrefetching(value);
};

resetPrefetching = imageIdIndex => {
cornerstoneTools.stackPrefetch.setConfiguration({
maxImagesToPrefetch: Infinity,
preserveExistingPool: false,
maxSimultaneousRequests: 6,
});

const { imageIds } = this.props;

const minImageIdIndex = 0;
const maxImageIdIndex = imageIds.length - 1;

let lowerImageIdIndex = imageIdIndex;
let upperImageIdIndex = imageIdIndex;

// Build up an array of images to prefetch, starting with the current image.
let imageIdsToPrefetch = [imageIds[imageIdIndex]];

// 0: From current stack position down to minimum.
// 1: From current stack position up to maximum.

const prefetchQueuedFilled = [false, false];

// Check if on edges and some criteria is already fulfilled

if (imageIdIndex === minImageIdIndex) {
prefetchQueuedFilled[0] = true;
} else if (imageIdIndex === maxImageIdIndex) {
prefetchQueuedFilled[1] = true;
}

while (
prefetchQueuedFilled[0] === false ||
prefetchQueuedFilled[1] === false
) {
if (prefetchQueuedFilled[0] === false) {
// Add imageId bellow
lowerImageIdIndex--;
imageIdsToPrefetch.push(imageIds[lowerImageIdIndex]);

if (lowerImageIdIndex === minImageIdIndex) {
prefetchQueuedFilled[0] = true;
}
}

if (prefetchQueuedFilled[1] === false) {
// Add imageId above
upperImageIdIndex++;
imageIdsToPrefetch.push(imageIds[upperImageIdIndex]);

if (upperImageIdIndex === maxImageIdIndex) {
prefetchQueuedFilled[1] = true;
}
}
}

const requestPoolManager = cornerstoneTools.requestPoolManager;
const requestType = 'prefetch';
const preventCache = false;
const noop = () => {};

requestPoolManager.clearRequestStack('prefetch');

imageIdsToPrefetch.forEach(imageId => {
requestPoolManager.addRequest(
{},
imageId,
requestType,
preventCache,
noop,
noop
);
});

requestPoolManager.startGrabbing();
};

setViewportActive = () => {
Expand Down Expand Up @@ -886,14 +805,6 @@ function _trySetActiveTool(element, activeToolName) {
});
}

function _enableStackPrefetching(element, clear = false) {
if (clear) {
cornerstoneTools.stackPrefetch.disable(element);
} else {
cornerstoneTools.stackPrefetch.enable(element);
}
}

/**
* Iterate over the provided tools; Add each tool to the target element
*
Expand Down

0 comments on commit 557594d

Please sign in to comment.