User talk:Braindawg: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(Fix lonk)
 
(21 intermediate revisions by 4 users not shown)
Line 1: Line 1:
== 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 in a table before executing it.
<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)
 
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}}
 
<code lang=js>
foreach(k, v in ::NetProps.getclass())
if (k != "IsValid" && !(k in ROOT))
ROOT[k] <- ::NetProps[k].bindenv(::NetProps)
</code>
 
== 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.
 
<code 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;
}
</code>
 
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.

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)