Skip to content

Commit b01db7d

Browse files
committed
timetravel git -- add an LRU cache for git show
1 parent c2d62fb commit b01db7d

File tree

1 file changed

+16
-0
lines changed
  • src/packages/frontend/frame-editors/time-travel-editor

1 file changed

+16
-0
lines changed

src/packages/frontend/frame-editors/time-travel-editor/actions.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,25 @@ import {
3636
import { FrameTree } from "../frame-tree/types";
3737
import { export_to_json } from "./export-to-json";
3838
import type { Document } from "@cocalc/sync/editor/generic/types";
39+
import LRUCache from "lru-cache";
3940

4041
const EXTENSION = ".time-travel";
4142

43+
// We use a global cache so if user closes and opens file
44+
// later it is fast.
45+
const gitShowCache = new LRUCache<string, string>({
46+
maxSize: 10 * 10 ** 6, // 10MB
47+
sizeCalculation: (value, _key) => {
48+
return value.length + 1; // must be positive
49+
},
50+
});
51+
4252
/*interface FrameState {
4353
version: number;
4454
version0: number;
4555
version1: number;
4656
changes_mode: boolean;
57+
git_mode: boolean;
4758
}*/
4859

4960
export interface TimeTravelState extends CodeEditorState {
@@ -472,8 +483,13 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
472483
if (h == null) {
473484
return;
474485
}
486+
const key = `${h}:${this.docpath}`;
487+
if (gitShowCache.has(key)) {
488+
return gitShowCache.get(key);
489+
}
475490
try {
476491
const { stdout } = await this.gitCommand(["show"], h);
492+
gitShowCache.set(key, stdout);
477493
return stdout;
478494
} catch (err) {
479495
this.set_error(`${err}`);

0 commit comments

Comments
 (0)