Skip to content

Use a spawn API for getting status details - #5053

Merged
shiftkey merged 26 commits into
masterfrom
shiftkey/status-with-spawn
Jul 20, 2018
Merged

shiftkey merged 26 commits into
masterfrom
shiftkey/status-with-spawn

Conversation

@shiftkey

@shiftkey shiftkey commented Jun 27, 2018 •

Copy link
Copy Markdown
Member

Tackling the known bottleneck identified in #2449

Fixes #2449

TODO:

@iAmWillShepherd iAmWillShepherd changed the title [WIP] use a spawn API for getting status details Use a spawn API for getting status details Jul 13, 2018
@iAmWillShepherd

iAmWillShepherd commented Jul 13, 2018 •

Copy link
Copy Markdown
Contributor

Problem

The getStatus function performs poorly when there is a large number of new and untracked files.

Reproduction steps

  1. mkdir test && cd test
  2. git init
  3. for i in {1..99999}; do touch file-$i; done
  4. Open repo in Desktop 😕

Investigation

Raw Git (Baseline)

╭─williamshepherd@Hera ~/Desktop/test  ‹master*›
╰─$ time git status --untracked-files=all --branch --porcelain=2 -z
...
0.13s user 0.13s system 30% cpu 0.845 total

Dugite

I wrote a small program to test Dugite in isolation to make sure we aren't loosing to much performance at that stage.

import { performance } from 'perf_hooks'
import * as proc from 'process'
import { GitProcess } from 'dugite'

const path = proc.argv[2]
const args = [
  'status',
  '--untracked-files=all',
  '--branch',
  '--porcelain=2',
  '-z'
]
const startTime = performance.now()
const process = GitProcess.spawn(args, path)
const stdoutBuf: Array<Buffer> = []

process.on('close', (code, signal) => {
  const diff = performance.now() - startTime
  console.log(`Completed in: ${diff}`)
})

process.stdout.on('data', (chunk: Buffer) => {
  stdoutBuf.push(chunk)
})

process.stdout.on('error', (err) => {
  console.error(err)
})

The run time using Dugite takes twice as long, but still completes in a very short amount of time.

╭─williamshepherd@Hera ~/Desktop/nodeproc
╰─$ time node build/main.js /Users/williamshepherd/Desktop/test
Completed in: 266.1664929986
node build/main.js /Users/williamshepherd/Desktop/test  0.25s user 0.14s system 101% cpu 0.384 total

Desktop

Opening the repo in Desktop managed to corrupt the entire application. All I attempted to profile the application which also failed spectacularly!

Note that Desktop isn't recognized as a repo on GitHub
desktop

Here is DevTools in the current app state
devtools

Checked to make sure the remote was set correctly

╭─williamshepherd@Hera ~/Desktop/desktop  ‹master› 
╰─$ cat .git/config 
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
	ignorecase = true
	precomposeunicode = true
[submodule]
	active = .
[remote "origin"]
	url = https://github.com/desktop/desktop.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
	remote = origin
	merge = refs/heads/master
[submodule "app/static/common/choosealicense.com"]
	url = https://github.com/github/choosealicense.com.git
[submodule "app/static/common/gitignore"]
	url = https://github.com/github/gitignore.git
[submodule "gemoji"]
	url = https://github.com/github/gemoji.git

Update 1

  • I am no longer using performance.now(), it has been replaced with Date.now()
  • I decided to test with fewer files (50k) and was able to get Desktop back into a usable state taking 297ms to get the status.

@shiftkey

shiftkey commented Jul 18, 2018 •

Copy link
Copy Markdown
Member Author

@iAmWillShepherd digging into this a bit more, I added some localized timings to the actions that make up getStatus:

diff --git a/app/src/lib/git/status.ts b/app/src/lib/git/status.ts
index 7390d855e..98079820b 100644
--- a/app/src/lib/git/status.ts
+++ b/app/src/lib/git/status.ts
@@ -64,11 +64,14 @@ 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`)
+  console.log(`output length: ${result.output.length}`)
 
   if (result.output.length > MaxStatusBufferSize) {
     log.error(
@@ -81,6 +84,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>()
@@ -155,6 +159,7 @@ export async function getStatus(
 
   const workingDirectory = WorkingDirectoryStatus.fromFiles([...files.values()])
 
+  console.timeEnd(`status parsing`)
   return {
     currentBranch,
     currentTip,

This showed how the overall timings were split up when running this on your 100000 untracked files example:

Old behaviour:

status git invocation: 393.72705078125ms
status parsing: 67778.72485351562ms

New behaviour:

status git invocation: 228.77978515625ms
output length: 1288925
status parsing: 85645.01196289062ms

I think that's the core of the performance problem you're seeing - now that Git itself can send the 10MB+ of output it was previously erroring on, we're uncovered a new bottleneck when parsing this much output.

@shiftkey

shiftkey commented Jul 18, 2018 •

Copy link
Copy Markdown
Member Author

By my count we're spending most of the getStatus work inside parsePorcelainStatus:

status git invocation: 323.88720703125ms
output length: 1288925
parse porcelain: 61767.81005859375ms
status parsing: 62007.541259765625ms

At quick glance, Array.shift might not be the most optimal approach here, as lots of articles talk about the work to reindex the array - which would add overhead when we're working with 100000 entries or more. I did a quick spike of using double-ended-queue and the results were, well, amazing:

status git invocation: 139.11474609375ms
output length: 1288925
parse porcelain: 78.654052734375ms
status parsing: 331.564208984375ms

I'll put together a PR to target this branch and we can see how that affects things.

@j-f1

j-f1 commented Jul 18, 2018

Copy link
Copy Markdown
Contributor

Out of curiosity, what would happen if you pushed the elements onto the array, then reversed it?

@shiftkey

Copy link
Copy Markdown
Member Author

@j-f1 is this related to #5186 rather than this PR?

…sing

Gonna go ahead and merge since this isn't being merged into master.
@j-f1

j-f1 commented Jul 18, 2018

Copy link
Copy Markdown
Contributor

Yes it is! Sorry about that.

Comment thread app/src/lib/stores/git-store.ts Outdated
)

if (status == null) {
return

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


const fields = output.split('\0')
const tokens = output.split('\0')
const queue = new Deque(tokens)

This comment was marked as spam.

@shiftkey shiftkey added this to the 1.3.0 milestone Jul 19, 2018
@shiftkey

Copy link
Copy Markdown
Member Author

Flagging this one as in-scope for 1.3.0 as the status improvements will help out in many parts of the app.

@iAmWillShepherd iAmWillShepherd added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label Jul 20, 2018

@shiftkey shiftkey left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Close, just one more message to address

if (status === null) {
this.props.dispatcher.postError(
new Error(
`createRepository: unable to get status for repository at ${fullPath}`

This comment was marked as spam.

@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.

I'm approving sine @shiftkey can't approve his own PR and it's just us two reviewing PRs today.

@shiftkey

Copy link
Copy Markdown
Member Author

Yep, we're limited on reviewers and resources right now - and we believe these changes are of benefit to everyone - so we're going to ensure this gets the maximum time on beta before the next release. #jolo

@shiftkey
shiftkey merged commit ff2c945 into master Jul 20, 2018
@shiftkey
shiftkey deleted the shiftkey/status-with-spawn branch July 20, 2018 21:17
@shiftkey shiftkey mentioned this pull request Jul 21, 2018
@lock lock Bot locked as resolved and limited conversation to collaborators Sep 18, 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.

3 participants