User talk:Braindawg: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
(Fix lonk)
 
(13 intermediate revisions by 4 users not shown)
Line 1: Line 1:
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 {{tf2|4}}.  Your mileage may vary in other games.


{{Warning|Only optimize your scripts if you need to!  Some of these tips may introduce extra unnecessary complexity to your project.  Premature optimization without knowing where your performance issues actually come from is extremely ill-advised.}}
<small>(using [[Template:Message]])</small>&nbsp; Hello Braindawg I have moved all content from here, to [[User:Braindawg/performance|this page]] as this page is meant for discussions about users. Feel free to revert if you dislike this change.&nbsp;--[[User:Seal Enthusiast|Seal Enthusiast]] ([[User talk:Seal Enthusiast|talk]]) 18:09, 15 Apr 2024 (UTC)
 
== 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, <code>NetProps.GetPropString(...)</code> would simply become <code>GetPropString(...)</code>.  This can yield performance improvements of up to 20%{{confirm}}
 
<source lang=js>
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
</source>
 
=== Benchmark ===
{{todo|Add 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.
 
<source lang=js>
::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;
}
</source>
 
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 ===
{{todo|Add Benchmark}}
 
== String Formatting ==
 
Squirrel supports two main ways to format strings: Concatenation using the + symbol, and the <code>format()</code> function.
 
<code>format()</code> 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 <code>TOKVString()</code> VScript function takes a Vector/QAngle and formats the values into a string.  For example, <code>Vector(0, 0, 0).ToKVString()</code> would be <code>"0 0 0"</code>
 
On top of being less cumbersome to write, <code>ToKVString()</code> is marginally faster than <code>format()</code>.  Interestingly though, when formatting multiple <code>ToKVString()</code> outputs into a new string, concatenation may be faster.
 
=== Benchmark ===
 
<source lang=js>
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());
</source>
 
Result:
 
{| class="standard-table"
! Configuration
! Results
|-
|<code>concat</code>
|<code>39.0847ms</code>
|-
|<code>format</code>
|<code>24.0123ms</code>
|-
|<code>ToKVString</code>
|<code> 19.9377ms</code>
|-
|<code>ToKVString + concat</code>
|<code>18.5166ms</code>
|}
 
== 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 <code>SpawnEntityFromTable</code>, as there is no extra step with needing to parse a table of keyvalues.
 
=== Benchmark ===
 
<source lang=js>
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());
</source>
 
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 <code>parentname</code> 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.
 
{{todo|investigate these further, add benchmarks}}

Latest revision as of 07:24, 30 August 2024

(using Template:Message)  Hello Braindawg I have moved all content from here, to this page as this page is meant for discussions about users. Feel free to revert if you dislike this change. --Seal Enthusiast (talk) 18:09, 15 Apr 2024 (UTC)