Skip to content

improve status parsing with this one weird trick - #5186

Merged
iAmWillShepherd merged 2 commits into
shiftkey/status-with-spawnfrom
shiftkey/improved-porcelain-parsing
Jul 18, 2018
Merged

iAmWillShepherd merged 2 commits into
shiftkey/status-with-spawnfrom
shiftkey/improved-porcelain-parsing

Conversation

@shiftkey

Copy link
Copy Markdown
Member

I uncovered in #5053 (comment) that's there's a known O(N) issue with using Array.shift to return the first element from an array. This means parsePorcelainStatus can spend a significant amount of it's time churning on an array that's 100000 items or so (which can easily happen when users have a lot of untracked files by mistake).

The V8 team do have some tricks for optimizing this, but this discussion shows that it shouldn't be relied upon:

Array.shift() is an expensive operation, as it requires all array elements to be moved.

So I stumbled upon double-ended-queue which talks about this in more detail, and offers an easy way to convert an array to a queue.

Benchmarking

This is more paper-napkin-esque timings using the live app than any sort of structured benchmarking, but here's where I added timings to the getStatus call so I can see how time is spent:

diff --git a/app/src/lib/git/status.ts b/app/src/lib/git/status.ts
index 7390d855e..cbfcaebe9 100644
--- a/app/src/lib/git/status.ts
+++ b/app/src/lib/git/status.ts
@@ -64,11 +64,13 @@ function convertToAppStatus(status: FileEntry): AppFileStatus {
 export async function getStatus(
   repository: Repository
 ): Promise<IStatusResult | null> {
+  console.time(`status git invocation`)
   const result = await spawnAndComplete(
     ['status', '--untracked-files=all', '--branch', '--porcelain=2', '-z'],
     repository.path,
     'getStatus'
   )
+  console.timeEnd(`status git invocation`)
 
   if (result.output.length > MaxStatusBufferSize) {
     log.error(
@@ -81,6 +83,7 @@ export async function getStatus(
 
   const stdout = result.output.toString('utf8')
 
+  console.time(`status parsing`)
   // Map of files keyed on their paths.
   // Note, map maintains insertion order
   const files = new Map<string, WorkingDirectoryFileChange>()
@@ -90,7 +93,11 @@ export async function getStatus(
   let currentTip: string | undefined = undefined
   let branchAheadBehind: IAheadBehind | undefined = undefined
 
-  for (const entry of parsePorcelainStatus(stdout)) {
+  console.time(`parse porcelain`)
+  const status = parsePorcelainStatus(stdout)
+  console.timeEnd(`parse porcelain`)
+
+  for (const entry of status) {
     if (entry.kind === 'entry') {
       const status = mapStatus(entry.statusCode)
 
@@ -155,6 +162,7 @@ export async function getStatus(
 
   const workingDirectory = WorkingDirectoryStatus.fromFiles([...files.values()])
 
+  console.timeEnd(`status parsing`)
   return {
     currentBranch,
     currentTip,

So these numbers below represent:

  • status git invocation - the time to spawn Git and return the output
  • parse porcelain - the time to parse the raw status output
  • status parsing - the full time to parse the output from Git (includes parse porcelain)

Benchmarking with clean working directory (atom/atom)

Base branch:

status git invocation: 36.142822265625ms
parse porcelain: 0.036865234375ms
status parsing: 0.25390625ms

This PR:

status git invocation: 42.76708984375ms
parse porcelain: 0.380859375ms
status parsing: 0.62109375ms

Benchmarking with 50 deleted files (desktop/desktop)

Base branch:

status git invocation: 76.4931640625ms
parse porcelain: 0.155029296875ms
status parsing: 0.60400390625ms

This PR:

status git invocation: 69.932861328125ms
parse porcelain: 0.336181640625ms
status parsing: 0.639892578125ms

Benchmarking with 100000 untracked files (this sample repo)

Base branch:

status git invocation: 259.572021484375ms
parse porcelain: 88005.873046875ms
status parsing: 88254.67407226562ms

This PR:

status git invocation: 240.36181640625ms
parse porcelain: 47.43603515625ms
status parsing: 263.915771484375ms

@shiftkey shiftkey added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Jul 18, 2018
@shiftkey
shiftkey requested a review from iAmWillShepherd July 18, 2018 15:56
@shiftkey
shiftkey force-pushed the shiftkey/improved-porcelain-parsing branch from e8b698e to 3b7af92 Compare July 18, 2018 16:00
@shiftkey

Copy link
Copy Markdown
Member Author

@iAmWillShepherd just a heads up, this one is another "PR into PR branch" so it won't be picked up by some of the CI providers. VSTS will catch all three platforms at least, so I think we're good for coverage.

@iAmWillShepherd iAmWillShepherd self-assigned this Jul 18, 2018

@iAmWillShepherd iAmWillShepherd left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚤

@shiftkey shiftkey mentioned this pull request Jul 18, 2018
3 tasks done
@iAmWillShepherd
iAmWillShepherd merged commit 1c4f944 into shiftkey/status-with-spawn Jul 18, 2018
@iAmWillShepherd
iAmWillShepherd deleted the shiftkey/improved-porcelain-parsing branch July 18, 2018 18:48
@shiftkey shiftkey mentioned this pull request Jul 21, 2018
@lock lock Bot locked as resolved and limited conversation to collaborators Sep 16, 2018
@desktop desktop unlocked this conversation Sep 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review Pull Requests that are ready to be reviewed by the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants