精選文章

SmallBurger Asset Home

  SmallBurger

2011年3月30日 星期三

關於AI for LUA的效能問題…

  最近在整合AI,發現per frame 大量call LUA bind function會造成效能上的問題,於是加入了措開處理機制(目的是要降低per frame call的次數),發現效果出奇的好,原本40~20的fps提升到100~90(測試的數量為5000隻,之前有用openmp來測試call LUA bind function,但是會造成死結,就放棄了)。

  相關參考code如下:
const bool CMDBrainProcesser::UpdateLUAContex(const float& fDeltaTime)
{
 m_fRemainingUpdateLUAContexTime -= fDeltaTime;
 if(m_fRemainingUpdateLUAContexTime <= 0.0f)
 {
  static std::string s_strTargetFunctionName;
  s_strTargetFunctionName = m_strCurrentStateName + "_Update";
  luabind::call_function(m_pLUAContex->GetLuaState(), s_strTargetFunctionName.c_str(),
   ms_fUpdateLUAContexIntervalTime-m_fRemainingUpdateLUAContexTime);
  m_fRemainingUpdateLUAContexTime = ms_fUpdateLUAContexIntervalTime;
  return true;
 }
 return false;
}
  其中每一個AI物件的m_fRemainingUpdateLUAContexTime都不同,比方第一個是0.01,第二個是0.02,第三個是0.03,以此類推,而ms_fUpdateLUAContexIntervalTime是固定的,目前是0.5秒。
  這個結果算蠻能接受的,我可不想什麼事都沒做,server就已經lag成這樣(雖然AI處理的部份應該是Server負擔最重的其中之一…),接下來就是server backend viewer…

2 則留言:

  1. 不論使用哪種 binding 方法,binding functions 的 overhead 都滿重的。

    我想還是得盡量減少呼叫 binding functions,才比較能節省效能。

    回覆刪除
  2. 嗯!的確,如何減少call的次數是效能關鍵(尤其是per frame call)。

    回覆刪除