mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-22 01:03:01 +02:00
NETSCRIPT: Rework disableLog for efficiency (#1589)
The current implementation was naive; disableLog("ALL") was storing a
key for every function, and iterating over a different object to do it
(when iterating over objects is quite slow).
The common cases of Bitburner (and especially batching, where efficiency
matters most) are either never disabling anything, or disabling "ALL".
This optimizes for these two cases, at the expense of slightly more
complicated code to deal with the less-common edge cases.
This commit is contained in:
+33
-18
@@ -495,37 +495,46 @@ export const ns: InternalAPI<NSFull> = {
|
||||
},
|
||||
disableLog: (ctx) => (_fn) => {
|
||||
const fn = helpers.string(ctx, "fn", _fn);
|
||||
if (fn === "ALL") {
|
||||
for (const fn of Object.keys(possibleLogs)) {
|
||||
ctx.workerScript.disableLogs[fn] = true;
|
||||
}
|
||||
helpers.log(ctx, () => `Disabled logging for all functions`);
|
||||
} else if (possibleLogs[fn] === undefined) {
|
||||
if (possibleLogs[fn] === undefined) {
|
||||
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
||||
}
|
||||
if (fn === "ALL") {
|
||||
ctx.workerScript.disableLogs = allDisabled;
|
||||
// No need to log here, it's been disabled.
|
||||
} else {
|
||||
ctx.workerScript.disableLogs[fn] = true;
|
||||
helpers.log(ctx, () => `Disabled logging for ${fn}`);
|
||||
// We don't track individual log entries when all are disabled.
|
||||
if (!ctx.workerScript.disableLogs["ALL"]) {
|
||||
ctx.workerScript.disableLogs[fn] = true;
|
||||
helpers.log(ctx, () => `Disabled logging for ${fn}`);
|
||||
}
|
||||
}
|
||||
},
|
||||
enableLog: (ctx) => (_fn) => {
|
||||
const fn = helpers.string(ctx, "fn", _fn);
|
||||
if (fn === "ALL") {
|
||||
for (const fn of Object.keys(possibleLogs)) {
|
||||
delete ctx.workerScript.disableLogs[fn];
|
||||
}
|
||||
helpers.log(ctx, () => `Enabled logging for all functions`);
|
||||
} else if (possibleLogs[fn] === undefined) {
|
||||
if (possibleLogs[fn] === undefined) {
|
||||
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
||||
}
|
||||
delete ctx.workerScript.disableLogs[fn];
|
||||
helpers.log(ctx, () => `Enabled logging for ${fn}`);
|
||||
if (fn === "ALL") {
|
||||
ctx.workerScript.disableLogs = {};
|
||||
helpers.log(ctx, () => `Enabled logging for all functions`);
|
||||
} else {
|
||||
if (ctx.workerScript.disableLogs["ALL"]) {
|
||||
// As an optimization, we normally store only that key, but we have to
|
||||
// expand it out to all keys at this point.
|
||||
// Conveniently, possibleLogs serves as a model for "all keys disabled."
|
||||
ctx.workerScript.disableLogs = Object.assign({}, possibleLogs, { ALL: false, [fn]: false });
|
||||
} else {
|
||||
ctx.workerScript.disableLogs[fn] = false;
|
||||
}
|
||||
helpers.log(ctx, () => `Enabled logging for ${fn}`);
|
||||
}
|
||||
},
|
||||
isLogEnabled: (ctx) => (_fn) => {
|
||||
const fn = helpers.string(ctx, "fn", _fn);
|
||||
if (possibleLogs[fn] === undefined) {
|
||||
throw helpers.errorMessage(ctx, `Invalid argument: ${fn}.`);
|
||||
}
|
||||
return !ctx.workerScript.disableLogs[fn];
|
||||
return ctx.workerScript.shouldLog(fn);
|
||||
},
|
||||
getScriptLogs:
|
||||
(ctx) =>
|
||||
@@ -1840,7 +1849,13 @@ export function NetscriptFunctions(ws: WorkerScript): NSFull {
|
||||
return NSProxy(ws, ns, [], { args: ws.args.slice(), pid: ws.pid, enums });
|
||||
}
|
||||
|
||||
const possibleLogs = Object.fromEntries([...getFunctionNames(ns, "")].map((a) => [a, true]));
|
||||
const possibleLogs = Object.fromEntries(getFunctionNames(ns, "").map((a) => [a, true]));
|
||||
possibleLogs.ALL = true;
|
||||
|
||||
// We reuse this object for *all* scripts that disable all keys, to prevent memory growth.
|
||||
// Any script that needs a custom set of values will use a fresh object.
|
||||
const allDisabled = { ALL: true } as const;
|
||||
|
||||
/** Provides an array of all function names on a nested object */
|
||||
function getFunctionNames(obj: object, prefix: string): string[] {
|
||||
const functionNames: string[] = [];
|
||||
|
||||
Reference in New Issue
Block a user