Zero Hour rebuilt from EA's source: x64, Direct3D 11, VS2022/CMake, ~60 original bugs fixed, an AI that builds a base. Bring your own game.
C++ cmakecnccommand-and-conquercppdirectx11game-enginegame-preservationgeneralsrtssage-enginex64zero-hour
perf(gameengine): cut html templates once, fill only values per frame
Olcay Seygan58d5e713a5c1
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 05f946917..96c0bfb1d 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -26,6 +26,7 @@ found and fixed — EA's own, not port damage.**- A screen full of fire and smoke is cheaper to draw. Thirty Inferno Cannons shelling twenty Crusaders used to send the graphics card 459 separate batches of see-through effects a frame, mostly the same texture again and again, and the game looked up the same seven card settings from scratch for every one of them. Matching effects go out together now, and settings that have not changed are not looked up again: 177 batches, and the same battle went from 11.6ms a frame to 8.6ms. It is at 6.7ms now, since the game stopped preparing everything it draws a second time for a copy of the picture nobody sees. Nothing on screen changes, the effects are drawn in exactly the order they were.- The same battle with the particle limit turned up costs 7.6ms a frame where it cost 9.1ms, and the old renderer went from 9.3ms to 6.3ms. Both were keeping a count of every kind of draw the game makes, and both did it by writing out and reading back a description of each one, on every draw, for a report no player ever opens. They glance at one draw in sixteen now. The new renderer also stopped re-sending the graphics card settings it had just sent.- The new renderer stopped redoing work between two pieces of ground that look the same. The map's terrain goes out as hundreds of tiles a frame under the same camera and the same settings, and for every tile the game worked the camera's arithmetic out again, rebuilt its description of the surface and of each texture's filtering, and compared the lot with the tile before. It now notices nothing was changed and goes straight to drawing. The fog of war is sent to the card only where it moved since the last frame, instead of all of it every frame. Two brutal computer players on Twilight Flame at 1920x1080 went from 2.46ms a frame to 2.24ms, and every picture is the one it was.+- The command bar and the observer's panel stopped reading their own pages from scratch every frame. The bar's page alone is 38 KB, and the game copied it, lowercased it and hunted through it for the blanks four times a frame before it wrote in a single number. Each page is read once now, and a frame only fills in the numbers. The same two computer players on Twilight Flame went from 2.03ms a frame to 1.84ms watched as an observer, and from 2.75ms to 2.50ms from a player's seat. Every page still comes out letter for letter as it did: two matches filled 270,000 of them both ways and found no difference.- Raise the particle limit as far as you like and every puff gets drawn. Past about sixteen thousand smoke and fire sprites on screen the game quietly stopped drawing the rest: a test with 68,000 of them showed barely a fifth. A test with 101,000 now draws all of them at 8.3ms a frame, and in 1,200 frames not one fell below 60 a second. The first version that drew them all took 22.4ms; the rest came from sorting and copying them far less and from sharing the work across every core of the processor.- Big fights no longer drag the picture down into the twenties. Every moving tank and soldier, and every puff of smoke, goes to the graphics card through one shared scratch area, and it was so small that a big fight on Bombardment Beach filled it about fifty times a frame. Each refill made the processor wait for the card, 8 to 12ms of every frame, while the card sat two thirds idle. The area is made full size once now and refills about seven times a frame, in 0.02ms. The same 16,000-frame battle, fast-forwarded, finishes in 226 seconds where it took between 263 and 325, and the seconds that fell under 50 frames a second went from 125 or more to 4.- Every animation runs on a clock instead of counting frames.diff --git a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cppindex 9481b9903..a5e9e5261 100644--- a/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp+++ b/GeneralsMD/Code/GameEngine/Source/GameClient/HtmlTemplate.cpp@@ -69,28 +69,41 @@ std::string trimmed( const std::string &text )return text.substr( first, last - first );}-std::string valueOf( const std::string &name, const HtmlValues *entry, const HtmlValues &values, const HtmlLookup &lookup )+void appendEscaped( std::string &out, const std::string &text ){- if( entry )+ for( size_t index = 0; index < text.size(); index++ ){- HtmlValues::const_iterator found = entry->find( name );- if( found != entry->end() )- return found->second;+ switch( text[ index ] )+ {+ case '&': out += "&"; break;+ case '<': out += "<"; break;+ case '>': out += ">"; break;+ case '"': out += """; break;+ case '\'': out += "'"; break;+ default: out += text[ index ]; break;+ }}+}- HtmlValues::const_iterator found = values.find( name );- if( found != values.end() )- return found->second;+/** A stretch of page: literal text, or the trimmed name inside one {{name}}. */+struct Piece+{+ std::string text;+ Bool name;+};+typedef std::vector< Piece > Pieces;- std::string looked;- if( lookup && lookup( name, looked ) )- return looked;- return std::string();-}+/** Page text written once, or a data-each element written once per entry of `list`. */+struct Block+{+ Pieces pieces;+ std::string list;+ Bool each;+};+typedef std::vector< Block > Blocks;-std::string filled( const std::string &text, const HtmlValues *entry, const HtmlValues &values, const HtmlLookup &lookup )+void split( const std::string &text, Pieces &pieces ){- std::string out;size_t at = 0;for( ;; ){@@ -98,17 +111,52 @@ std::string filled( const std::string &text, const HtmlValues *entry, const Htmlconst size_t close = open == std::string::npos ? std::string::npos : text.find( CLOSE_BRACES, open + OPEN_BRACES.size() );if( close == std::string::npos ){- out.append( text, at, std::string::npos );- return out;+ if( at < text.size() )+ pieces.push_back( Piece{ text.substr( at ), FALSE } );+ return;}- out.append( text, at, open - at );- const std::string name = trimmed( text.substr( open + OPEN_BRACES.size(), close - open - OPEN_BRACES.size() ) );- out += HtmlTemplate_escape( valueOf( name, entry, values, lookup ) );+ if( open > at )+ pieces.push_back( Piece{ text.substr( at, open - at ), FALSE } );+ pieces.push_back( Piece{ trimmed( text.substr( open + OPEN_BRACES.size(), close - open - OPEN_BRACES.size() ) ), TRUE } );at = close + CLOSE_BRACES.size();}}+void fill( std::string &out, const Pieces &pieces, const HtmlValues *entry, const HtmlValues &values,+ const HtmlLookup &lookup, std::string &looked )+{+ for( Pieces::const_iterator piece = pieces.begin(); piece != pieces.end(); ++piece )+ {+ if( !piece->name )+ {+ out += piece->text;+ continue;+ }++ if( entry )+ {+ HtmlValues::const_iterator found = entry->find( piece->text );+ if( found != entry->end() )+ {+ appendEscaped( out, found->second );+ continue;+ }+ }++ HtmlValues::const_iterator found = values.find( piece->text );+ if( found != values.end() )+ {+ appendEscaped( out, found->second );+ continue;+ }++ looked.clear();+ if( lookup && lookup( piece->text, looked ) )+ appendEscaped( out, looked );+ }+}+/** Just past the end tag of the element whose start tag is at `open`, counting elements of the same* name nested inside it.* ponytail: an element with no end tag (<img data-each>) runs to the end of the page; wrap it in a@@ -134,34 +182,12 @@ size_t elementEnd( const std::string &lower, size_t open, const std::string &tagreturn lower.size();}-} // namespace--//--------------------------------------------------------------------------------------------------std::string HtmlTemplate_escape( const std::string &text )-{- std::string escaped;- for( size_t index = 0; index < text.size(); index++ )- {- switch( text[ index ] )- {- case '&': escaped += "&"; break;- case '<': escaped += "<"; break;- case '>': escaped += ">"; break;- case '"': escaped += """; break;- case '\'': escaped += "'"; break;- default: escaped += text[ index ]; break;- }- }- return escaped;-}--//--------------------------------------------------------------------------------------------------std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &values,- const HtmlLists &lists, const HtmlLookup &lookup )+/** The page cut into blocks once: everything but filling in the values, which is all that changes+ * from one frame to the next. */+void compile( const std::string &written, Blocks &blocks ){const std::string page = withoutComments( written );const std::string lower = lowered( page );- std::string out;size_t at = 0;for( ;; ){@@ -169,8 +195,9 @@ std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &vconst size_t open = attribute == std::string::npos ? std::string::npos : lower.rfind( '<', attribute );if( open == std::string::npos || open < at ){- out += filled( page.substr( at ), NULL, values, lookup );- return out;+ blocks.push_back( Block{ Pieces(), std::string(), FALSE } );+ split( page.substr( at ), blocks.back().pieces );+ return;}size_t nameAt = attribute + EACH_ATTRIBUTE.size();@@ -181,7 +208,6 @@ std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &vsize_t nameEnd = nameAt;while( nameEnd < page.size() && ( quoted ? page[ nameEnd ] != quote : strchr( " \t\r\n>", page[ nameEnd ] ) == NULL ) )nameEnd++;- const std::string listName = page.substr( nameAt, nameEnd - nameAt );size_t tagEnd = open + 1;while( tagEnd < lower.size() && isalnum( (unsigned char)lower[ tagEnd ] ) )@@ -189,18 +215,60 @@ std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &vconst std::string tag = lower.substr( open + 1, tagEnd - open - 1 );const size_t end = elementEnd( lower, open, tag );- out += filled( page.substr( at, open - at ), NULL, values, lookup );+ blocks.push_back( Block{ Pieces(), std::string(), FALSE } );+ split( page.substr( at, open - at ), blocks.back().pieces );- const std::string element = page.substr( open, end - open );- HtmlLists::const_iterator list = lists.find( listName );- if( list != lists.end() )- for( size_t entry = 0; entry < list->second.size(); entry++ )- out += filled( element, &list->second[ entry ], values, lookup );+ blocks.push_back( Block{ Pieces(), page.substr( nameAt, nameEnd - nameAt ), TRUE } );+ split( page.substr( open, end - open ), blocks.back().pieces );at = end;}}+} // namespace++//-------------------------------------------------------------------------------------------------+std::string HtmlTemplate_escape( const std::string &text )+{+ std::string escaped;+ appendEscaped( escaped, text );+ return escaped;+}++//-------------------------------------------------------------------------------------------------+std::string HtmlTemplate_expand( const std::string &written, const HtmlValues &values,+ const HtmlLists &lists, const HtmlLookup &lookup )+{+ // the command bar's page alone is 38 KB and was lowercased and searched four times a frame;+ // keyed by its text, so a page that changes is cut again rather than served stale.+ // ponytail: never evicted; the game has a dozen fixed pages, add a bound if pages become generated+ static std::map< std::string, Blocks > compiled;+ std::map< std::string, Blocks >::iterator found = compiled.find( written );+ if( found == compiled.end() )+ {+ found = compiled.insert( std::make_pair( written, Blocks() ) ).first;+ compile( written, found->second );+ }++ std::string out;+ out.reserve( written.size() );+ std::string looked;+ for( Blocks::const_iterator block = found->second.begin(); block != found->second.end(); ++block )+ {+ if( !block->each )+ {+ fill( out, block->pieces, NULL, values, lookup, looked );+ continue;+ }++ HtmlLists::const_iterator list = lists.find( block->list );+ if( list != lists.end() )+ for( size_t entry = 0; entry < list->second.size(); entry++ )+ fill( out, block->pieces, &list->second[ entry ], values, lookup, looked );+ }+ return out;+}+//-------------------------------------------------------------------------------------------------static Bool isPageSpace( char character ){diff --git a/GeneralsMD/Code/Tests/test_gameengine.cpp b/GeneralsMD/Code/Tests/test_gameengine.cppindex 15477d90c..8a177f29b 100644--- a/GeneralsMD/Code/Tests/test_gameengine.cpp+++ b/GeneralsMD/Code/Tests/test_gameengine.cpp@@ -14796,6 +14796,20 @@ TEST(html_template_fills_values_and_repeats_each)"<li class=\"t1\" data-each=\"players\"><li>Bo</li></li></ul>""looked" );CHECK_STR( HtmlTemplate_escape( "a\"b'c" ).c_str(), "a"b'c" );++ // the page is cut once and kept by its text: new values still come through on the next call,+ // and a page with other text is cut on its own rather than served the first one's blocks+ values[ "title" ] = "U";+ lists[ "players" ].pop_back();+ CHECK_STR( HtmlTemplate_expand( page, values, lists, lookup ).c_str(),+ "<p class=\"on\">U</p>"+ "<ul><li class=\"t9\" data-each=\"players\"><li><b>&</li></li></ul>"+ "looked" );+ CHECK_STR( HtmlTemplate_expand( page + "{{title}}", values, lists, lookup ).c_str(),+ "<p class=\"on\">U</p>"+ "<ul><li class=\"t9\" data-each=\"players\"><li><b>&</li></li></ul>"+ "lookedU" );+ CHECK_STR( HtmlTemplate_expand( "a {{ open", values, lists, lookup ).c_str(), "a {{ open" );}// litehtml builds an element for every word and every white space character, so the page it is