Skip to content

Commit eaaa050

Browse files
committed
Make file watcher more efficient
1 parent c7ece66 commit eaaa050

9 files changed

Lines changed: 450 additions & 61 deletions

‎Applications/ycode/Tests/main.m‎

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,28 @@ - (NSString *) resolvedFilePathForNavigatorItem:
136136
- (void) updateThumbnailForItem: (YCodeProjectNavigatorItem *)item;
137137
@end
138138

139+
@interface YCodeThumbnailTestNavigator : YCodeProjectNavigatorController
140+
{
141+
NSUInteger _thumbnailRequests;
142+
}
143+
144+
- (NSUInteger) thumbnailRequests;
145+
@end
146+
147+
@implementation YCodeThumbnailTestNavigator
148+
- (void) updateThumbnailForItem: (YCodeProjectNavigatorItem *)item
149+
{
150+
_thumbnailRequests++;
151+
[super updateThumbnailForItem: item];
152+
}
153+
154+
- (NSUInteger) thumbnailRequests
155+
{
156+
return _thumbnailRequests;
157+
}
158+
159+
@end
160+
139161
@interface YCodeProjectNavigatorItem (SoundIconTesting)
140162
- (NSImage *) iconForFileExtension: (NSString *)extension;
141163
@end
@@ -5794,6 +5816,149 @@ - (void) changed: (NSNotification *)notification
57945816
[project release];
57955817
}
57965818

5819+
static void
5820+
testLargeDirectoryWatching (NSString *temporaryDirectory)
5821+
{
5822+
NSFileManager *manager = [NSFileManager defaultManager];
5823+
NSString *root = [temporaryDirectory
5824+
stringByAppendingPathComponent: @"Large Watched Folder"];
5825+
NSUInteger index;
5826+
for (index = 0; index < 160; index++)
5827+
{
5828+
NSString *folder = [root
5829+
stringByAppendingPathComponent: [NSString
5830+
stringWithFormat: @"folder-%03lu",
5831+
(unsigned long)
5832+
index]];
5833+
[manager createDirectoryAtPath: folder
5834+
withIntermediateDirectories: YES
5835+
attributes: nil
5836+
error: NULL];
5837+
}
5838+
NSString *wide = [root stringByAppendingPathComponent: @"folder-000"];
5839+
for (index = 0; index < 2000; index++)
5840+
[manager
5841+
createFileAtPath: [wide stringByAppendingPathComponent:
5842+
[NSString
5843+
stringWithFormat: @"file-%04lu.txt",
5844+
(unsigned long)index]]
5845+
contents: [NSData data]
5846+
attributes: nil];
5847+
NSString *imagePath = [root stringByAppendingPathComponent: @"Preview.png"];
5848+
NSBitmapImageRep *bitmap =
5849+
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes: NULL
5850+
pixelsWide: 1024
5851+
pixelsHigh: 768
5852+
bitsPerSample: 8
5853+
samplesPerPixel: 4
5854+
hasAlpha: YES
5855+
isPlanar: NO
5856+
colorSpaceName: NSDeviceRGBColorSpace
5857+
bytesPerRow: 0
5858+
bitsPerPixel: 0];
5859+
memset ([bitmap bitmapData], 128,
5860+
[bitmap bytesPerRow] * [bitmap pixelsHigh]);
5861+
[[bitmap representationUsingType: NSPNGFileType
5862+
properties: [NSDictionary dictionary]]
5863+
writeToFile: imagePath
5864+
atomically: YES];
5865+
[bitmap release];
5866+
YCodeProject *project = [[YCodeProject alloc] init];
5867+
testAssert (
5868+
[project readFromURL: [NSURL fileURLWithPath: root]
5869+
ofType: @"YCodeMakeProjectType"
5870+
error: NULL],
5871+
@"Open more directories than the usual Linux inotify instance limit");
5872+
#if defined(__linux__)
5873+
NSDictionary *watchers = [project valueForKey: @"directoryWatchers"];
5874+
testAssert ([watchers count] == 161 &&
5875+
[[NSSet setWithArray: [watchers allValues]] count] == 1,
5876+
@"All project directories share a single Linux watcher");
5877+
#endif
5878+
YCodeProjectNavigatorController *navigator = [project navigatorController];
5879+
NSString *untouchedPath =
5880+
[root stringByAppendingPathComponent: @"folder-000"];
5881+
PBXGroup *untouched = [[waitForDirectoryItem (project, untouchedPath, YES)
5882+
representedObject] retain];
5883+
NSArray *listing = [[untouched children] retain];
5884+
testAssert ([listing count] == 2000,
5885+
@"Wide directories retain every file in the navigator");
5886+
YCodeThumbnailTestNavigator *previews = [YCodeThumbnailTestNavigator new];
5887+
[previews setProject: project];
5888+
testAssert (
5889+
[previews thumbnailRequests] == 0,
5890+
@"Building a navigator does not load previews for undisplayed files");
5891+
YCodeProjectNavigatorItem *preview = directoryItem (
5892+
previews, [[previews rootItems] objectAtIndex: 0], imagePath);
5893+
NSTableColumn *column = [[NSTableColumn alloc] initWithIdentifier: @"name"];
5894+
NSCell *cell = [[NSCell alloc] initImageCell: nil];
5895+
[previews outlineView: nil
5896+
willDisplayCell: cell
5897+
forTableColumn: column
5898+
item: preview];
5899+
[previews outlineView: nil
5900+
willDisplayCell: cell
5901+
forTableColumn: column
5902+
item: preview];
5903+
testAssert ([previews thumbnailRequests] == 1,
5904+
@"Displaying a row loads its preview once per refresh");
5905+
NSBitmapImageRep *pixels =
5906+
[NSBitmapImageRep imageRepWithData: [[preview icon] TIFFRepresentation]];
5907+
testAssert (
5908+
[pixels pixelsWide] == 16 && [pixels pixelsHigh] == 12 &&
5909+
[[pixels colorAtX: 8 y: 8] alphaComponent] > 0,
5910+
@"Large previews retain only downsampled pixels with their original proportions");
5911+
5912+
[cell release];
5913+
[column release];
5914+
[previews release];
5915+
5916+
NSString *lastFolder = [root stringByAppendingPathComponent: @"folder-159"];
5917+
NSString *added = [lastFolder stringByAppendingPathComponent: @"added.txt"];
5918+
[@"new" writeToFile: added
5919+
atomically: YES
5920+
encoding: NSUTF8StringEncoding
5921+
error: NULL];
5922+
testAssert (waitForDirectoryItem (project, added, YES) != nil,
5923+
@"Directories beyond the old watcher limit receive updates");
5924+
testAssert (
5925+
[waitForDirectoryItem (project, untouchedPath, YES) representedObject]
5926+
== untouched
5927+
&& [untouched children] == listing,
5928+
@"A change in another folder preserves the unchanged directory listing");
5929+
YCodeProjectNavigatorItem *fileItem =
5930+
[waitForDirectoryItem (project, added, YES) retain];
5931+
NSString *second = [lastFolder stringByAppendingPathComponent: @"second.txt"];
5932+
[@"second" writeToFile: second
5933+
atomically: YES
5934+
encoding: NSUTF8StringEncoding
5935+
error: NULL];
5936+
testAssert (waitForDirectoryItem (project, second, YES) != nil
5937+
&& waitForDirectoryItem (project, added, YES) == fileItem,
5938+
@"Directory updates reuse navigator items for unchanged files");
5939+
NSString *renamed = [root stringByAppendingPathComponent: @"Renamed Folder"];
5940+
[manager moveItemAtPath: lastFolder toPath: renamed error: NULL];
5941+
testAssert (waitForDirectoryItem (
5942+
project,
5943+
[renamed stringByAppendingPathComponent: @"added.txt"],
5944+
YES) != nil
5945+
&& waitForDirectoryItem (project, lastFolder, NO) == nil,
5946+
@"Renaming a watched directory updates its subtree");
5947+
NSString *later = [renamed stringByAppendingPathComponent: @"later.txt"];
5948+
[@"later" writeToFile: later
5949+
atomically: YES
5950+
encoding: NSUTF8StringEncoding
5951+
error: NULL];
5952+
testAssert (waitForDirectoryItem (project, later, YES) != nil,
5953+
@"A renamed directory continues receiving notifications");
5954+
[fileItem release];
5955+
[listing release];
5956+
[untouched release];
5957+
[project close];
5958+
[navigator setOutlineView: nil];
5959+
[project release];
5960+
}
5961+
57975962
static void
57985963
testMakeDirectory (NSString *temporaryDirectory)
57995964
{
@@ -8270,6 +8435,7 @@ + (void) cancelPanel: (NSTimer *)timer
82708435
{
82718436
testExternalFileChanges (temporaryDirectory);
82728437
testDirectoryWatching (temporaryDirectory);
8438+
testLargeDirectoryWatching (temporaryDirectory);
82738439
[manager removeItemAtPath: temporaryDirectory error: NULL];
82748440
NSLog (@"Ycode external file checks: %lu failures",
82758441
(unsigned long)failures);
@@ -8343,6 +8509,7 @@ + (void) cancelPanel: (NSTimer *)timer
83438509
{
83448510
testFileModificationTracking (temporaryDirectory);
83458511
testDirectoryWatching (temporaryDirectory);
8512+
testLargeDirectoryWatching (temporaryDirectory);
83468513
[manager removeItemAtPath: temporaryDirectory error: NULL];
83478514
NSLog (@"Ycode file modification checks: %lu failures",
83488515
(unsigned long)failures);
@@ -8560,6 +8727,7 @@ + (void) cancelPanel: (NSTimer *)timer
85608727
testFindReplace (temporaryDirectory);
85618728
testFileModificationTracking (temporaryDirectory);
85628729
testDirectoryWatching (temporaryDirectory);
8730+
testLargeDirectoryWatching (temporaryDirectory);
85638731
testTranscriptCopy ();
85648732
testDebuggerRecords ();
85658733
testAssert ([manager createDirectoryAtPath: temporaryDirectory
@@ -8807,6 +8975,9 @@ + (void) cancelPanel: (NSTimer *)timer
88078975
@"Readable image gets a miniature navigator thumbnail");
88088976
NSBitmapImageRep *thumbnailPixels = [NSBitmapImageRep
88098977
imageRepWithData: [[previewItem icon] TIFFRepresentation]];
8978+
testAssert (
8979+
[thumbnailPixels pixelsWide] <= 16 && [thumbnailPixels pixelsHigh] <= 16,
8980+
@"Navigator stores downsampled pixels instead of the original image");
88108981
testAssert ([[thumbnailPixels colorAtX: 8 y: 8] alphaComponent] > 0,
88118982
@"Thumbnail retains decoded image pixels");
88128983
navigatorCell = [[[navigator outlineView] outlineTableColumn] dataCell];

‎Applications/ycode/YCodeFileWatcher.h‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@
2424

2525
#import <Foundation/Foundation.h>
2626

27-
/* One blocking OS event source per directory. Notifications arrive on the main
28-
thread; stopping does not wait for that thread or retain an editor. */
27+
/* Notifications arrive on the main thread. Linux directory projects share one
28+
event source and thread across their directories. */
2929
@interface YCodeFileWatcher : NSObject
3030
{
3131
NSString *_directory;
3232
NSArray *_paths;
3333
NSLock *_lock;
34+
NSMutableDictionary *_watchedDirectories;
35+
NSMutableDictionary *_directoryDescriptors;
36+
NSMutableSet *_changedDirectories;
3437
BOOL _pending;
3538
BOOL _stopped;
3639
int _stopPipe[2];
@@ -42,5 +45,9 @@
4245
/* A nil paths argument watches directory membership instead of selected files. */
4346
- (id) initWithDirectory: (NSString *)directory paths: (NSArray *)paths;
4447
- (NSArray *) paths;
48+
- (NSString *) directory;
49+
/* Linux: attach/detach directories to this event source. */
50+
- (BOOL) addDirectory: (NSString *)directory;
51+
- (void) removeDirectory: (NSString *)directory;
4552
- (void) stop;
4653
@end

0 commit comments

Comments
 (0)