Skip to content

Commit e631640

Browse files
committed
Optimized parsers for speed & memory usage
Switches to lazy parsing of diff chunks
1 parent eeff31c commit e631640

16 files changed

+338
-335
lines changed

src/annotations/diffAnnotationProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class DiffAnnotationProvider extends AnnotationProviderBase {
4141
const decorators: DecorationOptions[] = [];
4242

4343
for (const chunk of diff.chunks) {
44-
let count = chunk.currentStart - 2;
44+
let count = chunk.currentPosition.start - 2;
4545
for (const change of chunk.current) {
4646
if (change === undefined) continue;
4747

src/commands/openCommitInRemote.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { Arrays } from '../system';
33
import { commands, TextEditor, TextEditorEdit, Uri, window } from 'vscode';
44
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
5-
import { GitCommit, GitService, GitUri } from '../gitService';
5+
import { GitBlameCommit, GitService, GitUri } from '../gitService';
66
import { Logger } from '../logger';
77
import { Messages } from '../messages';
88
import { OpenInRemoteCommandArgs } from './openInRemote';
@@ -33,7 +33,7 @@ export class OpenCommitInRemoteCommand extends ActiveEditorCommand {
3333
let commit = blame.commit;
3434
// If the line is uncommitted, find the previous commit
3535
if (commit.isUncommitted) {
36-
commit = new GitCommit(commit.type, commit.repoPath, commit.previousSha!, commit.previousFileName!, commit.author, commit.date, commit.message);
36+
commit = new GitBlameCommit(commit.repoPath, commit.previousSha!, commit.previousFileName!, commit.author, commit.date, commit.message, []);
3737
}
3838

3939
const remotes = Arrays.uniqueBy(await this.git.getRemotes(gitUri.repoPath), _ => _.url, _ => !!_.provider);

src/git/models/blame.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
'use strict';
2-
import { GitAuthor, GitCommit, GitCommitLine } from './commit';
2+
import { GitAuthor, GitCommitLine } from './commit';
3+
import { GitBlameCommit } from './blameCommit';
34

45
export interface GitBlame {
56
repoPath: string;
67
authors: Map<string, GitAuthor>;
7-
commits: Map<string, GitCommit>;
8+
commits: Map<string, GitBlameCommit>;
89
lines: GitCommitLine[];
910
}
1011

1112
export interface GitBlameLine {
1213
author: GitAuthor;
13-
commit: GitCommit;
14+
commit: GitBlameCommit;
1415
line: GitCommitLine;
1516
}
1617

@@ -20,6 +21,6 @@ export interface GitBlameLines extends GitBlame {
2021

2122
export interface GitBlameCommitLines {
2223
author: GitAuthor;
23-
commit: GitCommit;
24+
commit: GitBlameCommit;
2425
lines: GitCommitLine[];
2526
}

src/git/models/blameCommit.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
import { GitCommit, GitCommitLine } from './commit';
3+
4+
export class GitBlameCommit extends GitCommit {
5+
6+
constructor(
7+
repoPath: string,
8+
sha: string,
9+
fileName: string,
10+
author: string,
11+
date: Date,
12+
message: string,
13+
public lines: GitCommitLine[],
14+
originalFileName?: string,
15+
previousSha?: string,
16+
previousFileName?: string
17+
) {
18+
super('blame', repoPath, sha, fileName, author, date, message, originalFileName, previousSha, previousFileName);
19+
}
20+
}

src/git/models/commit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export type GitCommitType = 'blame' | 'branch' | 'file' | 'stash';
2121
export class GitCommit {
2222

2323
type: GitCommitType;
24-
lines: GitCommitLine[];
24+
// lines: GitCommitLine[];
2525
originalFileName?: string;
2626
previousSha?: string;
2727
previousFileName?: string;
@@ -36,15 +36,15 @@ export class GitCommit {
3636
public author: string,
3737
public date: Date,
3838
public message: string,
39-
lines?: GitCommitLine[],
39+
// lines?: GitCommitLine[],
4040
originalFileName?: string,
4141
previousSha?: string,
4242
previousFileName?: string
4343
) {
4444
this.type = type;
4545
this.fileName = this.fileName && this.fileName.replace(/, ?$/, '');
4646

47-
this.lines = lines || [];
47+
// this.lines = lines || [];
4848
this.originalFileName = originalFileName;
4949
this.previousSha = previousSha;
5050
this.previousFileName = previousFileName;

src/git/models/diff.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,41 @@
11
'use strict';
2+
import { GitDiffParser } from '../parsers/diffParser';
23

34
export interface GitDiffLine {
45
line: string;
56
state: 'added' | 'removed' | 'unchanged';
67
}
78

8-
export interface GitDiffChunk {
9-
current: (GitDiffLine | undefined)[];
10-
currentStart: number;
11-
currentEnd: number;
9+
export class GitDiffChunk {
1210

13-
previous: (GitDiffLine | undefined)[];
14-
previousStart: number;
15-
previousEnd: number;
11+
private _chunk: string | undefined;
12+
private _current: (GitDiffLine | undefined)[] | undefined;
13+
private _previous: (GitDiffLine | undefined)[] | undefined;
1614

17-
chunk?: string;
15+
constructor(chunk: string, public currentPosition: { start: number, end: number }, public previousPosition: { start: number, end: number }) {
16+
this._chunk = chunk;
17+
}
18+
19+
get current(): (GitDiffLine | undefined)[] {
20+
if (this._chunk !== undefined) {
21+
this.parseChunk();
22+
}
23+
24+
return this._current!;
25+
}
26+
27+
get previous(): (GitDiffLine | undefined)[] {
28+
if (this._chunk !== undefined) {
29+
this.parseChunk();
30+
}
31+
32+
return this._previous!;
33+
}
34+
35+
private parseChunk() {
36+
[this._current, this._previous] = GitDiffParser.parseChunk(this._chunk!);
37+
this._chunk = undefined;
38+
}
1839
}
1940

2041
export interface GitDiff {

src/git/models/logCommit.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
import { Uri } from 'vscode';
3-
import { GitCommit, GitCommitLine, GitCommitType } from './commit';
3+
import { GitCommit, GitCommitType } from './commit';
44
import { GitStatusFileStatus, IGitStatusFile } from './status';
55
import * as path from 'path';
66

@@ -23,12 +23,11 @@ export class GitLogCommit extends GitCommit {
2323
message: string,
2424
status?: GitStatusFileStatus,
2525
fileStatuses?: IGitStatusFile[],
26-
lines?: GitCommitLine[],
2726
originalFileName?: string,
2827
previousSha?: string,
2928
previousFileName?: string
3029
) {
31-
super(type, repoPath, sha, fileName, author, date, message, lines, originalFileName, previousSha, previousFileName);
30+
super(type, repoPath, sha, fileName, author, date, message, originalFileName, previousSha, previousFileName);
3231

3332
this.fileNames = this.fileName;
3433

src/git/models/models.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22
export * from './blame';
3+
export * from './blameCommit';
34
export * from './branch';
45
export * from './commit';
56
export * from './diff';

src/git/models/stashCommit.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
import { GitCommitLine } from './commit';
32
import { GitLogCommit } from './logCommit';
43
import { GitStatusFileStatus, IGitStatusFile } from './status';
54

@@ -14,12 +13,11 @@ export class GitStashCommit extends GitLogCommit {
1413
message: string,
1514
status?: GitStatusFileStatus,
1615
fileStatuses?: IGitStatusFile[],
17-
lines?: GitCommitLine[],
1816
originalFileName?: string,
1917
previousSha?: string,
2018
previousFileName?: string
2119
) {
22-
super('stash', repoPath, sha, fileName, 'You', date, message, status, fileStatuses, lines, originalFileName, previousSha, previousFileName);
20+
super('stash', repoPath, sha, fileName, 'You', date, message, status, fileStatuses, originalFileName, previousSha, previousFileName);
2321
}
2422

2523
get shortSha() {

0 commit comments

Comments
 (0)