Skip to content

Commit

Permalink
refactor: use cache to speed up duplicate calls parseTableStatements
Browse files Browse the repository at this point in the history
Especially important for sheet loading since this function will get called for every math field in a table but the result will always be the same
  • Loading branch information
mgreminger committed May 12, 2024
1 parent 6cf373f commit a653a69
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/TableCell.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import IconButton from "./IconButton.svelte";
import { deltaToMarkdown } from "quill-delta-to-markdown";
import { column } from "mathjs";
export let index: number;
export let tableCell: TableCell;
Expand Down
14 changes: 11 additions & 3 deletions src/cells/TableCell.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseCell, type DatabaseTableCell } from "./BaseCell";
import { MathField } from "./MathField";
import type { Statement } from "../parser/types";
import QuickLRU from "quick-lru";

class TableRowLabelField {
label: string;
Expand All @@ -26,6 +27,7 @@ export default class TableCell extends BaseCell {
rowJsons: string[];
richTextInstance: HTMLElement | null;
tableStatements: Statement[];
cache: QuickLRU<string, Statement>;

constructor (arg?: DatabaseTableCell) {
if (arg === undefined) {
Expand All @@ -43,6 +45,7 @@ export default class TableCell extends BaseCell {
this.rowJsons = [];
this.richTextInstance = null;
this.tableStatements = [];
this.cache = new QuickLRU<string, Statement>({maxSize: 100});
} else {
super("table", arg.id);
this.rowLabels = arg.rowLabels.map((label) => new TableRowLabelField(label));
Expand All @@ -57,6 +60,7 @@ export default class TableCell extends BaseCell {
this.rowJsons = arg.rowJsons;
this.richTextInstance = null;
this.tableStatements = [];
this.cache = new QuickLRU<string, Statement>({maxSize: 100});
}
}

Expand Down Expand Up @@ -104,9 +108,13 @@ export default class TableCell extends BaseCell {
this.rhsFields[rowIndex][colIndex].latex +
this.parameterUnitFields[colIndex].latex;

this.combinedFields[colIndex].parseLatex(combinedLatex);

this.tableStatements.push(this.combinedFields[colIndex].statement);
if (this.cache.has(combinedLatex)) {
this.tableStatements.push(this.cache.get(combinedLatex));
} else {
this.combinedFields[colIndex].parseLatex(combinedLatex);
this.tableStatements.push(this.combinedFields[colIndex].statement);
this.cache.set(combinedLatex, this.combinedFields[colIndex].statement)
}
}
}
}
Expand Down

0 comments on commit a653a69

Please sign in to comment.