Skip to content

Commit

Permalink
Attempt at performance improvements in recalculateVminsumForAffectedP…
Browse files Browse the repository at this point in the history
…ixels

Replaced marked with a weird LRU-esque set thing. This array gets to be
enormous but only the last few items are ever relevant.

Removed enqueued, this also gets enormous and we can just rely on marked
stopping these pixels getting re-processed.
  • Loading branch information
Alligator committed Feb 25, 2021
1 parent 5140d40 commit 05c4c87
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions SeamCarver.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ const GREEN = 1;
const BLUE = 2;
const BORDER_ENERGY = 1000;

class LRUCache {
constructor(max = 10) {
this.max = max;
this.cache = new Map();
}

has(key) { return this.cache.has(key); }
set(key) {
if (this.cache.size > this.max) {
this.cache.delete(this.cache.keys().next().value);
}
if (this.cache.has(key)) {
return;
}
this.cache.set(key, 1);
}
}

/** Seam carver removes low energy seams in an image from HTML5 canvas. */
class SeamCarver {

Expand Down Expand Up @@ -310,9 +328,7 @@ class SeamCarver {
* Recalculate vminsum for affected pixels
*/
recalculateVminsumForAffectedPixels(queue) {
var marked = {};
var enqueued = {};
var maxRow = -1;
var marked = new LRUCache(16);
// start at second to last row
var row = this.height - 2;
var enqueuedCols = queue[row];
Expand All @@ -327,9 +343,11 @@ class SeamCarver {
if (enqueuedCols.length === 0) enqueuedCols = queue[--row];

// already explored this pixel
if (marked[pixelIndex]) continue;
if (marked.has(pixelIndex)) {
continue;
}

marked[pixelIndex] = true;
marked.set(pixelIndex);

var nodeEnergy = this.energyMatrix[col][row];
var oldVminsum = this.minsumMatrix[col][row];
Expand All @@ -355,11 +373,7 @@ class SeamCarver {

// enqueue three affected children from row above
for (var i = Math.max(col - 1, 0); i < Math.min(col + 2, lastCol + 1); i ++) {
var childIndex = this.pixelToIndex(i, row - 1);
if (!enqueued[childIndex]) {
enqueued[childIndex] = true;
queue[row - 1].push(i);
}
queue[row - 1].push(i);
}
}
}
Expand Down

0 comments on commit 05c4c87

Please sign in to comment.