You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you do this, I think that wiki page glosses over some things that might be worth paying attention to:
If the alternative to Linq/foreach is creating an intermediate list/array, you're probably better off not changing anything. The array/list itself is garbage, and for larger collections the allocation size grows (whereas for Linq/foreach it should usually be constant).
Lists can be even worse in this regard because the backing array gets completely re-allocated each time it runs out of space (it starts at 4 by default, then when you need to add the 5th, it goes to 8, then to 16...)
The allocations in question are quite small, perhaps tens of bytes (don't recall exactly though). If you're not doing it very often (e.g. not every frame), then it might not be worth it.
The wiki article is correct that foreach will not allocate for most .net classes, but will allocate a bit for many things in KSP. Squad could fix this with a few lines of code in each affected class and I've logged an issue. My initial investigation found the problem on these classes:
PartResourceList
PartModuleList
AssemblyLoader.LoadedAssembyList
BaseFieldList
ConfigNode.ValueList
ConfigNode.ConfigNodeList
PartResourceDefinitionList
PartUpgradeHandler
PQSMeshPlanet.VertList
ShipConstruct
UrlDir.ConfigFileType
Finally, I think readability counts for something, and that for loops can often take away from readability. This is especially for loops with nested conditionals that would often replace linq.
see http://wiki.kerbalspaceprogram.com/wiki/Garbage_Reduction
The text was updated successfully, but these errors were encountered: