Skip to content

Instantly share code, notes, and snippets.

@nosoop
Last active June 28, 2021 02:08
Show Gist options
  • Save nosoop/e19dd9c103bdc1c697a3e83603bf7725 to your computer and use it in GitHub Desktop.
Save nosoop/e19dd9c103bdc1c697a3e83603bf7725 to your computer and use it in GitHub Desktop.

fresh benchmarks for attributes and attribute strings with some new optimizations on tf2attributes' side

(note that the optimizations only affect native attributes)


Running benchmark with 65536 iterations. (This benchmark was run multiple times, and the best time for each was taken.)

  • GetGameTime took 0.000384s
  • GetEntPropString took 0.010754s

The above are just baseline values on known 'simple' operations for reference

  • TF2Attrib_HookValueFloat took 0.037156s

TF2Attrib_HookValueFloat iterates over all items on a player if given a player entity to hook

  • TF2Attrib_GetStringValue took 0.116503s
  • TF2Attrib_GetStringValue (missing) took 1.443081s

TF2Attrib_GetStringValue does naive lookups and doesn't take advantage of the attrib cache

  • TF2Attrib_HookValueString took 0.081578s
  • TF2Attrib_HookValueString (missing) took 0.079989s

TF2Attrib_HookValueString does caching and iterates over all player items

  • TF2CustAttr_GetInt took 0.044222s
  • TF2CustAttr_GetFloat took 0.048780s
  • TF2CustAttr_GetString took 0.051782s

custattr doesn't iterate over all items on a player, so you have to make multiple calls for an attribute that combines values — multiply the int / float bench times by at least 4x (player + 3 weapons) if you want to match hookvalue


tl;dr:

  • native int / float hooks are faster than iterated calls to custattr int / float (they should be the same-ish speed for single entities)
  • TF2CustAttr_GetString is ~40% faster than TF2Attrib_HookValueString
  • TF2Attrib_HookValueString is ~75% faster than TF2Attrib_GetStringValue in the optimal case where the attribute exists as a runtime attribute, and 1800% faster than TF2Attrib_GetStringValue in the worst case where the attribute doesn't exist on an entity (since it iterates over runtime / SOC / static). TF2Attrib_GetStringValue is literally dead on arrival, since it's not even on a public release branch yet!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment