Skip to content

Commit e1c52b0

Browse files
committed
Stop the worktree lookup stalling the history while it draws (issue gitx#647)
Which branches other worktrees hold was read by the first label drawn after each refresh, so wherever git is slow to start the window waited on it mid-draw. * Read it on a utility queue when the refs reload, and swap the finished snapshot in on the main thread, behind a generation guard so an overlapping reload cannot land out of order. * Keep the previous snapshot readable until the new one arrives, so the labels do not fall back to their plain colours after every refresh. * Announce only a snapshot that differs. The refs observers rearrange the history and reload the sidebar, so announcing every reload would cost two of those in place of the 9ms it saves. * Answer the drawing paths from the snapshot alone, and never with nil, since -refNamesHeldByOtherWorktrees feeds an array literal.
1 parent 01bb16f commit e1c52b0

2 files changed

Lines changed: 109 additions & 10 deletions

File tree

‎Classes/git/PBGitRepository.m‎

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ @interface PBGitRepository () {
4242

4343
@property (nonatomic, strong) NSNumber *hasSVNRepoConfig;
4444
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *worktreePathsByRefName;
45+
@property (nonatomic, assign) NSUInteger worktreeLookupGeneration;
46+
47+
- (void)reloadWorktreePaths;
48+
- (void)takeWorktreePaths:(NSDictionary<NSString *, NSString *> *)paths;
4549

4650
@end
4751

@@ -223,7 +227,7 @@ - (void)reloadRefs
223227
// clear out ref caches
224228
_headRef = nil;
225229
_headOID = nil;
226-
self.worktreePathsByRefName = nil;
230+
[self reloadWorktreePaths];
227231
self->refs = [NSMutableDictionary dictionary];
228232

229233
NSError *error = nil;
@@ -305,21 +309,54 @@ + (BOOL)worktreeAtPath:(NSString *)path isTheOneAt:(NSString *)ourPath
305309
return paths;
306310
}
307311

312+
// Read by -drawLabelAtIndex: and by the sidebar's cell, so it answers from the
313+
// snapshot alone. Never nil: -refNamesHeldByOtherWorktrees feeds an array
314+
// literal.
308315
- (NSDictionary<NSString *, NSString *> *)worktreePathsByRefName
309316
{
310-
if (_worktreePathsByRefName)
311-
return _worktreePathsByRefName;
317+
return _worktreePathsByRefName ?: @{};
318+
}
319+
320+
- (void)reloadWorktreePaths
321+
{
322+
if (![PBGitBinary path].length) {
323+
[self takeWorktreePaths:@{}];
324+
return;
325+
}
326+
327+
NSUInteger generation = ++self.worktreeLookupGeneration;
328+
NSString *ourPath = self.workingDirectory;
329+
__weak typeof(self) weakSelf = self;
330+
331+
dispatch_async(dispatch_get_global_queue(QOS_CLASS_UTILITY, 0), ^{
332+
PBGitRepository *repository = weakSelf;
333+
if (!repository)
334+
return;
312335

313-
if (![PBGitBinary path].length)
314-
return @{};
336+
NSString *output = [repository outputOfTaskWithArguments:@[ @"worktree", @"list", @"--porcelain" ] error:NULL];
337+
NSDictionary *paths = output ? [PBGitRepository worktreePathsFromPorcelain:output excludingWorktreeAtPath:ourPath] : @{};
315338

316-
NSString *output = [self outputOfTaskWithArguments:@[ @"worktree", @"list", @"--porcelain" ] error:NULL];
317-
if (!output)
318-
return @{};
339+
dispatch_async(dispatch_get_main_queue(), ^{
340+
PBGitRepository *mainRepository = weakSelf;
341+
if (!mainRepository || generation != mainRepository.worktreeLookupGeneration)
342+
return;
319343

320-
_worktreePathsByRefName = [PBGitRepository worktreePathsFromPorcelain:output excludingWorktreeAtPath:self.workingDirectory];
344+
[mainRepository takeWorktreePaths:paths];
345+
});
346+
});
347+
}
321348

322-
return _worktreePathsByRefName;
349+
// The refs observers rearrange the history and reload the sidebar, so a reload
350+
// that found the same worktrees as last time says nothing.
351+
- (void)takeWorktreePaths:(NSDictionary<NSString *, NSString *> *)paths
352+
{
353+
if ([self.worktreePathsByRefName isEqualToDictionary:paths])
354+
return;
355+
356+
self.worktreePathsByRefName = paths;
357+
358+
[self willChangeValueForKey:@"refs"];
359+
[self didChangeValueForKey:@"refs"];
323360
}
324361

325362
- (NSString *)pathOfWorktreeHoldingRef:(PBGitRef *)ref

‎GitXTests/PBGitRepositoryWorktreeTests.m‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// test names both rather than launching git for them.
1212
@interface PBGitRepository (WorktreeTesting)
1313
+ (NSDictionary<NSString *, NSString *> *)worktreePathsFromPorcelain:(NSString *)output excludingWorktreeAtPath:(NSString *)ourPath;
14+
- (void)takeWorktreePaths:(NSDictionary<NSString *, NSString *> *)paths;
1415
@end
1516

1617
// What `git worktree list --porcelain` prints for a repository whose own
@@ -34,6 +35,21 @@ @interface PBGitRepository (WorktreeTesting)
3435
@"branch refs/heads/locked_work\n"
3536
@"locked\n";
3637

38+
// Counts what the history and sidebar controllers would act on: both observe
39+
// "refs", and answer by rearranging the history or reloading the sidebar.
40+
@interface PBRefsChangeCounter : NSObject
41+
@property (nonatomic, assign) NSUInteger count;
42+
@end
43+
44+
@implementation PBRefsChangeCounter
45+
46+
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
47+
{
48+
self.count++;
49+
}
50+
51+
@end
52+
3753
@interface PBGitRepositoryWorktreeTests : XCTestCase
3854
@property (nonatomic, strong) PBGitRepository *repository;
3955
@end
@@ -164,4 +180,50 @@ - (void)testRemovingABranchHeldElsewhereIsRefusedBeforeGitRuns
164180
XCTAssertTrue([error.localizedFailureReason containsString:@"/repos/gitx-feature"], @"%@", error.localizedFailureReason);
165181
}
166182

183+
184+
#pragma mark The snapshot is read while drawing and refreshed away from it
185+
186+
// -drawLabelAtIndex: and the sidebar's cell read this while drawing, and
187+
// -refNamesHeldByOtherWorktrees feeds an array literal, where a nil would
188+
// raise rather than draw nothing.
189+
- (void)testARepositoryThatHasReadNoWorktreesYetStillAnswers
190+
{
191+
PBGitRepository *repository = [[PBGitRepository alloc] init];
192+
193+
XCTAssertNotNil([repository valueForKey:@"worktreePathsByRefName"]);
194+
XCTAssertNotNil([repository refNamesHeldByOtherWorktrees]);
195+
XCTAssertFalse([repository isRefHeldByAnotherWorktree:[PBGitRef refFromString:@"refs/heads/feature"]]);
196+
}
197+
198+
- (NSUInteger)refsChangesTaking:(NSDictionary *)paths after:(NSDictionary *)previous
199+
{
200+
[self.repository takeWorktreePaths:previous];
201+
202+
PBRefsChangeCounter *counter = [[PBRefsChangeCounter alloc] init];
203+
[self.repository addObserver:counter forKeyPath:@"refs" options:0 context:NULL];
204+
[self.repository takeWorktreePaths:paths];
205+
[self.repository removeObserver:counter forKeyPath:@"refs"];
206+
207+
return counter.count;
208+
}
209+
210+
// The refs observers rearrange the history and reload the sidebar, so a reload
211+
// that found the same worktrees as last time has to stay quiet: otherwise
212+
// moving the lookup off the drawing path would cost two reloads per refresh
213+
// in place of the 9ms it saves.
214+
- (void)testFindingTheSameWorktreesAgainAnnouncesNothing
215+
{
216+
NSDictionary *paths = @{@"refs/heads/feature" : @"/repos/gitx-feature"};
217+
218+
XCTAssertEqual([self refsChangesTaking:[paths copy] after:paths], 0u,
219+
@"an unchanged snapshot must not trigger a reload");
220+
}
221+
222+
- (void)testFindingDifferentWorktreesAnnouncesTheChange
223+
{
224+
XCTAssertEqual([self refsChangesTaking:@{@"refs/heads/other" : @"/repos/gitx-other"}
225+
after:@{@"refs/heads/feature" : @"/repos/gitx-feature"}],
226+
1u, @"a snapshot that changed has to reach the labels");
227+
}
228+
167229
@end

0 commit comments

Comments
 (0)