Skip to content

Commit

Permalink
perf: ⚡️ On scroll, reset the prefetch queue
Browse files Browse the repository at this point in the history
On scroll with the scrollbar, reset the prefetch queue so that we don't
wait for images distant from the target. Build a new interleaved above
and below queue and fetch from there.
  • Loading branch information
JamesAPetts committed Sep 24, 2020
1 parent d3093d0 commit a3e851e
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/CornerstoneViewport/CornerstoneViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,85 @@ 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

0 comments on commit a3e851e

Please sign in to comment.