Skip to content

Commit

Permalink
Adds memoization to many members
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed May 5, 2019
1 parent 6d9a666 commit 81f931e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 147 deletions.
30 changes: 12 additions & 18 deletions src/git/models/branch.ts
Expand Up @@ -3,6 +3,7 @@ import { StarredBranches, WorkspaceState } from '../../constants';
import { Container } from '../../container';
import { Git } from '../git';
import { GitStatus } from './status';
import { memoize } from '../../system';

export interface GitTrackingState {
ahead: number;
Expand Down Expand Up @@ -44,26 +45,19 @@ export class GitBranch {
return this.detached ? this.sha! : this.name;
}

private _basename: string | undefined;
@memoize()
getBasename(): string {
if (this._basename === undefined) {
const name = this.getName();
const index = name.lastIndexOf('/');
this._basename = index !== -1 ? name.substring(index + 1) : name;
}

return this._basename;
const name = this.getName();
const index = name.lastIndexOf('/');
return index !== -1 ? name.substring(index + 1) : name;
}

private _name: string | undefined;
@memoize()
getName(): string {
if (this._name === undefined) {
this._name = this.remote ? this.name.substring(this.name.indexOf('/') + 1) : this.name;
}

return this._name;
return this.remote ? this.name.substring(this.name.indexOf('/') + 1) : this.name;
}

@memoize()
getRemote(): string | undefined {
if (this.remote) return GitBranch.getRemote(this.name);
if (this.tracking !== undefined) return GitBranch.getRemote(this.tracking);
Expand Down Expand Up @@ -111,14 +105,14 @@ export class GitBranch {
await Container.context.workspaceState.update(WorkspaceState.StarredBranches, starred);
}

static getRemote(branch: string): string {
return branch.substring(0, branch.indexOf('/'));
}

static formatDetached(sha: string): string {
return `(${Git.shortenSha(sha)}...)`;
}

static getRemote(branch: string): string {
return branch.substring(0, branch.indexOf('/'));
}

static isDetached(name: string): boolean {
// If there is whitespace in the name assume this is not a valid branch name
// Deals with detached HEAD states
Expand Down
98 changes: 66 additions & 32 deletions src/git/models/commit.ts
Expand Up @@ -2,7 +2,7 @@
import { Uri } from 'vscode';
import { configuration, DateSource, DateStyle, GravatarDefaultStyle } from '../../configuration';
import { Container } from '../../container';
import { Dates } from '../../system';
import { Dates, memoize } from '../../system';
import { CommitFormatter } from '../formatters/formatters';
import { Git } from '../git';
import { GitUri } from '../gitUri';
Expand Down Expand Up @@ -75,12 +75,9 @@ export abstract class GitCommit {
: this.formatDateFromNow();
}

private _shortSha: string | undefined;
@memoize()
get shortSha() {
if (this._shortSha === undefined) {
this._shortSha = Git.shortenSha(this.sha);
}
return this._shortSha;
return Git.shortenSha(this.sha);
}

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

private _isStagedUncommitted: boolean | undefined;
@memoize()
get isStagedUncommitted(): boolean {
if (this._isStagedUncommitted === undefined) {
this._isStagedUncommitted = Git.isUncommittedStaged(this.sha);
}
return this._isStagedUncommitted;
return Git.isUncommittedStaged(this.sha);
}

private _isUncommitted: boolean | undefined;
@memoize()
get isUncommitted(): boolean {
if (this._isUncommitted === undefined) {
this._isUncommitted = Git.isUncommitted(this.sha);
}
return this._isUncommitted;
return Git.isUncommitted(this.sha);
}

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

@memoize()
get uri(): Uri {
return GitUri.resolveToUri(this.fileName, this.repoPath);
}

private _workingUriPromise: Promise<Uri | undefined> | undefined;
getWorkingUri(): Promise<Uri | undefined> | undefined {
if (this._workingUriPromise === undefined) {
this._workingUriPromise = Container.git.getWorkingUri(this.repoPath, this.uri);
}
// @memoize()
// getFileStatus() {
// if (!this.isFile) return Promise.resolve(undefined);

// return Container.git.getStatusForFile(this.repoPath, this.fileName);
// }

return this._workingUriPromise;
// @memoize(
// (uri: Uri, ref: string | undefined, editorLine?: number) =>
// `${uri.toString(true)}|${ref || ''}|${editorLine || ''}`
// )
getPreviousDiffUris(uri: Uri, ref: string | undefined, editorLine?: number) {
if (!this.isFile) return Promise.resolve(undefined);

return Container.git.getPreviousDiffUris(this.repoPath, uri, ref, 0, editorLine);
}

// private _previousUriPromise: Promise<GitUri | undefined> | undefined;
// getPreviousUri(staged: boolean = false) {
// if (!this.isFile) return Promise.resolve(undefined);
// if (!this.isUncommitted && this.previousSha !== undefined && !GitService.isShaParent(this.previousSha)) {
// return Promise.resolve(this.toGitUri(true));
// }

// if (this._previousUriPromise === undefined) {
// this._previousUriPromise = this._getPreviousUriCore(staged);
// }

// return this._previousUriPromise;
// }

// private async _getPreviousUriCore(staged: boolean) {
// if (!staged && !GitService.isStagedUncommitted(this.sha)) {
// const status = await this.getFileStatus();
// if (status !== undefined) {
// // If the file is staged, diff with the staged version
// if (status.indexStatus !== undefined) {
// return GitUri.fromFile(this.fileName, this.repoPath, GitService.stagedUncommittedSha);
// }
// }
// }

// return Container.git.getPreviousUri(this.repoPath, this.uri, this.sha);
// }

@memoize()
getWorkingUri(): Promise<Uri | undefined> {
if (!this.isFile) return Promise.resolve(undefined);

return Container.git.getWorkingUri(this.repoPath, this.uri);
}

private _authorDateFormatter: Dates.DateFormatter | undefined;
@memoize()
private get authorDateFormatter(): Dates.DateFormatter {
if (this._authorDateFormatter === undefined) {
this._authorDateFormatter = Dates.toFormatter(this.authorDate);
}
return this._authorDateFormatter;
return Dates.toFormatter(this.authorDate);
}

private _committerDateFormatter: Dates.DateFormatter | undefined;
@memoize()
private get committerDateFormatter(): Dates.DateFormatter {
if (this._committerDateFormatter === undefined) {
this._committerDateFormatter = Dates.toFormatter(this.committerDate);
}
return this._committerDateFormatter;
return Dates.toFormatter(this.committerDate);
}

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

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

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

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

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

@memoize()
toGitUri(previous: boolean = false): GitUri {
return GitUri.fromCommit(this, previous);
}
Expand Down
10 changes: 3 additions & 7 deletions src/git/models/diff.ts
@@ -1,5 +1,6 @@
'use strict';
import { GitDiffParser } from '../parsers/diffParser';
import { memoize } from '../../system';

export interface GitDiffLine {
line: string;
Expand All @@ -13,20 +14,15 @@ export interface GitDiffHunkLine {
}

export class GitDiffHunk {
private _lines: GitDiffHunkLine[] | undefined;

constructor(
public readonly diff: string,
public currentPosition: { start: number; end: number },
public previousPosition: { start: number; end: number }
) {}

@memoize()
get lines(): GitDiffHunkLine[] {
if (this._lines === undefined) {
this._lines = GitDiffParser.parseHunk(this);
}

return this._lines;
return GitDiffParser.parseHunk(this);
}
}

Expand Down
52 changes: 22 additions & 30 deletions src/git/models/logCommit.ts
@@ -1,7 +1,7 @@
'use strict';
import * as paths from 'path';
import { Uri } from 'vscode';
import { Strings } from '../../system';
import { memoize, Strings } from '../../system';
import { GitUri } from '../gitUri';
import { GitCommit, GitCommitType } from './commit';
import { GitFile, GitFileStatus } from './file';
Expand Down Expand Up @@ -67,39 +67,31 @@ export class GitLogCommit extends GitCommit {
return this.isFile ? this.previousSha! : `${this.sha}^`;
}

private _diff?: {
added: number;
deleted: number;
changed: number;
};

@memoize()
getDiffStatus() {
if (this._diff === undefined) {
this._diff = {
added: 0,
deleted: 0,
changed: 0
};

if (this.files.length !== 0) {
for (const f of this.files) {
switch (f.status) {
case 'A':
case '?':
this._diff.added++;
break;
case 'D':
this._diff.deleted++;
break;
default:
this._diff.changed++;
break;
}
}
const diff = {
added: 0,
deleted: 0,
changed: 0
};
if (this.files.length === 0) return diff;

for (const f of this.files) {
switch (f.status) {
case 'A':
case '?':
diff.added++;
break;
case 'D':
diff.deleted++;
break;
default:
diff.changed++;
break;
}
}

return this._diff;
return diff;
}

getFormattedDiffStatus({
Expand Down

0 comments on commit 81f931e

Please sign in to comment.