Skip to content

Refactors the stashing flow a bit to reduce unnecessary work - #7474

Merged
outofambit merged 40 commits into
developmentfrom
pick-and-pop
May 23, 2019
Merged

outofambit merged 40 commits into
developmentfrom
pick-and-pop

Conversation

@iAmWillShepherd

@iAmWillShepherd iAmWillShepherd commented May 7, 2019 •

Copy link
Copy Markdown
Contributor

Address some concerns brought up in #7464 under the heading "Only stash when there are problems with checkout" by attempting to implement the flow describe in #7464 (comment).

The following are the different flows this PR implements:

New branch is not ahead of the current branch (ideal)

  1. Perform checkout and rely on Git's default behavior

New branch is ahead of the current branch; create stash (worst case)

  1. Perform the checkout (this will result in an error)
  2. Inspect the error to ensure it's DugiteError.LocalChangesOverwritten
  3. Create temporary stash
  4. Perform checkout (should work since working directory is clean at this point)
  5. Pop the entry if the branch checkout succeeds
--- Create stash on every checkout/Deleted files exist in the index New branch is not ahead of the current branch New branch is ahead of the current branch
Checkout - 180ms 150ms (error thrown)
Stash 150ms - 150ms
Checkout 180ms - 180ms
Pop 155ms - 155ms
Total 485ms 180ms (-403%) 635ms (31%)

Note, all values have been rounded

This PR differs from the flow referenced above in one key way: we resort to creating a temporary stash when there are deleted files in the working directory. What follows is the flow when deleted files exist in the working directory.

Deleted files exist

  1. Create a stash entry
  2. Checkout new branch
  3. Immediately apply the stash entry

Same performance as the original Create stash on every checkout flow.

Since we are introducing a new flow that relies on creating a temporary stash, CheckoutBranch has been modified to return a value so that we can use it in AppStore._checkoutBranch to determine if checkout succeeded so that we can pop the temporary stash entry if necessary.

cc @shiftkey, @outofambit , @billygriffin

Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
@shiftkey shiftkey self-assigned this May 7, 2019
Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
@outofambit

Copy link
Copy Markdown
Contributor

While this approach should make switching branches without errors faster by avoiding two git stash operations, i want to note it will most likely make switching branches with errors slower.

Here's the before and after for that case.

Before:

  1. create stash
  2. checkout
  3. pop stash

After:

  1. attempt checkout — error encountered
  2. create stash
  3. checkout
  4. pop stash

This is probably the right approach, making the (probably) majority case faster and the (probably) minority case slower. (I'm saying probably here, cause I haven't spent the time to have solid analysis or data to back up this hunch.)

I think its also worth thinking about how we might communicate this to the user so they understand why bringing their changes with them might take a bit longer than the usual switch branches.

@iAmWillShepherd

iAmWillShepherd commented May 7, 2019 •

Copy link
Copy Markdown
Contributor Author

I think its also worth thinking about how we might communicate this to the user so they understand why bringing their changes with them might take a bit longer than the usual switch branches.

I definitely agree with that. I think it would be best to capture that in a separate issue if we end up going with this approach.

@outofambit

Copy link
Copy Markdown
Contributor

While this approach should make switching branches without errors faster by avoiding two git stash operations, i want to note it will most likely make switching branches with errors slower.

So, I talked through this a bit more with @billygriffin and I think its fine to ignore this concern for this PR. The cases in which an error will be encountered during checkout is definitely a subset of all "bringing changes with you to a branch" workflows. That is to say, it will be slower in those cases, but those cases are most likely quite uncommon.

@shiftkey shiftkey added the performance Relating to things affecting performance label May 7, 2019
@shiftkey shiftkey removed their assignment May 8, 2019
@iAmWillShepherd iAmWillShepherd added the ready-for-review Pull Requests that are ready to be reviewed by the maintainers label May 8, 2019
@iAmWillShepherd
iAmWillShepherd requested a review from a team May 8, 2019 16:28
@outofambit outofambit added the epic:stashing Tracking label for work related to the stashing flow label May 9, 2019
@shiftkey

shiftkey commented May 9, 2019

Copy link
Copy Markdown
Member

@iAmWillShepherd looks like we've got some conflicts now that #7471 has been merged

Comment thread app/src/lib/stores/app-store.ts Resolved
@iAmWillShepherd

Copy link
Copy Markdown
Contributor Author

🍎

return error
const { branchToCheckout } = gitContext

await dispatcher.moveChangesToBranchAndCheckout(repository, branchToCheckout)

@outofambit outofambit May 22, 2019 •

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 just noticed this await in another read through the code. i think we should not use await here so that we don't have a second _checkoutBranch executed before the first _checkoutBranch can finish. however this requires us to exit from the initial _checkoutBranch early if checkout is not successful. that feels like a more risky change to the behavior of checkout, which prior to now has never checked wether checkout was actually successful before making some state updates/refreshes.

since it doesn't seem like this is causing any obvious bugs i'm going to say that this is fine for now, but is something we should keep an eye on going forward. 👀

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A great shout @outofambit. _checkoutBranch does become re-entrant in this scenario. I think we should still await here though, not awaiting will not alter the re-entrant nature of the logic as performFailableOperation doesn't itself await the result of the error handlers. I think instead we should focus on your second idea and explore that a little bit more but I definitely agree with your assessment of risk in doing so.

I imagine that the fact that we have this disconnect in the awaitable chain will cause other problems such as the StashAndSwitchBranch progress not working properly. If you choose to move your changes to the next branch and hit Ok, the dialog progress will start spinning but if the checkout fails and the error handler takes over I would imagine that the dialog will close and the stash + checkout operation will continue in the background.

My gut feeling here is that a better approach would be to have _checkoutBranch catch this specific error inline and deal with it accordingly rather than waiting for the global error handler to pick it up. That, however, is a much bigger task that I don't think we'll have time to implement and test but definitely something worth investigating post 2.0.

A completely untested rough draft of how inline-management of this would work:

diff --git a/app/src/lib/stores/app-store.ts b/app/src/lib/stores/app-store.ts
index 05d219da5..5472d89d9 100644
--- a/app/src/lib/stores/app-store.ts
+++ b/app/src/lib/stores/app-store.ts
@@ -2918,10 +2918,48 @@ export class AppStore extends TypedBaseStore<IAppState> {
     const checkoutSucceeded =
       (await this.withAuthenticatingUser(repository, (repository, account) =>
         gitStore.performFailableOperation(
-          () =>
-            checkoutBranch(repository, account, foundBranch, progress => {
-              this.updateCheckoutProgress(repository, progress)
-            }),
+          async () => {
+            try {
+              await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+              return true
+            } catch (err) {
+              if (
+                !isLocalChangesWouldBeOverwrittenError(err) ||
+                stashToPop !== null
+              ) {
+                throw err
+              }
+
+              if (
+                !(await gitStore.performFailableOperation(() => {
+                  return createDesktopStashEntry(repository, foundBranch.name)
+                }))
+              ) {
+                return false
+              }
+
+              stashToPop = await getLastDesktopStashEntryForBranch(
+                repository,
+                foundBranch.name
+              )
+
+              return await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+            }
+          },

outofambit
outofambit previously approved these changes May 22, 2019
@outofambit
outofambit requested a review from niik May 22, 2019 22:57
@tierninho

Copy link
Copy Markdown
Contributor

Not sure if this error is associated with this PR, but I spotted it a few times. Happy to add repro steps if needed.

Screen Shot 2019-05-22 at 1 47 10 PM

49d5d54, mac

@outofambit

Copy link
Copy Markdown
Contributor

Not sure if this error is associated with this PR, but I spotted it a few times. Happy to add repro steps if needed.

@tierninho thanks for sharing this! Could you file a new issue for that? with repro steps? if you're not seeing any weird behavior along with the error, then i think i'd rather look into that after merging this.

niik
niik previously requested changes May 23, 2019

@niik niik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I have three pieces of feedback this time around but the suggested change is the only one that I think needs to be considered before merging.

return error
const { branchToCheckout } = gitContext

await dispatcher.moveChangesToBranchAndCheckout(repository, branchToCheckout)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A great shout @outofambit. _checkoutBranch does become re-entrant in this scenario. I think we should still await here though, not awaiting will not alter the re-entrant nature of the logic as performFailableOperation doesn't itself await the result of the error handlers. I think instead we should focus on your second idea and explore that a little bit more but I definitely agree with your assessment of risk in doing so.

I imagine that the fact that we have this disconnect in the awaitable chain will cause other problems such as the StashAndSwitchBranch progress not working properly. If you choose to move your changes to the next branch and hit Ok, the dialog progress will start spinning but if the checkout fails and the error handler takes over I would imagine that the dialog will close and the stash + checkout operation will continue in the background.

My gut feeling here is that a better approach would be to have _checkoutBranch catch this specific error inline and deal with it accordingly rather than waiting for the global error handler to pick it up. That, however, is a much bigger task that I don't think we'll have time to implement and test but definitely something worth investigating post 2.0.

A completely untested rough draft of how inline-management of this would work:

diff --git a/app/src/lib/stores/app-store.ts b/app/src/lib/stores/app-store.ts
index 05d219da5..5472d89d9 100644
--- a/app/src/lib/stores/app-store.ts
+++ b/app/src/lib/stores/app-store.ts
@@ -2918,10 +2918,48 @@ export class AppStore extends TypedBaseStore<IAppState> {
     const checkoutSucceeded =
       (await this.withAuthenticatingUser(repository, (repository, account) =>
         gitStore.performFailableOperation(
-          () =>
-            checkoutBranch(repository, account, foundBranch, progress => {
-              this.updateCheckoutProgress(repository, progress)
-            }),
+          async () => {
+            try {
+              await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+              return true
+            } catch (err) {
+              if (
+                !isLocalChangesWouldBeOverwrittenError(err) ||
+                stashToPop !== null
+              ) {
+                throw err
+              }
+
+              if (
+                !(await gitStore.performFailableOperation(() => {
+                  return createDesktopStashEntry(repository, foundBranch.name)
+                }))
+              ) {
+                return false
+              }
+
+              stashToPop = await getLastDesktopStashEntryForBranch(
+                repository,
+                foundBranch.name
+              )
+
+              return await checkoutBranch(
+                repository,
+                account,
+                foundBranch,
+                progress => {
+                  this.updateCheckoutProgress(repository, progress)
+                }
+              )
+            }
+          },

Comment thread app/src/lib/stores/app-store.ts Resolved
Comment thread app/src/lib/stores/app-store.ts Resolved
@shiftkey
shiftkey requested a review from outofambit May 23, 2019 16:08
@shiftkey
shiftkey dismissed niik’s stale review May 23, 2019 16:09

Out for the day, and I think this needs to get in for the today's beta

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

let's do it

@outofambit
outofambit merged commit cf5f267 into development May 23, 2019
@outofambit
outofambit deleted the pick-and-pop branch May 23, 2019 16:51
niik added a commit that referenced this pull request Nov 2, 2020
Rather than depending on the error handler ping pong game

See #7474 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

epic:stashing Tracking label for work related to the stashing flow performance Relating to things affecting performance 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.

6 participants