Skip to content

Commit

Permalink
Mangling cache (#35)
Browse files Browse the repository at this point in the history
* ManglingCache

* optimize func temp param (#34)
  • Loading branch information
defisym authored Jul 12, 2023
1 parent ddb77b5 commit ba8376c
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 40 deletions.
9 changes: 8 additions & 1 deletion Extensions/Func/Definition.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ struct Data {
bool IsNumber = true;
bool IsNumberChecked = true;

Data() = default;
bool bDefault = false;

Data() {
this->bDefault = true;
}

Data(float Val) {
this->Val = Val;
Expand Down Expand Up @@ -39,6 +43,9 @@ typedef PARAMVEC* LPPARAMVEC;
typedef std::vector<PARAMVEC> PARAMSTACK;
typedef PARAMSTACK* LPPARAMSTACK;

typedef std::unordered_map<std::wstring, std::wstring> MANGLINGNAME;
typedef MANGLINGNAME* LPMANGLINGNAME;

typedef std::unordered_map<std::wstring, Data> PARAMMAP;
typedef PARAMMAP* LPPARAMMAP;

Expand Down
36 changes: 28 additions & 8 deletions Extensions/Func/Func.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ inline void UpdateReturn(LPRDATA rdPtr, const std::wstring& Param) {

inline std::wstring GetRecursiveSuffix(size_t ID) {
switch (ID) {
case 0:
return L"_0";
case 1:
return L"_1";
case 2:
Expand All @@ -100,17 +102,31 @@ inline std::wstring GetRecursiveSuffix(size_t ID) {
}
}

inline std::wstring GetFuncNameWithRecursiveID(LPRDATA rdPtr, const std::wstring& funcName) {
const auto it = rdPtr->RecursiveIndex->find(funcName);
const std::wstring suffix = it == rdPtr->RecursiveIndex->end() ? L"_0" : GetRecursiveSuffix(it->second);
inline const std::wstring& GetFuncNameWithRecursiveID(LPRDATA rdPtr, const std::wstring& funcName) {
const auto it = rdPtr->FuncManglingName->find(funcName);

return funcName + suffix;
if (it == rdPtr->FuncManglingName->end()) {
(*rdPtr->FuncManglingName)[funcName] = funcName + GetRecursiveSuffix(0);
}

return (*rdPtr->FuncManglingName)[funcName];
}

class FuncInfoObject {
LPRDATA rdPtr = nullptr;
std::wstring funcName;
std::wstring manglingName;
std::wstring oldManglingName;

inline std::wstring GetFuncNameWithRecursiveID() const {
const auto it = rdPtr->RecursiveIndex->find(funcName);
const std::wstring suffix = GetRecursiveSuffix(it != rdPtr->RecursiveIndex->end()
? it->second
: 0);

return funcName + suffix;
}

public:
FuncInfoObject(LPRDATA rdPtr, std::wstring& funcName, std::wstring& param) {
this->rdPtr = rdPtr;
Expand All @@ -122,10 +138,12 @@ class FuncInfoObject {
rdPtr->FuncParamStack->emplace_back();
UpdateParam(rdPtr, param);

rdPtr->FuncReturn->clear();
rdPtr->FuncReturn->clear();

(*rdPtr->RecursiveIndex)[funcName] += 1;
this->manglingName = GetFuncNameWithRecursiveID(rdPtr, funcName);

this->manglingName = GetFuncNameWithRecursiveID();
(*rdPtr->FuncManglingName)[funcName] = this->manglingName;

*rdPtr->pPreviousFuncName = rdPtr->FuncNameStack->back();
}
Expand All @@ -142,6 +160,8 @@ class FuncInfoObject {
if ((*rdPtr->RecursiveIndex)[funcName] == 0) {
rdPtr->RecursiveIndex->erase(funcName);
}

(*rdPtr->FuncManglingName)[funcName] = GetFuncNameWithRecursiveID();
}

inline const auto& GetManglingName() { return manglingName; }
Expand Down Expand Up @@ -257,7 +277,7 @@ inline Data& TempParam(LPRDATA rdPtr, const std::wstring& FuncName, const std::w
}

inline bool HasTempParam(LPRDATA rdPtr, const std::wstring& FuncName, const std::wstring& ParamName) {
const auto name = GetFuncNameWithRecursiveID(rdPtr, FuncName);
const auto& name = GetFuncNameWithRecursiveID(rdPtr, FuncName);

return (*rdPtr->FuncTempParam).contains(name) && (*rdPtr->FuncTempParam)[name].contains(ParamName);
}
}
62 changes: 31 additions & 31 deletions Extensions/Func/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ short WINAPI DLLExport Action_SetLoopIndex(LPRDATA rdPtr, long param1, long para
std::wstring FuncName = (LPCTSTR)CNC_GetStringParameter(rdPtr);
size_t LoopIndex = (size_t)CNC_GetIntParameter(rdPtr);

const auto name = GetFuncNameWithRecursiveID(rdPtr, FuncName);
const auto& name = GetFuncNameWithRecursiveID(rdPtr, FuncName);

if (rdPtr->FuncLoopIndex->contains(name)) {
(*rdPtr->FuncCurLoopIndex)[name] = LoopIndex;
Expand All @@ -423,7 +423,7 @@ short WINAPI DLLExport Action_SetLoopIndex(LPRDATA rdPtr, long param1, long para
short WINAPI DLLExport Action_StopLoop(LPRDATA rdPtr, long param1, long param2) {
std::wstring FuncName = (LPCTSTR)CNC_GetStringParameter(rdPtr);

const auto name = GetFuncNameWithRecursiveID(rdPtr, FuncName);
const auto& name = GetFuncNameWithRecursiveID(rdPtr, FuncName);

if (rdPtr->FuncLoopIndex->contains(name)) {
(*rdPtr->FuncCurLoopIndex)[name] = (*rdPtr->FuncLoopIndex)[name];
Expand Down Expand Up @@ -708,7 +708,7 @@ long WINAPI DLLExport Expression_GetRawParam(LPRDATA rdPtr, long param1) {
long WINAPI DLLExport Expression_GetGlobalParamRV(LPRDATA rdPtr, long param1) {
std::wstring ParamName = (LPCTSTR)CNC_GetFirstExpressionParameter(rdPtr, param1, TYPE_STRING);

auto it = rdPtr->GlobalTempParam->find(ParamName);
const auto it = rdPtr->GlobalTempParam->find(ParamName);

if (it != rdPtr->GlobalTempParam->end()) {
Data_StoV(it->second);
Expand All @@ -727,7 +727,7 @@ long WINAPI DLLExport Expression_GetGlobalParamRS(LPRDATA rdPtr, long param1) {
rdPtr->rHo.hoFlags |= HOF_STRING;

//This returns a pointer to the string for MMF.
auto it = rdPtr->GlobalTempParam->find(ParamName);
const auto it = rdPtr->GlobalTempParam->find(ParamName);

if (it != rdPtr->GlobalTempParam->end()) {
Data_VtoS(it->second);
Expand All @@ -749,15 +749,15 @@ long WINAPI DLLExport Expression_GetTempParamRV(LPRDATA rdPtr, long param1) {
}
#endif

if (HasTempParam(rdPtr, FuncName, ParamName)) {
auto& ret = TempParam(rdPtr, FuncName, ParamName);
Data_StoV(ret);
auto& ret = TempParam(rdPtr, FuncName, ParamName);

return ReturnFloat(ret.Val);
}
else {
if (ret.bDefault) {
return 0;
}

Data_StoV(ret);

return ReturnFloat(ret.Val);
}

long WINAPI DLLExport Expression_GetTempParamRS(LPRDATA rdPtr, long param1) {
Expand All @@ -773,30 +773,30 @@ long WINAPI DLLExport Expression_GetTempParamRS(LPRDATA rdPtr, long param1) {
//Setting the HOF_STRING flag lets MMF know that you are a string.
rdPtr->rHo.hoFlags |= HOF_STRING;

//This returns a pointer to the string for MMF.
if (HasTempParam(rdPtr, FuncName, ParamName)) {
auto& ret = TempParam(rdPtr, FuncName, ParamName);
Data_VtoS(ret);
auto& ret = TempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName);

return (long)ret.Str.c_str();
}
else {
//This returns a pointer to the string for MMF.
if (ret.bDefault) {
return (long)Default_Str;
}

Data_VtoS(ret);

return (long)ret.Str.c_str();
}

long WINAPI DLLExport Expression_GetCurrentTempParamRV(LPRDATA rdPtr, long param1) {
std::wstring ParamName = (LPCTSTR)CNC_GetFirstExpressionParameter(rdPtr, param1, TYPE_STRING);

if (HasTempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName)) {
auto& ret = TempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName);
Data_StoV(ret);
auto& ret = TempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName);

return ReturnFloat(ret.Val);
}
else {
if (ret.bDefault) {
return 0;
}

Data_StoV(ret);

return ReturnFloat(ret.Val);
}

long WINAPI DLLExport Expression_GetCurrentTempParamRS(LPRDATA rdPtr, long param1) {
Expand All @@ -805,16 +805,16 @@ long WINAPI DLLExport Expression_GetCurrentTempParamRS(LPRDATA rdPtr, long param
//Setting the HOF_STRING flag lets MMF know that you are a string.
rdPtr->rHo.hoFlags |= HOF_STRING;

//This returns a pointer to the string for MMF.
if (HasTempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName)) {
auto& ret = TempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName);
Data_VtoS(ret);
auto& ret = TempParam(rdPtr, rdPtr->FuncNameStack->back(), ParamName);

return (long)ret.Str.c_str();
}
else {
//This returns a pointer to the string for MMF.
if (ret.bDefault) {
return (long)Default_Str;
}

Data_VtoS(ret);

return (long)ret.Str.c_str();
}

long WINAPI DLLExport Expression_GetRetRV(LPRDATA rdPtr, long param1) {
Expand Down Expand Up @@ -910,7 +910,7 @@ long WINAPI DLLExport Expression_GetCurrentFuncName(LPRDATA rdPtr, long param1)
long WINAPI DLLExport Expression_GetLoopIndex(LPRDATA rdPtr, long param1) {
std::wstring FuncName = (LPCTSTR)CNC_GetFirstExpressionParameter(rdPtr, param1, TYPE_STRING);

const auto name = GetFuncNameWithRecursiveID(rdPtr, FuncName);
const auto& name = GetFuncNameWithRecursiveID(rdPtr, FuncName);

if (rdPtr->FuncLoopIndex->contains(name)) {
return (long)(*rdPtr->FuncCurLoopIndex)[name];
Expand Down
1 change: 1 addition & 0 deletions Extensions/Func/Main.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ typedef struct tagRDATA
LPPARAMSTACK FuncParamStack; // name independent
LPPARAMVEC FuncReturn; // name independent

LPMANGLINGNAME FuncManglingName;// mangling name
LPTPARAM FuncTempParam; // mangling name
LPLIDX FuncLoopIndex; // mangling name
LPLIDX FuncCurLoopIndex; // mangling name
Expand Down
5 changes: 5 additions & 0 deletions Extensions/Func/Runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ short WINAPI DLLExport CreateRunObject(LPRDATA rdPtr, LPEDATA edPtr, fpcob cobPt
rdPtr->FuncParamStack = new PARAMSTACK;
rdPtr->FuncParamStack->reserve(DefaultVecSize);

rdPtr->FuncManglingName = new MANGLINGNAME;
rdPtr->FuncManglingName->reserve(DefaultVecSize);

rdPtr->FuncTempParam = new TPARAM;
rdPtr->FuncTempParam->reserve(DefaultVecSize);

Expand Down Expand Up @@ -136,6 +139,8 @@ short WINAPI DLLExport DestroyRunObject(LPRDATA rdPtr, long fast)

delete rdPtr->pScope;

delete rdPtr->FuncManglingName;

delete rdPtr->FuncNameStack;
delete rdPtr->FuncRawParamStack;

Expand Down
Binary file modified Extensions/Func/ToInstall/Files/Data/Runtime/Unicode/Func.mfx
Binary file not shown.
Binary file not shown.
Binary file modified Extensions/Func/ToInstall/Files/Examples/Func/Func.mfa
Binary file not shown.
Binary file modified Extensions/Func/ToInstall/Files/Extensions/Unicode/Func.mfx
Binary file not shown.

0 comments on commit ba8376c

Please sign in to comment.