script optimization - #1044
Conversation
Actually, this is certain. However, there's one thing I'm not entirely sure about — for such small collections, is hash-based lookup actually faster than linear search using contains()? Hash reading involves some computation, and the memory might not be very contiguous, so it's hard to guess which one performs better. I think it would be more appropriate to let the test results speak for themselves. |
Okay, I'll take a look and see if this change can be made. |
|
Through my testing, I found that |
Optimized script classes to reduce memory usage, GC pressure, and improve performance.
Script.hx:
Type.resolveClasscalls don't need to be rebuilt every time a script is created.stateandwindoware excluded from this cache because they can change — they remain dynamic and are still reassigned on each creation._EMPTY_ARGSvariable for use in places where an empty array is needed. Previously,var result = onCall(func, parameters == null ? [] : parameters);would create a new array whenparameterswas null, which was unnecessary. This optimization has been moved to Optimization hscript-improved#21, where the same empty array reuse is also applied.HScript.hx:
__parserPoolvariable to reuse theParserinstance. Since theParserclass is relatively large, reusing it may improve script startup speed and slightly reduce memory usage.if (!interp.variables.exists(funcName)) return null;becauseinterp.variables.get(funcName);combined with the subsequentif (func != null && Reflect.isFunction(func))effectively already serves as a null check.set()could cause incorrect caching invarLocationCache. Addedinterp.invalidateCache()to clear the cache when necessary.MultiThreadedScript.hx:
Array-basedcontains()O(n) operation withMap'sexists()O(1) lookup for the__variablesvariable.ScriptPack.hx:
e.call(func, [event])would create a new array every time — and it did this for every single script. This change significantly reduces unnecessary memory allocations.GlobalScript.hx:
call("preStateSwitch", [])to eliminate the[]empty array allocation.These changes have been tested with mods containing a large number of scripts, and no issues were observed.