NETSCRIPT: Greatly speed up script launching, and remove the limitation unique args per script (#440)

* Remove the limitation unique args per script
* Internal changes to how runningScripts are stored on the server, to make common usage faster.
This commit is contained in:
David Walker
2023-04-27 15:21:06 -07:00
committed by GitHub
parent f81297dcd6
commit aa7facd4ba
44 changed files with 573 additions and 493 deletions
+1 -1
View File
@@ -88,7 +88,7 @@ export function Generic_fromJSON<T extends Record<string, any>>(
if (keys) {
for (const key of keys) {
const val = data[key];
if (val) obj[key] = val;
if (val !== undefined) obj[key] = val;
}
return obj;
}
+1 -3
View File
@@ -3,7 +3,5 @@
* @param decimal A decimal value to trim to two places.
*/
export function roundToTwo(decimal: number): number {
const leftShift: number = Math.round(parseFloat(`${decimal}e+2`));
return +`${leftShift}e-2`;
return Math.round(decimal * 100) / 100;
}
+29
View File
@@ -0,0 +1,29 @@
import type { ScriptArg } from "../../Netscript/ScriptArg";
import type { ScriptFilePath } from "../../Paths/ScriptFilePath";
// This needs to be high in the dependency graph, with few/no dependencies of
// its own, since many key modules depend on it.
export type ScriptKey = string /*& { __type: "ScriptKey" }*/;
// The key used to lookup worker scripts in their map.
export function scriptKey(path: ScriptFilePath, args: ScriptArg[]): ScriptKey {
// Asterisk is used as a delimiter because it' not a valid character in paths.
return (path + "*" + JSON.stringify(args)) as ScriptKey;
}
// Returns a RegExp that can be used to find scripts with a path that fully
// matches "pattern" in the scriptKey.
export function matchScriptPathExact(pattern: string) {
// Must fully match pattern, starting at the beginning and ending with the
// asterisk delimiter, which can't appear in script paths.
return new RegExp("^" + pattern + "\\*");
}
// Returns a RegExp that can be used to find scripts with a path that
// matches "pattern" somewhere in the scriptKey.
export function matchScriptPathUnanchored(pattern: string) {
// Don't let the match extend into the arguments part (script paths can't
// include "[").
return matchScriptPathExact("[^[]*" + pattern + "[^[]*");
}
+1 -1
View File
@@ -237,7 +237,7 @@ export const v2APIBreak = () => {
openV2Modal();
for (const server of GetAllServers()) {
server.runningScripts = [];
server.runningScriptMap = new Map();
}
saveObject.exportGame();
};