Skip to content

Commit 81f931e

Browse files
committed
Adds memoization to many members
1 parent 6d9a666 commit 81f931e

File tree

7 files changed

+182
-147
lines changed

7 files changed

+182
-147
lines changed

src/git/models/branch.ts

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { StarredBranches, WorkspaceState } from '../../constants';
33
import { Container } from '../../container';
44
import { Git } from '../git';
55
import { GitStatus } from './status';
6+
import { memoize } from '../../system';
67

78
export interface GitTrackingState {
89
ahead: number;
@@ -44,26 +45,19 @@ export class GitBranch {
4445
return this.detached ? this.sha! : this.name;
4546
}
4647

47-
private _basename: string | undefined;
48+
@memoize()
4849
getBasename(): string {
49-
if (this._basename === undefined) {
50-
const name = this.getName();
51-
const index = name.lastIndexOf('/');
52-
this._basename = index !== -1 ? name.substring(index + 1) : name;
53-
}
54-
55-
return this._basename;
50+
const name = this.getName();
51+
const index = name.lastIndexOf('/');
52+
return index !== -1 ? name.substring(index + 1) : name;
5653
}
5754

58-
private _name: string | undefined;
55+
@memoize()
5956
getName(): string {
60-
if (this._name === undefined) {
61-
this._name = this.remote ? this.name.substring(this.name.indexOf('/') + 1) : this.name;
62-
}
63-
64-
return this._name;
57+
return this.remote ? this.name.substring(this.name.indexOf('/') + 1) : this.name;
6558
}
6659

60+
@memoize()
6761
getRemote(): string | undefined {
6862
if (this.remote) return GitBranch.getRemote(this.name);
6963
if (this.tracking !== undefined) return GitBranch.getRemote(this.tracking);
@@ -111,14 +105,14 @@ export class GitBranch {
111105
await Container.context.workspaceState.update(WorkspaceState.StarredBranches, starred);
112106
}
113107

114-
static getRemote(branch: string): string {
115-
return branch.substring(0, branch.indexOf('/'));
116-
}
117-
118108
static formatDetached(sha: string): string {
119109
return `(${Git.shortenSha(sha)}...)`;
120110
}
121111

112+
static getRemote(branch: string): string {
113+
return branch.substring(0, branch.indexOf('/'));
114+
}
115+
122116
static isDetached(name: string): boolean {
123117
// If there is whitespace in the name assume this is not a valid branch name
124118
// Deals with detached HEAD states

src/git/models/commit.ts

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Uri } from 'vscode';
33
import { configuration, DateSource, DateStyle, GravatarDefaultStyle } from '../../configuration';
44
import { Container } from '../../container';
5-
import { Dates } from '../../system';
5+
import { Dates, memoize } from '../../system';
66
import { CommitFormatter } from '../formatters/formatters';
77
import { Git } from '../git';
88
import { GitUri } from '../gitUri';
@@ -75,12 +75,9 @@ export abstract class GitCommit {
7575
: this.formatDateFromNow();
7676
}
7777

78-
private _shortSha: string | undefined;
78+
@memoize()
7979
get shortSha() {
80-
if (this._shortSha === undefined) {
81-
this._shortSha = Git.shortenSha(this.sha);
82-
}
83-
return this._shortSha;
80+
return Git.shortenSha(this.sha);
8481
}
8582

8683
get isFile() {
@@ -95,20 +92,14 @@ export abstract class GitCommit {
9592
return this.type === GitCommitType.Stash || this.type === GitCommitType.StashFile;
9693
}
9794

98-
private _isStagedUncommitted: boolean | undefined;
95+
@memoize()
9996
get isStagedUncommitted(): boolean {
100-
if (this._isStagedUncommitted === undefined) {
101-
this._isStagedUncommitted = Git.isUncommittedStaged(this.sha);
102-
}
103-
return this._isStagedUncommitted;
97+
return Git.isUncommittedStaged(this.sha);
10498
}
10599

106-
private _isUncommitted: boolean | undefined;
100+
@memoize()
107101
get isUncommitted(): boolean {
108-
if (this._isUncommitted === undefined) {
109-
this._isUncommitted = Git.isUncommitted(this.sha);
110-
}
111-
return this._isUncommitted;
102+
return Git.isUncommitted(this.sha);
112103
}
113104

114105
get previousFileSha(): string {
@@ -123,33 +114,71 @@ export abstract class GitCommit {
123114
return this.previousFileName ? GitUri.resolveToUri(this.previousFileName, this.repoPath) : this.uri;
124115
}
125116

117+
@memoize()
126118
get uri(): Uri {
127119
return GitUri.resolveToUri(this.fileName, this.repoPath);
128120
}
129121

130-
private _workingUriPromise: Promise<Uri | undefined> | undefined;
131-
getWorkingUri(): Promise<Uri | undefined> | undefined {
132-
if (this._workingUriPromise === undefined) {
133-
this._workingUriPromise = Container.git.getWorkingUri(this.repoPath, this.uri);
134-
}
122+
// @memoize()
123+
// getFileStatus() {
124+
// if (!this.isFile) return Promise.resolve(undefined);
125+
126+
// return Container.git.getStatusForFile(this.repoPath, this.fileName);
127+
// }
135128

136-
return this._workingUriPromise;
129+
// @memoize(
130+
// (uri: Uri, ref: string | undefined, editorLine?: number) =>
131+
// `${uri.toString(true)}|${ref || ''}|${editorLine || ''}`
132+
// )
133+
getPreviousDiffUris(uri: Uri, ref: string | undefined, editorLine?: number) {
134+
if (!this.isFile) return Promise.resolve(undefined);
135+
136+
return Container.git.getPreviousDiffUris(this.repoPath, uri, ref, 0, editorLine);
137+
}
138+
139+
// private _previousUriPromise: Promise<GitUri | undefined> | undefined;
140+
// getPreviousUri(staged: boolean = false) {
141+
// if (!this.isFile) return Promise.resolve(undefined);
142+
// if (!this.isUncommitted && this.previousSha !== undefined && !GitService.isShaParent(this.previousSha)) {
143+
// return Promise.resolve(this.toGitUri(true));
144+
// }
145+
146+
// if (this._previousUriPromise === undefined) {
147+
// this._previousUriPromise = this._getPreviousUriCore(staged);
148+
// }
149+
150+
// return this._previousUriPromise;
151+
// }
152+
153+
// private async _getPreviousUriCore(staged: boolean) {
154+
// if (!staged && !GitService.isStagedUncommitted(this.sha)) {
155+
// const status = await this.getFileStatus();
156+
// if (status !== undefined) {
157+
// // If the file is staged, diff with the staged version
158+
// if (status.indexStatus !== undefined) {
159+
// return GitUri.fromFile(this.fileName, this.repoPath, GitService.stagedUncommittedSha);
160+
// }
161+
// }
162+
// }
163+
164+
// return Container.git.getPreviousUri(this.repoPath, this.uri, this.sha);
165+
// }
166+
167+
@memoize()
168+
getWorkingUri(): Promise<Uri | undefined> {
169+
if (!this.isFile) return Promise.resolve(undefined);
170+
171+
return Container.git.getWorkingUri(this.repoPath, this.uri);
137172
}
138173

139-
private _authorDateFormatter: Dates.DateFormatter | undefined;
174+
@memoize()
140175
private get authorDateFormatter(): Dates.DateFormatter {
141-
if (this._authorDateFormatter === undefined) {
142-
this._authorDateFormatter = Dates.toFormatter(this.authorDate);
143-
}
144-
return this._authorDateFormatter;
176+
return Dates.toFormatter(this.authorDate);
145177
}
146178

147-
private _committerDateFormatter: Dates.DateFormatter | undefined;
179+
@memoize()
148180
private get committerDateFormatter(): Dates.DateFormatter {
149-
if (this._committerDateFormatter === undefined) {
150-
this._committerDateFormatter = Dates.toFormatter(this.committerDate);
151-
}
152-
return this._committerDateFormatter;
181+
return Dates.toFormatter(this.committerDate);
153182
}
154183

155184
private get dateFormatter(): Dates.DateFormatter {
@@ -158,6 +187,7 @@ export abstract class GitCommit {
158187
: this.authorDateFormatter;
159188
}
160189

190+
@memoize(format => (format == null ? 'MMMM Do, YYYY h:mma' : format))
161191
formatAuthorDate(format?: string | null) {
162192
if (format == null) {
163193
format = 'MMMM Do, YYYY h:mma';
@@ -170,6 +200,7 @@ export abstract class GitCommit {
170200
return this.authorDateFormatter.fromNow();
171201
}
172202

203+
@memoize(format => (format == null ? 'MMMM Do, YYYY h:mma' : format))
173204
formatCommitterDate(format?: string | null) {
174205
if (format == null) {
175206
format = 'MMMM Do, YYYY h:mma';
@@ -182,6 +213,7 @@ export abstract class GitCommit {
182213
return this.committerDateFormatter.fromNow();
183214
}
184215

216+
@memoize(format => (format == null ? 'MMMM Do, YYYY h:mma' : format))
185217
formatDate(format?: string | null) {
186218
if (format == null) {
187219
format = 'MMMM Do, YYYY h:mma';
@@ -202,11 +234,13 @@ export abstract class GitCommit {
202234
return getGravatarUri(this.email, fallback, size);
203235
}
204236

237+
@memoize()
205238
getShortMessage() {
206239
// eslint-disable-next-line no-template-curly-in-string
207240
return CommitFormatter.fromTemplate('${message}', this, { truncateMessageAtNewLine: true });
208241
}
209242

243+
@memoize()
210244
toGitUri(previous: boolean = false): GitUri {
211245
return GitUri.fromCommit(this, previous);
212246
}

src/git/models/diff.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
import { GitDiffParser } from '../parsers/diffParser';
3+
import { memoize } from '../../system';
34

45
export interface GitDiffLine {
56
line: string;
@@ -13,20 +14,15 @@ export interface GitDiffHunkLine {
1314
}
1415

1516
export class GitDiffHunk {
16-
private _lines: GitDiffHunkLine[] | undefined;
17-
1817
constructor(
1918
public readonly diff: string,
2019
public currentPosition: { start: number; end: number },
2120
public previousPosition: { start: number; end: number }
2221
) {}
2322

23+
@memoize()
2424
get lines(): GitDiffHunkLine[] {
25-
if (this._lines === undefined) {
26-
this._lines = GitDiffParser.parseHunk(this);
27-
}
28-
29-
return this._lines;
25+
return GitDiffParser.parseHunk(this);
3026
}
3127
}
3228

src/git/models/logCommit.ts

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
import * as paths from 'path';
33
import { Uri } from 'vscode';
4-
import { Strings } from '../../system';
4+
import { memoize, Strings } from '../../system';
55
import { GitUri } from '../gitUri';
66
import { GitCommit, GitCommitType } from './commit';
77
import { GitFile, GitFileStatus } from './file';
@@ -67,39 +67,31 @@ export class GitLogCommit extends GitCommit {
6767
return this.isFile ? this.previousSha! : `${this.sha}^`;
6868
}
6969

70-
private _diff?: {
71-
added: number;
72-
deleted: number;
73-
changed: number;
74-
};
75-
70+
@memoize()
7671
getDiffStatus() {
77-
if (this._diff === undefined) {
78-
this._diff = {
79-
added: 0,
80-
deleted: 0,
81-
changed: 0
82-
};
83-
84-
if (this.files.length !== 0) {
85-
for (const f of this.files) {
86-
switch (f.status) {
87-
case 'A':
88-
case '?':
89-
this._diff.added++;
90-
break;
91-
case 'D':
92-
this._diff.deleted++;
93-
break;
94-
default:
95-
this._diff.changed++;
96-
break;
97-
}
98-
}
72+
const diff = {
73+
added: 0,
74+
deleted: 0,
75+
changed: 0
76+
};
77+
if (this.files.length === 0) return diff;
78+
79+
for (const f of this.files) {
80+
switch (f.status) {
81+
case 'A':
82+
case '?':
83+
diff.added++;
84+
break;
85+
case 'D':
86+
diff.deleted++;
87+
break;
88+
default:
89+
diff.changed++;
90+
break;
9991
}
10092
}
10193

102-
return this._diff;
94+
return diff;
10395
}
10496

10597
getFormattedDiffStatus({

0 commit comments

Comments
 (0)