User talk:Braindawg: Difference between revisions
Line 105: | Line 105: | ||
|- | |- | ||
|<code>ToKVString + concat</code> | |<code>ToKVString + concat</code> | ||
|<code> | |<code>18.5166ms</code> | ||
|} | |} | ||
Revision as of 21:17, 14 April 2024
This page includes performance tips and tricks for VScript. Many of these tips can be used in other games, however, all of these were tested in Team Fortress 2. Your mileage may vary in other games.

Folding Functions
Folding functions in the context of VScript means folding them into the root table. It is recommended that you do this for functions that are commonly used in expensive operations, and for functions that have unique names that are not shared by other classes. Not only is this more readable and easier to write, but it also skips the extra step where the game needs to first find the function before executing it.
The following example folds all NetProp related functions into the root table. for example, NetProps.GetPropString(...)
would simply become GetPropString(...)
. This can yield performance improvements of up to 20%[confirm]
foreach(k, v in ::NetProps.getclass())
if (k != "IsValid" && !(k in ROOT))
ROOT[k] <- ::NetProps[k].bindenv(::NetProps)
local gamerules = Entities.FindByClassname(null, "tf_gamerules")
local gamerules_targetname = GetPropString(gamerules, "m_iName") // "NetProps." prefix is no longer necessary
Benchmark
Constants
Folding Constants
Similar to folding functions, folding pre-defined Constant values into the constant table (or the root table) makes referencing them less cumbersome, as well as increasing performance.
::ROOT <- getroottable();
if (!("ConstantNamingConvention" in ROOT)) // make sure folding is only done once
{
foreach (a,b in Constants)
foreach (k,v in b)
ROOT[k] <- v != null ? v : 0;
}
You may have noticed that we are folding our constants into the root table instead of the constant table. This will be explained below
Root table vs Constant table
Unlike values inserted into the root table, values inserted into the constant table are cached at the pre-processor level. What this means is, while accessing them can be faster (up to 1.5x faster to be more precise), it may not be feasible to fold your constants into the constant table
If you intend to insert values into the constant table, you must do this before any other scripts are executed, otherwise your script will not be able to read any values from it.
Benchmark
String Formatting
Squirrel supports two main ways to format strings: Concatenation using the + symbol, and the format()
function.
format()
does not support formatting entity handles and other VScript-specific datatypes, however it does support formatting strings, integers, and floats. It is also significantly faster than concatenation.
ToKVString
the TOKVString()
VScript function takes a Vector/QAngle and formats the values into a string. For example, Vector(0, 0, 0).ToKVString()
would be "0 0 0"
On top of being less cumbersome to write, ToKVString()
is marginally faster than format()
. Interestingly though, when formatting multiple ToKVString()
outputs into a new string, concatenation may be faster.
Benchmark
local mins = Vector(-1, -2, -3);
local maxs = Vector(1, 2, 3);
local keyvalues = { responsecontext = "" }
BeginBenchmark();
for (local i = 0; i < 10000; i++)
keyvalues.responsecontext <- mins.x.tostring() + "," + mins.y.tostring() + "," + mins.z.tostring() + "," + maxs.x.tostring() + "," + maxs.y.tostring() + "," + maxs.z.tostring()
printf("concat took %.4f ms\n", EndBenchmark());
BeginBenchmark();
for (local i = 0; i < 10000; i++)
keyvalues.responsecontext <- format("%g,%g,%g,%g,%g,%g", mins.x, mins.y, mins.z, maxs.x, maxs.y, maxs.z)
printf("format took %.4f ms\n", EndBenchmark());
BeginBenchmark();
for (local i = 0; i < 10000; i++)
keyvalues.responsecontext <- format("%s %s", mins.ToKVString(), maxs.ToKVString())
printf("kvstring took %.4f ms\n", EndBenchmark());
BeginBenchmark();
for (local i = 0; i < 10000; i++)
keyvalues.responsecontext <- mins.ToKVString() + " " + maxs.ToKVString()
printf("kvstring concat took %.4f ms\n", EndBenchmark());
Result:
Configuration | Results |
---|---|
concat
|
39.0847ms
|
format
|
24.0123ms
|
ToKVString
|
19.9377ms
|
ToKVString + concat
|
18.5166ms
|
Spawning Entities
in VScript, there are four common ways to spawn entities:
- CreateByClassname + DispatchSpawn functions
- SpawnEntityFromTable function
- SpawnEntityGroupFromTable function
- point_script_template entity + AddTemplate functions
CreateByClassname + DispatchSpawn vs SpawnEntityFromTable
In general, performance is not a major concern when spawning entities. In special circumstances though, you may need to spawn and kill a temporary entity in an already expensive function. A notable example of an entity that would need this is trigger_stun. This entity will not attempt to re-stun the same player multiple times, so it is not possible to spawn a single entity and repeatedly fire StartTouch/EndTouch on the same target.
In situations like this, CreateByClassname + DispatchSpawn is roughly 4x faster in comparison to SpawnEntityFromTable
, as there is no extra step with needing to parse a table of keyvalues.
Benchmark
BeginBenchmark();
trigger_stun = SpawnEntityFromTable("trigger_stun",
{
stun_type = 2,
stun_effects = true,
stun_duration = 3,
move_speed_reduction = 0.1,
trigger_delay = 0.1,
spawnflags = 1,
});
printf("table took %.4f ms\n", EndBenchmark());
BeginBenchmark();
trigger_stun = Entities.CreateByClassname("trigger_stun");
trigger_stun.KeyValueFromInt("stun_type", 2);
trigger_stun.KeyValueFromInt("stun_effects", 1);
trigger_stun.KeyValueFromFloat("stun_duration", 3.0);
trigger_stun.KeyValueFromFloat("move_speed_reduction", 0.1);
trigger_stun.KeyValueFromFloat("trigger_delay", 0.1);
trigger_stun.KeyValueFromInt("spawnflags", 1);
printf("manual took %.4f ms\n", EndBenchmark());
result: table took 0.0806 ms manual took 0.0201 ms
SpawnEntityGroupFromTable vs point_script_template
When spawning multiple entities at the same time, it is more efficient to use SpawnEntityGroupFromTable or a point_script_template entity. These options also have the added benefit of respecting parent hierarchy, so the parentname
keyvalue works as intended.
SpawnEntityGroupFromTable has several major limitations in comparison to point_script_template. The most significant limitation is it does not return any entity handles that can be accessed elsewhere in the script, and is generally not a good option in comparison to point_script_template.