Added basic caching for scripts

This commit is contained in:
Sachith Hasaranga Seneviratne 2014-10-31 16:23:56 +05:30
parent 9822234fc5
commit ebb914d0ef
2 changed files with 93 additions and 46 deletions

View File

@ -48,6 +48,9 @@ ScriptEngine::ScriptEngine()
// Configure the script engine with all the functions,
// and variables that the script should be able to use.
configureEngine(m_engine);
//Cache options
m_use_cache = true;
}
ScriptEngine::~ScriptEngine()
{
@ -95,18 +98,6 @@ void ScriptEngine::runScript(std::string scriptName)
{
return; // Scripting disabled for now
// TODO: this code seems to fetch the script from disk and compile it on every execution?
// A cache should be created.
int r;
// Compile the script code
r = compileScript(m_engine,scriptName);
if( r < 0 )
{
//m_engine->Release();
return;
}
// Create a context that will execute the script.
asIScriptContext *ctx = m_engine->CreateContext();
if (ctx == 0)
@ -115,6 +106,20 @@ void ScriptEngine::runScript(std::string scriptName)
//m_engine->Release();
return;
}
int r; //int for error checking
asIScriptFunction *func;
if (m_use_cache && isCacheMiss(scriptName))
{
// Compile the script code
r = compileScript(m_engine, scriptName);
if (r < 0)
{
//m_engine->Release();
return;
}
if (r < 0)
{
@ -127,7 +132,6 @@ void ScriptEngine::runScript(std::string scriptName)
// Find the function for the function we want to execute.
//This is how you call a normal function with arguments
//asIScriptFunction *func = engine->GetModule(0)->GetFunctionByDecl("void func(arg1Type, arg2Type)");
asIScriptFunction *func;
if (scriptName == "collisions")
{
func = Scripting::Physics::registerScriptCallbacks(m_engine);
@ -153,10 +157,21 @@ void ScriptEngine::runScript(std::string scriptName)
return;
}
//CACHE UPDATE
m_function_cache.push_back(func);
m_cache_index.push_back(scriptName);
}
else
{
//Script present in cache
int index = getCacheIndex(scriptName);
func = m_function_cache[index];
//std::cout << "FOUND CACHED : " << scriptName << std::endl;
}
// Prepare the script context with the function we wish to execute. Prepare()
// must be called on the context before each new script function that will be
// executed. Note, that if you intend to execute the same function several
// times, it might be a good idea to store the function returned by
// executed. Note, that if because we intend to execute the same function
// several times, we will store the function returned by
// GetFunctionByDecl(), so that this relatively slow call can be skipped.
r = ctx->Prepare(func);
if( r < 0 )
@ -282,4 +297,31 @@ int ScriptEngine::compileScript(asIScriptEngine *engine, std::string scriptName)
return 0;
}
//-----------------------------------------------------------------------------
/** Checks if the script has already been compiled/built before
* \param string scriptname = name of the script currently under consideration
*/
bool ScriptEngine::isCacheMiss(std::string scriptname)
{
for (int i = 0; i < m_cache_index.size(); i++)
{
if (m_cache_index[i] == scriptname) return false;
}
return true;
}
//-----------------------------------------------------------------------------
/** Returns index of a script in the cache
* \param string scriptname = name of the script currently under consideration
*/
int ScriptEngine::getCacheIndex(std::string scriptname)
{
for (int i = 0; i < m_cache_index.size(); i++)
{
if (m_cache_index[i] == scriptname) return i;
}
return -1;
}
}

View File

@ -68,7 +68,12 @@ namespace Scripting
private:
asIScriptEngine *m_engine;
std::vector <std::string> m_cache_index;
std::vector <asIScriptFunction*> m_function_cache;
bool m_use_cache;
int getCacheIndex(std::string scriptname);
bool isCacheMiss(std::string scriptname);
void configureEngine(asIScriptEngine *engine);
int compileScript(asIScriptEngine *engine,std::string scriptName);
}; // class ScriptEngine