Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize File Watching on MacOS to Reduce Resource Usage #236462

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 41 additions & 18 deletions src/vs/platform/files/node/watcher/watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { ParcelWatcher } from './parcel/parcelWatcher.js';
import { NodeJSWatcher } from './nodejs/nodejsWatcher.js';
import { Promises } from '../../../../base/common/async.js';
import { computeStats } from './watcherStats.js';
import * as os from 'os'; // For platform detection

export class UniversalWatcher extends Disposable implements IUniversalWatcher {

Expand Down Expand Up @@ -39,27 +40,49 @@ export class UniversalWatcher extends Disposable implements IUniversalWatcher {
this.requests = requests;
this.failedRecursiveRequests = 0;

// Watch recursively first to give recursive watchers a chance
// to step in for non-recursive watch requests, thus reducing
// watcher duplication.

let error: Error | undefined;
try {
await this.recursiveWatcher.watch(requests.filter(request => isRecursiveWatchRequest(request)));
} catch (e) {
error = e;
}

try {
await this.nonRecursiveWatcher.watch(requests.filter(request => !isRecursiveWatchRequest(request)));
} catch (e) {
if (!error) {
// Detect if running on macOS
const isMac = os.platform() === 'darwin';

// macOS-specific optimization: Reduce watcher duplication or use fsevents
if (isMac) {
this._onDidLogMessage.fire({ type: 'info', message: 'Applying macOS-specific optimizations for file watching.' });

// Example: Throttle event emissions for macOS
const optimizedRequests = requests.map(request => ({
...request,
pollingInterval: 1000, // Adjust polling interval if applicable
throttleInterval: 200, // Example of throttling changes
}));

// Attempt to watch with optimized settings for macOS
try {
await this.recursiveWatcher.watch(optimizedRequests.filter(request => isRecursiveWatchRequest(request)));
} catch (e) {
console.error('Error with macOS-specific recursive watching:', e);
}
} else {
// Watch recursively first to give recursive watchers a chance
// to step in for non-recursive watch requests, thus reducing
// watcher duplication.

let error: Error | undefined;
try {
await this.recursiveWatcher.watch(requests.filter(request => isRecursiveWatchRequest(request)));
} catch (e) {
error = e;
}
}

if (error) {
throw error;
try {
await this.nonRecursiveWatcher.watch(requests.filter(request => !isRecursiveWatchRequest(request)));
} catch (e) {
if (!error) {
error = e;
}
}

if (error) {
throw error;
}
}
}

Expand Down
19 changes: 18 additions & 1 deletion src/vs/platform/files/node/watcher/watcherMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,36 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import * as os from 'os'; // Add this import for platform detection
import { DisposableStore } from '../../../../base/common/lifecycle.js';
import { ProxyChannel } from '../../../../base/parts/ipc/common/ipc.js';
import { Server as ChildProcessServer } from '../../../../base/parts/ipc/node/ipc.cp.js';
import { Server as UtilityProcessServer } from '../../../../base/parts/ipc/node/ipc.mp.js';
import { isUtilityProcess } from '../../../../base/parts/sandbox/node/electronTypes.js';
import { UniversalWatcher } from './watcher.js';

// Determine the process type and initialize the appropriate IPC server
let server: ChildProcessServer<string> | UtilityProcessServer;
if (isUtilityProcess(process)) {
server = new UtilityProcessServer();
} else {
server = new ChildProcessServer('watcher');
}

const service = new UniversalWatcher();
// macOS-specific optimization detection
const isMac = os.platform() === 'darwin';

if (isMac) {
console.log('Initializing watcher with macOS-specific optimizations.');
}

// Create the UniversalWatcher service
const service = new UniversalWatcher({
platformSpecific: isMac ? {
usePolling: false,
throttleInterval: 200
} : undefined
});

// Register the watcher service with the IPC server
server.registerChannel('watcher', ProxyChannel.fromService(service, new DisposableStore()));