Hard Light Productions Forums
Modding, Mission Design, and Coding => FS2 Open Coding - The Source Code Project (SCP) => Topic started by: Echelon9 on October 17, 2011, 08:21:15 am
-
I've recently pruned from the code base a number of similar code patterns, best exemplified by:
--- trunk/fs2_open/code/graphics/gropenglshader.cpp 2011/10/17 13:01:48 7914
+++ trunk/fs2_open/code/graphics/gropenglshader.cpp 2011/10/17 13:07:24 7915
@@ -504,8 +504,6 @@
bool main_vert = cf_exists_full("main-v.sdr", CF_TYPE_EFFECTS) != 0;
bool main_frag = cf_exists_full("main-f.sdr", CF_TYPE_EFFECTS) != 0;
- GL_shader.reserve(Num_shader_files+1);
-
for (idx = 0; idx < Num_shader_files; idx++) {
bool in_error = false;
opengl_shader_t new_shader;
Broadly, STL types handle their own in-memory locations and reallocations (http://www.cplusplus.com/reference/stl/vector/). While STL types do require a copy/realloc operation when they fully utilise their capacity() this is an operation best left to the underlying implementation of the STL on your system.
Why?
- If we do manually set how much to increment the reserve() operation, we may cause unnecessary memory paging. See 'Effective STL' by Myers, Item 14, pg 50 on how systems allocate blocks of memory from different size pools for efficiency and do adjustments to STL types (http://www.uml.org.cn/c%2B%2B%2Fpdf%2FEffectiveSTL.pdf), and then think if we've actually tuned our implementations to make the increments match these sizes.... Probably not.
- General inefficiency. While most cases were well intentioned -- see further (1) -- examples which increment by +1 are poor reflections of former array code that was switched to STL types. While it's good to see more and more the engine utilise these variable memory containers, the old array access paradigms don't always work.
- It's fine where we've done some profiling and up front do one reserve() to allocate memory and preserve cache coherency (http://www.google.com.au/url?sa=t&source=web&cd=3&ved=0CCsQFjAC&url=http%3A%2F%2Fwww.tantalon.com%2Fpete%2Ffiles%2Fgdc04_common_cpp_mistakes_in_games.ppt&rct=j&q=don%27t%20use%20reserve%20capacity%20on%20STL%20types%20pete%20isensee&ei=rSqcTr_ROYSdiAKU3omoDQ&usg=AFQjCNGuRNkeMlr_JX0nBKAKQj1QwH95Mw&cad=rja). But not when a fixed, arbitrary increment is used multiple times. There were quite (http://svn.icculus.org/fs2open?view=rev&revision=7915) a few (http://svn.icculus.org/fs2open?view=rev&revision=7914) examples (http://svn.icculus.org/fs2open?view=rev&revision=7912) of these "magic numbers" in the code.
Next post I'll go into methods to be used to profile the impact of small code changes.