Changeset 27698

Timestamp:
Jun 14, 2023, 9:32:22 AM (14 months ago)
Author:
wraitii
Message:

Speed up AI computation by not using structured clones

Following the AI unthreading in rP26274 / D3769, structured clones are no longer needed to pass the gamestate around.
This is much faster, about 10-15% wall time on an AI 1v1 profile.

This also potentially reduces memory fragmentation and GC needs, as writing clones allocated a lot of data.

Differential Revision: https://code.wildfiregames.com/D5019

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/source/simulation2/components/CCmpAIManager.cpp

    r27323 r27698  
    522522    }
    523523
    524     void UpdateGameState(const Script::StructuredClone& gameState)
     524    void UpdateGameState( gameState)
    525525    {
    526526        ENSURE(m_CommandsComputed);
    527         m_GameState = gameState;
     527        m_GameState.reset();
     528        m_GameState.init(ScriptRequest(m_ScriptInterface).cx, gameState);
    528529    }
    529530
     
    798799        // Deserialize the game state, to pass to the AI's HandleMessage
    799800        ScriptRequest rq(m_ScriptInterface);
    800         JS::RootedValue state(rq.cx);
    801801        {
    802802            PROFILE3("AI compute read state");
    803             Script::ReadStructuredClone(rq, m_GameState, &state);
    804             Script::SetProperty(rq, state, "passabilityMap", m_PassabilityMapVal, true);
    805             Script::SetProperty(rq, state, "territoryMap", m_TerritoryMapVal, true);
     803            Script::SetProperty(rq, m_GameState, "passabilityMap", m_PassabilityMapVal, true);
     804            Script::SetProperty(rq, m_GameState, "territoryMap", m_TerritoryMapVal, true);
    806805        }
    807806
     
    816815        {
    817816            PROFILE3("AI run shared component");
    818             ScriptFunction::CallVoid(rq, m_SharedAIObj, "onUpdate", state);
     817            ScriptFunction::CallVoid(rq, m_SharedAIObj, "onUpdate", tate);
    819818        }
    820819
     
    826825
    827826            if (m_HasSharedComponent && m_Players[i]->m_UseSharedComponent)
    828                 m_Players[i]->Run(state, m_Players[i]->m_Player, m_SharedAIObj);
     827                m_Players[i]->Run(tate, m_Players[i]->m_Player, m_SharedAIObj);
    829828            else
    830                 m_Players[i]->Run(state, m_Players[i]->m_Player);
     829                m_Players[i]->Run(tate, m_Players[i]->m_Player);
    831830        }
    832831    }
     
    848847    std::set<std::wstring> m_LoadedModules;
    849848
    850     Script::StructuredClone m_GameState;
     849    e m_GameState;
    851850    Grid<NavcellData> m_PassabilityMap;
    852851    JS::PersistentRootedValue m_PassabilityMapVal;
     
    10041003
    10051004        // Update the game state
    1006         m_Worker.UpdateGameState(Script::WriteStructuredClone(rq, state));
     1005        m_Worker.UpdateGameState();
    10071006
    10081007        // Update the pathfinding data
Note: See TracChangeset for help on using the changeset viewer.