Hey guys,
I just wanted to ask for some (server) optimization tips.
Things like config settings or lua-functions I shouldnt use at all or exchange to others.
Just general, basic stuff i should note while making scripts.
Thanks. :)
It's good to write code first and then ask others about how it could be made more efficient, if at all since general optimisation is extremely vague and probably not useful to the specific algorithm you're wanting to write.
Feel free to PM me or post here any time with expensive/unfamiliar code to have someone review it and give feedback.
Whilst code_gs raises a fair point about vague clues to optimisation being not useful there is always a few key things to keep in mind. One major thing is that if a calculated value is unlikely to change then you are much better off saving it as a property of the player etc rather than calculating it each frame. This used to be something I did alot by mistake as a new coder and realised that actually that value only changed in very certain circumstances and I could regenerate it then rather than each and every frame.
dont use nw-vars everywhere
Try to avoid using Think hooks if you can, there are plenty of other hooks which work for most situations. If you really need to use Think, keep it as light as possible. Same for drawing hooks like PostDrawOpaqueRenderables. Also, try not to use many networked variables, like chewgum said, and use NetworkVar functions instead of NWVar, Garry said once they were better. If you need to network stuff only at specific moments, it's better to use user messages or the net library.
If something really does need to be done in a think hook, one trick you can use is to put the think hook on the client, have the client know when something should happen, send it in a net message to the server, and [B]CHECK IT SERVERSIDE[/B] before doing the action.
Something I see newer people do fairly often is using something like ents.FindByClass in a render/think hook.
Don't use ents.FindByClass, ents.FindByName, or any of the functions like those in any Think/Render hooks. These iterate over ents.GetAll internally(meaning it has to go through every entity on the map to compare).
Instead, if you need to get a table of specific entities often, use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/OnEntityCreated]GM:OnEntityCreated[/url] and [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityRemoved]GM:EntityRemoved[/url] to add/remove specific entities to your own table. That way you do not have to iterate through [i]every entity on the map[/i] just to find the few you need.
One more thing I see sometimes is people using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Global/Material]Material[/url] or [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/surface/GetTextureID]surface.GetTextureID[/url] inside render/paint functions too. Do these outside of said functions. They only need to be ran once.
[url]https://github.com/FPtje/FProfiler/blob/master/README.MD#about-bottlenecks[/url] has some good explanation of what to do and what not to do when optimizing code. FProfiler itself is a useful tool, I've never tried it myself (my code is optimized from the get go! :science101:) so I can't outright [i]recommend[/i] it, but assuming it works as described, it should be an extremely valuable tool.
[QUOTE=MaxShadow;51976560]Try to avoid using Think hooks if you can, there are plenty of other hooks which work for most situations. If you really need to use Think, keep it as light as possible. Same for drawing hooks like PostDrawOpaqueRenderables. Also, try not to use many networked variables, like chewgum said, and use NetworkVar functions instead of NWVar, Garry said once they were better. If you need to network stuff only at specific moments, it's better to use user messages or the net library.[/QUOTE]
I don't see why people keep thinking that a Think hook will suddenly eat all your frames what matters is what you do inside it.
However lets take a look on why your Think hook might be shit:
[lua]for i = 1, 1000 do
hook.Add("Think", "MyThink" .. i, function()
local n = CurTime() -- extern C func
end)
end[/lua]
Running this will eat nearly 1-2 Frames but as you can see adding a Think hook is not as slow as you actually think.
But there is a easy to actually kill the performance by doing silly things like this:
[lua]for i = 1, 1000 do
hook.Add("Think", "MyThink" .. i, function()
for k,v in pairs(player.GetAll()) do
local pos = v:GetPos()
end
end)
end[/lua]
If you are currently trying this ull notice no big difference if you are running a listen server with no one around.
In my case I was getting 240~ FPS before running the code, after running it with me alone I got around 220~ FPS. Now lets add some bots:
2 Player: 180~ FPS
3 Player: 150~ FPS
..
10 Player: 50~ FPS
So its just a matter of complexity and what you do inside the hook, don't be fooled by claims like in the quote.
- snip -
Sorry, you need to Log In to post a reply to this thread.