Skip to content

Commit

Permalink
perf: improve performance when expanding map
Browse files Browse the repository at this point in the history
  • Loading branch information
kylehue committed Mar 29, 2024
1 parent ba8ecaf commit 13cf823
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export const useProjectStore = defineStore("project", () => {
}

function makeLayersMatrixSizeUniform() {
if (!_layers.length) return;
// should only take care of visible layers
if (_layers.filter((v) => v.isVisible).length <= 1) return;

const largestNonEmptyLayer = _layers
.filter((v) => v.isVisible)
Expand Down
43 changes: 40 additions & 3 deletions src/utils/MapMatrix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ export class MapMatrix {
private separator = " ";
private minSize: number | null = null;
private nonEmptyTotalSize = 0;
private minRecordedRow = 0;
private maxRecordedRow = 0;
private minRecordedCol = 0;
private maxRecordedCol = 0;

constructor() {}

public setMinSize(minSize: number | null) {
if (typeof minSize == "number") {
minSize = minSize % 2 !== 0 ? minSize + 1 : minSize;
}

this.minSize = minSize;
this.updateMinSize();
}
Expand Down Expand Up @@ -124,6 +128,7 @@ export class MapMatrix {
}

trim() {
const oldSize = this.matrix.length;
const firstRowIsEmpty = () =>
this.matrix[0]?.every((v) => v === this.emptyMatrixId);
const lastRowIsEmpty = () =>
Expand All @@ -149,7 +154,9 @@ export class MapMatrix {
});
}

this._updateNonEmptyTotalSize();
if (oldSize !== this.matrix.length) {
this._updateNonEmptyTotalSize();
}
}

private getBounds() {
Expand All @@ -166,6 +173,13 @@ export class MapMatrix {
};
}

private _recordRowAndColumn(row: number, col: number) {
this.minRecordedRow = Math.min(row, this.minRecordedRow);
this.maxRecordedRow = Math.max(row, this.maxRecordedRow);
this.minRecordedCol = Math.min(col, this.minRecordedCol);
this.maxRecordedCol = Math.max(col, this.maxRecordedCol);
}

fill(row: number, col: number, matrixId: string) {
// Expand matrix if the row/col exceeds the matrix bounds
const oldBounds = this.getBounds();
Expand Down Expand Up @@ -224,7 +238,16 @@ export class MapMatrix {
this.matrix[centerRow + row - o1][centerCol + col - o2]
);

this._updateNonEmptyTotalSize();
const isBoundsExpanded =
row < this.minRecordedRow ||
row > this.maxRecordedRow ||
col < this.minRecordedCol ||
col > this.maxRecordedCol;
if (isBoundsExpanded) {
this._updateNonEmptyTotalSize();
}

this._recordRowAndColumn(row, col);
}

private _add(row: number, col: number, matrixId: string) {
Expand Down Expand Up @@ -258,6 +281,17 @@ export class MapMatrix {
const o2 = col <= 0 ? 0 : 1;
this.matrix[centerRow + row - o1] ??= [];
this.matrix[centerRow + row - o1][centerCol + col - o2] = matrixId;

const isBoundsExpanded =
row < this.minRecordedRow ||
row > this.maxRecordedRow ||
col < this.minRecordedCol ||
col > this.maxRecordedCol;
if (isBoundsExpanded) {
this._updateNonEmptyTotalSize();
}

this._recordRowAndColumn(row, col);
}

add(row: number, col: number, matrixId: string) {
Expand All @@ -268,6 +302,9 @@ export class MapMatrix {
private translate(rowStep: number, colStep: number) {
const rowStepAbs = Math.abs(rowStep);
const colStepAbs = Math.abs(colStep);

if (rowStepAbs === 0 && colStepAbs === 0) return;

const translated: string[][] = [];

for (let i = 0; i < this.matrix.length + rowStepAbs; i++) {
Expand Down

0 comments on commit 13cf823

Please sign in to comment.