source-sdk-2013 icon indicating copy to clipboard operation
source-sdk-2013 copied to clipboard

Fix inefficient lookup for item definitions by name

Open ficool2 opened this issue 10 months ago • 3 comments

When items_game.txt is parsed by the client and server, they do a LOT of item definition lookups by the name. Unfortunately, CEconItemSchema::GetItemDefinitionByName searches the list of items manually which is terribly inefficient. In fact, it's so inefficient that it added 1-2 seconds of loading time on startup! This PR resolves this by using a dictionary mapping from name to item definition, reducing the time complexity from O(N) -> O(1). This reduces loading time on startup by at least 1 second.

Profiler capture before (in milliseconds)

before

Profiler capture after (in milliseconds)

after

ficool2 avatar Feb 27 '25 21:02 ficool2

This also causes significant spawning lag in MvM. Every AddItem call takes around 10 ms while the rest of the spawning logic is roughly 1 ms on my system. Every robot has at leat g_szRomePromoItems_Hat and g_szRomePromoItems_Misc so that's 20 ms minimum.

HyperionCoding avatar Mar 05 '25 19:03 HyperionCoding

I'm unsure of the performance implications but could something similar not also be applied to attributes ?

Megawolf7339 avatar May 20 '25 13:05 Megawolf7339

I finally tested this in MvM and here are the results

Without patch:

tf_logic_mann_vs_machine(.PEAVCBaseEntity@@) thinking for 46.81 ms!!!

With patch:

tf_logic_mann_vs_machine(.PEAVCBaseEntity@@) thinking for 34.33 ms!!!

This is without cosmetics on robots so the gain will be larger the more items they have.

HyperionCoding avatar Jun 08 '25 19:06 HyperionCoding