Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

提升instanceIndex、newInstanceIndex、classIndex三个方法的性能 #436

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 65 additions & 35 deletions Plugins/slua_unreal/Source/slua_unreal/Private/LuaObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,16 @@ namespace NS_SLUA {
}

// get blueprint member
func = cls->FindFunctionByName(UTF8_TO_TCHAR(name));
if (func) {
LuaObject::cacheFunction(L, cls, name, func);
return LuaObject::push(L, func, cls);
}
if (!func && !LuaObject::isFunctionSearched(L, cls, name))
{
func = cls->FindFunctionByName(UTF8_TO_TCHAR(name));
LuaObject::setFunctionSearched(L, cls, name);
if (func)
{
LuaObject::cacheFunction(L, cls, name, func);
return LuaObject::push(L, func, cls);
}
}
return searchExtensionMethod(L,cls,name,true);
}

Expand Down Expand Up @@ -743,32 +748,61 @@ namespace NS_SLUA {
state->classMap.cacheFunc(cls,fname,func);
}

void LuaObject::setFunctionSearched(lua_State* L, UClass* cls, const char* fname)
{
auto state = LuaState::get(L);
auto& item = state->classMap.funcSearchedMap.FindOrAdd(cls);
item.Add(UTF8_TO_TCHAR(fname), true);
}

bool LuaObject::isFunctionSearched(lua_State* L, UClass* cls, const char* fname)
{
auto state = LuaState::get(L);
auto item = state->classMap.funcSearchedMap.Find(cls);
if (!item) return false;
auto finded = item->Find(UTF8_TO_TCHAR(fname));
return finded ? *finded : false;
}

UProperty* LuaObject::findCacheProperty(lua_State* L, UClass* cls, const char* pname)
{
auto state = LuaState::get(L);
return state->classMap.findProp(cls, pname);
}

void LuaObject::cacheProperty(lua_State* L, UClass* cls, const char* pname, UProperty* property)
void LuaObject::cacheAllProperties(lua_State* L, UClass* cls)
{
auto state = LuaState::get(L);
state->classMap.cacheProp(cls, pname, property);
auto state = LuaState::get(L);
if (!state->classMap.propCachedMap.FindOrAdd(cls))
{
auto PropertyLink = cls->PropertyLink;
for (FProperty* Property = PropertyLink; Property != NULL; Property = Property->PropertyLinkNext) {
state->classMap.cacheProp(cls, TCHAR_TO_UTF8(*(Property->GetName())), Property);
}
state->classMap.propCachedMap[cls] = true;
}
}

// cache class property's
void cachePropertys(lua_State* L, UClass* cls) {
auto PropertyLink = cls->PropertyLink;
for (UProperty* Property = PropertyLink; Property != NULL; Property = Property->PropertyLinkNext) {
LuaObject::cacheProperty(L, cls, TCHAR_TO_UTF8(*(Property->GetName())), Property);
}
}
bool LuaObject::arePropertiesCached(lua_State* L, UClass* cls)
{
auto state = LuaState::get(L);
auto finded = state->classMap.propCachedMap.Find(cls);
return finded ? *finded : false;
}

int instanceIndex(lua_State* L) {
UObject* obj = LuaObject::checkValue<UObject*>(L, 1);
const char* name = LuaObject::checkValue<const char*>(L, 2);

UClass* cls = obj->GetClass();
UProperty* up = LuaObject::findCacheProperty(L, cls, name);

if (!up && !LuaObject::arePropertiesCached(L, cls))
{
LuaObject::cacheAllProperties(L, cls);
up = LuaObject::findCacheProperty(L, cls, name);
}

if (up)
{
return LuaObject::push(L, up, obj, nullptr);
Expand All @@ -781,35 +815,31 @@ namespace NS_SLUA {
}

// get blueprint member
FName wname(UTF8_TO_TCHAR(name));
func = cls->FindFunctionByName(wname);
if(!func) {
cachePropertys(L, cls);

up = LuaObject::findCacheProperty(L, cls, name);
if (up) {
return LuaObject::push(L, up, obj, nullptr);
if (!func && !LuaObject::isFunctionSearched(L, cls, name))
{
func = cls->FindFunctionByName(UTF8_TO_TCHAR(name));
LuaObject::setFunctionSearched(L, cls, name);

if (func)
{
LuaObject::cacheFunction(L, cls, name, func);
return LuaObject::push(L,func);
}

// search extension method
return searchExtensionMethod(L, obj, name);
}
else {
LuaObject::cacheFunction(L, cls, name, func);
return LuaObject::push(L,func);
}
// search extension method
return searchExtensionMethod(L, obj, name);
}

int newinstanceIndex(lua_State* L) {
UObject* obj = LuaObject::checkValue<UObject*>(L, 1);
const char* name = LuaObject::checkValue<const char*>(L, 2);
UClass* cls = obj->GetClass();
UProperty* up = LuaObject::findCacheProperty(L, cls, name);
if (!up)
{
cachePropertys(L, cls);
up = LuaObject::findCacheProperty(L, cls, name);
}
if (!up && !LuaObject::arePropertiesCached(L, cls))
{
LuaObject::cacheAllProperties(L, cls);
up = LuaObject::findCacheProperty(L, cls, name);
}
if (!up) luaL_error(L, "Property %s not found", name);
if(up->GetPropertyFlags() & CPF_BlueprintReadOnly)
luaL_error(L,"Property %s is readonly",name);
Expand Down
8 changes: 8 additions & 0 deletions Plugins/slua_unreal/Source/slua_unreal/Private/LuaState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,14 @@ namespace NS_SLUA {
it.RemoveCurrent();

for (ClassCache::CachePropMap::TIterator it(classMap.cachePropMap); it; ++it)
if (!it.Key().IsValid())
it.RemoveCurrent();

for (ClassCache::PropCachedMap::TIterator it(classMap.propCachedMap); it; ++it)
if (!it.Key().IsValid())
it.RemoveCurrent();

for (ClassCache::FuncSearchedMap::TIterator it(classMap.funcSearchedMap); it; ++it)
if (!it.Key().IsValid())
it.RemoveCurrent();

Expand Down
5 changes: 4 additions & 1 deletion Plugins/slua_unreal/Source/slua_unreal/Public/LuaObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -771,9 +771,12 @@ namespace NS_SLUA {

static UFunction* findCacheFunction(lua_State* L,UClass* cls,const char* fname);
static void cacheFunction(lua_State* L, UClass* cls,const char* fame,UFunction* func);
static void setFunctionSearched(lua_State* L, UClass* cls, const char* fname);
static bool isFunctionSearched(lua_State* L, UClass* cls, const char* fname);

static UProperty* findCacheProperty(lua_State* L, UClass* cls, const char* pname);
static void cacheProperty(lua_State* L, UClass* cls, const char* pname, UProperty* property);
static void cacheAllProperties(lua_State* L, UClass* cls);
static bool arePropertiesCached(lua_State* L, UClass* cls);

static bool getObjCache(lua_State* L, void* obj, const char* tn, bool check = true);
static void cacheObj(lua_State* L, void* obj);
Expand Down
8 changes: 8 additions & 0 deletions Plugins/slua_unreal/Source/slua_unreal/Public/LuaState.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ namespace NS_SLUA {

typedef TMap<FString, TWeakObjectPtr<UProperty>> CachePropItem;
typedef TMap<TWeakObjectPtr<UClass>, CachePropItem> CachePropMap;

typedef TMap<TWeakObjectPtr<UClass>, bool> PropCachedMap;
typedef TMap<FString, bool> FuncCachedItem;
typedef TMap<TWeakObjectPtr<UClass>, FuncCachedItem> FuncSearchedMap;

UFunction* findFunc(UClass* uclass, const char* fname);
UProperty* findProp(UClass* uclass, const char* pname);
Expand All @@ -273,10 +277,14 @@ namespace NS_SLUA {
void clear() {
cacheFuncMap.Empty();
cachePropMap.Empty();
propCachedMap.Empty();
funcSearchedMap.Empty();
}

CacheFuncMap cacheFuncMap;
CachePropMap cachePropMap;
PropCachedMap propCachedMap;
FuncSearchedMap funcSearchedMap;
} classMap;

FDeadLoopCheck* deadLoopCheck;
Expand Down