MISC: Improve several things relating to PID lookups (#1594)

findRunningScriptByPid needlessly took a "server" argument. This caused
there to be a "getRunningScriptByPid" version that did *not*, and it was
looping through all servers in order to function, which is needlessly
inefficient.

By removing the parameter and the needless inefficient helper method,
the following changes:
- Many Netscript functions such as isRunning() and getScript() become faster.
- The terminal "tail" command now works by pid regardless of the current
  server. Note that "kill" already worked this way.

I also improved the docs around "tail", since the pid argument wasn't
in the help.
This commit is contained in:
David Walker
2024-08-17 14:03:00 -07:00
committed by GitHub
parent 34db6e8b26
commit 5b2a4eafcb
5 changed files with 10 additions and 21 deletions

View File

@@ -10,7 +10,7 @@ import type { WorkerScript } from "./WorkerScript";
import React from "react";
import { killWorkerScript } from "./killWorkerScript";
import { GetAllServers, GetServer } from "../Server/AllServers";
import { GetServer } from "../Server/AllServers";
import { Player } from "@player";
import { ScriptDeath } from "./ScriptDeath";
import { formatExp, formatMoney, formatRam, formatThreads } from "../ui/formatNumber";
@@ -649,17 +649,9 @@ export function getRunningScriptsByArgs(
return findRunningScripts(path, scriptArgs, server);
}
function getRunningScriptByPid(pid: number): RunningScript | null {
for (const server of GetAllServers()) {
const runningScript = findRunningScriptByPid(pid, server);
if (runningScript) return runningScript;
}
return null;
}
function getRunningScript(ctx: NetscriptContext, ident: ScriptIdentifier): RunningScript | null {
if (typeof ident === "number") {
return getRunningScriptByPid(ident);
return findRunningScriptByPid(ident);
} else {
const scripts = getRunningScriptsByArgs(ctx, ident.scriptname, ident.hostname, ident.args);
if (scripts === null) return null;