Skip to content

Commit dcfe492

Browse files
committed
Trying to improve performance
1 parent ef274c7 commit dcfe492

File tree

2 files changed

+79
-90
lines changed

2 files changed

+79
-90
lines changed

source/lua_utils.cpp

Lines changed: 61 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -101,90 +101,82 @@ void SafeDelete(ILuaValue* value)
101101

102102
void FillValue(GarrysMod::Lua::ILuaBase* LUA, ILuaValue* val, int iStackPos, int type)
103103
{
104-
if (type == GarrysMod::Lua::Type::Number)
104+
LUA_Vector* vec;
105+
LUA_Angle* ang;
106+
LUA_File* file;
107+
LUA_File* copy;
108+
std::unordered_map<ILuaValue*, ILuaValue*> tbl;
109+
switch(type)
105110
{
106-
val->type = type;
107-
val->number = LUA->GetNumber(iStackPos);
108-
} else if (type == GarrysMod::Lua::Type::Bool)
109-
{
110-
val->type = type;
111-
val->number = LUA->GetBool(iStackPos) ? 1 : 0;
112-
} else if (type == GarrysMod::Lua::Type::String)
113-
{
114-
val->type = type;
115-
val->string = LUA->GetString(iStackPos);
116-
} else if (type == GarrysMod::Lua::Type::Entity)
117-
{
118-
//val->type = type;
119-
//val->number = ((CBaseEntity*)ILUA->GetObject(3)->GetEntity())->edict()->m_EdictIndex;
120-
} else if (type == GarrysMod::Lua::Type::Vector)
121-
{
122-
val->type = type;
123-
if (ThreadInMainThread())
124-
{
125-
const Vector& vec = LUA->GetVector(iStackPos);
126-
val->x = vec.x;
127-
val->y = vec.y;
128-
val->z = vec.z;
129-
} else {
130-
LUA_Vector* vec = Vector_Get(LUA, iStackPos);
111+
case GarrysMod::Lua::Type::Number:
112+
val->type = type;
113+
val->number = LUA->GetNumber(iStackPos);
114+
return;
115+
case GarrysMod::Lua::Type::Bool:
116+
val->type = type;
117+
val->number = LUA->GetBool(iStackPos) ? 1 : 0;
118+
return;
119+
case GarrysMod::Lua::Type::String:
120+
val->type = type;
121+
val->string = LUA->GetString(iStackPos);
122+
return;
123+
case GarrysMod::Lua::Type::Entity:
124+
//val->type = type;
125+
//val->number = ((CBaseEntity*)ILUA->GetObject(3)->GetEntity())->edict()->m_EdictIndex;
126+
return;
127+
case GarrysMod::Lua::Type::Vector:
128+
val->type = type;
129+
vec = Vector_Get(LUA, iStackPos);
131130
val->x = vec->x;
132131
val->y = vec->y;
133132
val->z = vec->z;
134-
}
135-
} else if (type == GarrysMod::Lua::Type::Angle)
136-
{
137-
val->type = type;
138-
if (ThreadInMainThread())
139-
{
140-
const QAngle& ang = LUA->GetAngle(iStackPos);
141-
val->x = ang.x;
142-
val->y = ang.y;
143-
val->z = ang.z;
144-
} else {
145-
LUA_Angle* ang = Angle_Get(LUA, iStackPos);
133+
return;
134+
case GarrysMod::Lua::Type::Angle:
135+
val->type = type;
136+
ang = Angle_Get(LUA, iStackPos);
146137
val->x = ang->x;
147138
val->y = ang->y;
148139
val->z = ang->z;
149-
}
150-
} else if (type == GarrysMod::Lua::Type::Table)
151-
{
152-
val->type = type;
153-
std::unordered_map<ILuaValue*, ILuaValue*> tbl;
140+
return;
141+
case GarrysMod::Lua::Type::Table:
142+
val->type = type;;
154143

155-
LUA->Push(iStackPos);
156-
LUA->PushNil();
157-
while (LUA->Next(-2)) {
158-
LUA->Push(-2);
144+
LUA->Push(iStackPos);
145+
LUA->PushNil();
146+
while (LUA->Next(-2)) {
147+
LUA->Push(-2);
159148

160-
ILuaValue* key = new ILuaValue;
161-
FillValue(LUA, key, -1, LUA->GetType(-1));
149+
ILuaValue* key = new ILuaValue;
150+
FillValue(LUA, key, -1, LUA->GetType(-1));
162151

163-
ILuaValue* new_val = new ILuaValue;
164-
FillValue(LUA, new_val, -2, LUA->GetType(-2));
152+
ILuaValue* new_val = new ILuaValue;
153+
FillValue(LUA, new_val, -2, LUA->GetType(-2));
165154

166-
tbl[key] = new_val;
155+
tbl[key] = new_val;
167156

168-
LUA->Pop(2);
169-
}
170-
LUA->Pop(1);
157+
LUA->Pop(2);
158+
}
159+
LUA->Pop(1);
171160

172-
val->tbl = tbl;
173-
} else if (type == GarrysMod::Lua::Type::File)
174-
{
175-
if (ThreadInMainThread()) // We cannot push a File from GMod to our module.
176-
{
161+
val->tbl = tbl;
177162
return;
178-
}
163+
case GarrysMod::Lua::Type::File:
164+
if (ThreadInMainThread()) // We cannot push a File from GMod to our module.
165+
{
166+
return;
167+
}
179168

180-
LUA_File* file = File_Get(LUA, iStackPos);
181-
LUA_File* copy = new LUA_File;
182-
copy->fileMode = file->fileMode;
183-
copy->filename = file->filename;
184-
copy->path = file->path;
185-
//copy->handle = file->handle // Should we really share the handle?
186-
val->type = type;
187-
val->data = copy;
169+
file = File_Get(LUA, iStackPos);
170+
copy = new LUA_File;
171+
copy->fileMode = file->fileMode;
172+
copy->filename = file->filename;
173+
copy->path = file->path;
174+
//copy->handle = file->handle // Should we really share the handle?
175+
val->type = type;
176+
val->data = copy;
177+
return;
178+
default:
179+
return;
188180
}
189181
}
190182

@@ -336,22 +328,4 @@ std::string ToPath(std::string path)
336328
}
337329

338330
return path;
339-
}
340-
341-
ILuaValue* CreateValue(int value)
342-
{
343-
ILuaValue* val = new ILuaValue;
344-
val->type = GarrysMod::Lua::Type::Number;
345-
val->number = value;
346-
347-
return val;
348-
}
349-
350-
ILuaValue* CreateValue(const char* value)
351-
{
352-
ILuaValue* val = new ILuaValue;
353-
val->type = GarrysMod::Lua::Type::String;
354-
val->string = value;
355-
356-
return val;
357331
}

source/lua_utils.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,24 @@ inline bool EqualValue(ILuaValue* val1, ILuaValue* val2)
258258
return false;
259259
}
260260

261+
inline ILuaValue* CreateValue(int value)
262+
{
263+
ILuaValue* val = new ILuaValue;
264+
val->type = GarrysMod::Lua::Type::Number;
265+
val->number = value;
266+
267+
return val;
268+
}
269+
270+
inline ILuaValue* CreateValue(const char* value)
271+
{
272+
ILuaValue* val = new ILuaValue;
273+
val->type = GarrysMod::Lua::Type::String;
274+
val->string = value;
275+
276+
return val;
277+
}
278+
261279
extern GMOD_Info* GMOD;
262280
extern int interfaces_count;
263281
extern std::unordered_map<double, ILuaThread*> interfaces;
@@ -270,9 +288,6 @@ extern void PushValue(GarrysMod::Lua::ILuaBase*, ILuaValue*);
270288
extern void SafeDelete(ILuaValue*);
271289
extern void FillValue(GarrysMod::Lua::ILuaBase*, ILuaValue*, int, int);
272290

273-
extern ILuaValue* CreateValue(int);
274-
extern ILuaValue* CreateValue(const char*);
275-
276291
extern void Add_Func(GarrysMod::Lua::ILuaBase*, GarrysMod::Lua::CFunc, const char*);
277292
extern ILuaThread* FindThread(int);
278293

0 commit comments

Comments
 (0)