Skip to content

fix(core): use v8 serializer in v19 #31222

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

Open
wants to merge 1 commit into
base: 19.8.x
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions packages/nx/src/command-line/daemon/daemon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Arguments } from 'yargs';
import { DAEMON_OUTPUT_LOG_FILE } from '../../daemon/tmp-dir';
import { output } from '../../utils/output';
import { generateDaemonHelpOutput } from '../../daemon/client/generate-help-output';
import { daemonClient } from '../../daemon/client/client';

export async function daemonHandler(args: Arguments) {
if (args.start) {
Expand Down
25 changes: 17 additions & 8 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import {
FLUSH_SYNC_GENERATOR_CHANGES_TO_DISK,
type HandleFlushSyncGeneratorChangesToDiskMessage,
} from '../message-types/flush-sync-generator-changes-to-disk';
import { deserialize } from 'node:v8';
import { isJsonMessage } from '../../utils/consume-messages-from-socket';

const DAEMON_ENV_SETTINGS = {
NX_PROJECT_GLOB_CACHE: 'false',
Expand Down Expand Up @@ -227,7 +229,10 @@ export class DaemonClient {
return this.sendToDaemonViaQueue({
type: 'HASH_TASKS',
runnerOptions,
env,
env:
process.env.NX_USE_V8_SERIALIZER !== 'false'
? structuredClone(process.env)
: env,
tasks,
taskGraph,
});
Expand Down Expand Up @@ -265,7 +270,9 @@ export class DaemonClient {
).listen(
(message) => {
try {
const parsedMessage = JSON.parse(message);
const parsedMessage = isJsonMessage(message)
? JSON.parse(message)
: deserialize(Buffer.from(message, 'binary'));
callback(null, parsedMessage);
} catch (e) {
callback(e, null);
Expand Down Expand Up @@ -543,21 +550,23 @@ export class DaemonClient {

private handleMessage(serializedResult: string) {
try {
performance.mark('json-parse-start');
const parsedResult = JSON.parse(serializedResult);
performance.mark('json-parse-end');
performance.mark('result-parse-start');
const parsedResult = isJsonMessage(serializedResult)
? JSON.parse(serializedResult)
: deserialize(Buffer.from(serializedResult, 'binary'));
performance.mark('result-parse-end');
performance.measure(
'deserialize daemon response',
'json-parse-start',
'json-parse-end'
'result-parse-start',
'result-parse-end'
);
if (parsedResult.error) {
this.currentReject(parsedResult.error);
} else {
performance.measure(
'total for sendMessageToDaemon()',
'sendMessageToDaemon-start',
'json-parse-end'
'result-parse-end'
);
return this.currentResolve(parsedResult);
}
Expand Down
16 changes: 12 additions & 4 deletions packages/nx/src/daemon/client/daemon-socket-messenger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { randomUUID } from 'crypto';
import { serialize } from 'v8';
import { Socket } from 'net';
import { performance } from 'perf_hooks';
import { consumeMessagesFromSocket } from '../../utils/consume-messages-from-socket';
import {
consumeMessagesFromSocket,
MESSAGE_END_SEQ,
} from '../../utils/consume-messages-from-socket';

export interface Message extends Record<string, any> {
type: string;
Expand All @@ -12,9 +15,14 @@ export class DaemonSocketMessenger {
constructor(private socket: Socket) {}

async sendMessage(messageToDaemon: Message) {
this.socket.write(JSON.stringify(messageToDaemon));
if (process.env.NX_USE_V8_SERIALIZER !== 'false') {
const serialized = serialize(messageToDaemon);
this.socket.write(serialized.toString('binary'));
} else {
this.socket.write(JSON.stringify(messageToDaemon));
}
// send EOT to indicate that the message has been fully written
this.socket.write(String.fromCodePoint(4));
this.socket.write(MESSAGE_END_SEQ);
}

listen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,18 @@ export function notifyFileWatcherSockets(
}

if (changedProjects.length > 0 || changedFiles.length > 0) {
return handleResult(socket, 'FILE-WATCH-CHANGED', () =>
Promise.resolve({
description: 'File watch changed',
response: JSON.stringify({
changedProjects,
changedFiles,
return handleResult(
socket,
'FILE-WATCH-CHANGED',
() =>
Promise.resolve({
description: 'File watch changed',
response: JSON.stringify({
changedProjects,
changedFiles,
}),
}),
})
'json'
);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HandlerResult } from './server';
export async function handleContextFileData(): Promise<HandlerResult> {
const files = await getAllFileDataInContext(workspaceRoot);
return {
response: JSON.stringify(files),
response: files,
description: 'handleContextFileData',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function handleFlushSyncGeneratorChangesToDisk(
const result = await flushSyncGeneratorChangesToDisk(generators);

return {
response: JSON.stringify(result),
response: result,
description: 'handleFlushSyncGeneratorChangesToDisk',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function handleGetFilesInDirectory(
): Promise<HandlerResult> {
const files = await getFilesInDirectoryUsingContext(workspaceRoot, dir);
return {
response: JSON.stringify(files),
response: files,
description: 'handleNxWorkspaceFiles',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export async function handleGetRegisteredSyncGenerators(): Promise<HandlerResult
const syncGenerators = await getCachedRegisteredSyncGenerators();

return {
response: JSON.stringify(syncGenerators),
response: syncGenerators,
description: 'handleGetSyncGeneratorChanges',
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export async function handleGetSyncGeneratorChanges(
);

return {
response: JSON.stringify(result),
response: result,
description: 'handleGetSyncGeneratorChanges',
};
}
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/handle-glob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function handleGlob(
): Promise<HandlerResult> {
const files = await globWithWorkspaceContext(workspaceRoot, globs, exclude);
return {
response: JSON.stringify(files),
response: files,
description: 'handleGlob',
};
}
6 changes: 4 additions & 2 deletions packages/nx/src/daemon/server/handle-hash-tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ export async function handleHashTasks(payload: {
payload.runnerOptions
);
}
const response = JSON.stringify(
await storedHasher.hashTasks(payload.tasks, payload.taskGraph, payload.env)
const response = await storedHasher.hashTasks(
payload.tasks,
payload.taskGraph,
payload.env
);
return {
response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function handleNxWorkspaceFiles(
projectRootMap
);
return {
response: JSON.stringify(files),
response: files,
description: 'handleNxWorkspaceFiles',
};
}
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/handle-outputs-tracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function handleOutputsHashesMatch(payload: {
payload.data.hash
);
return {
response: JSON.stringify(res),
response: res,
description: 'outputsHashesMatch',
};
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/daemon/server/handle-task-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export async function handleGetFlakyTasks(hashes: string[]) {
const taskHistory = getTaskHistory();
const history = await taskHistory.getFlakyTasks(hashes);
return {
response: JSON.stringify(history),
response: history,
description: 'handleGetFlakyTasks',
};
}
Expand All @@ -23,7 +23,7 @@ export async function handleGetEstimatedTaskTimings(targets: TaskTarget[]) {
const taskHistory = getTaskHistory();
const history = await taskHistory.getEstimatedTaskTimings(targets);
return {
response: JSON.stringify(history),
response: history,
description: 'handleGetEstimatedTaskTimings',
};
}
Loading