Raised This Month: $12 Target: $400
 3% 

OciXCrom's Rank System [XP|Levels|Ranks]


Post New Thread Reply   
 
Thread Tools Display Modes
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-23-2019 , 08:35   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #291

I pushed the new update with HamletEagle's recommendations.
__________________

Last edited by OciXCrom; 07-23-2019 at 08:36.
OciXCrom is offline
Send a message via Skype™ to OciXCrom
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 07-23-2019 , 12:05   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #292

Disregard my previous post, it was incomplete. I planned to write a more detailed explanation about new/static, but I had to go so I figured posting something is better than nothing.
I am not familiar with the pawn assembly, so I'll use x86 specific registers, but this should suffice to understand the core concepts.

new is used to create a new variable on the stack. The stack is a storage space with a LIFO policy: last in first out(the last variable added to the stack is the first to be removed). It has a base register(the base address of the stack), a stack pointer register named esp(at which address we are currently in the stack) and it grows downwards(from the base address towards 0).

Creating a new variable means allocating some space for it on the stack, which in turn means moving esp to grow the stack. Since the stack grows downwards we actually have to lower esp.
A variable has 4 bytes, therefore we subtract 4 from the esp to make room for a new variable. This is called pushing to the stack.
To delete the variable we simply have to move the esp back to what it was, so add 4 to the esp. This is called popping the stack.
When a function exits it is responsible to leave the stack in the same state as when the function was called: the function has to clean all the variables it created before finishing it's job. This is exactly why local variables are not preserved between function calls.

In pawn variables are initialized to 0, so after changing esp we have to put 0 in that memory location.
The corresponding assembly would be:
PHP Code:
sub esp//grow the stack and make room for 4 bytes - one variable
mov dword [esp], //write 0 at the address pointed by esp register. dword only tells how to write to that memory location(dword 4 bytes/word 2 bytes/byte 1 byte)

//later
add esp//clean the stack 
Moving from one variable to one array: creating/deleting is pretty much the same, but instead of 4 we have to subtract/add 4 * ARRAY_SIZE. However we have to 0 the entire array, which we can't do with a simple mov instruction(would require a loop) so the cost of creating the array mainly comes from 0'ing the memory location, not from making space for the variable.

PHP Code:
   sub esp4000 // make room for one array of size 1000
    
    
mov ecx1000 // put the size of the array in the counter register. we need this to simulate a loop(ecx - the "i" variable from an usual loop)
initialize_memory:
    
mov dword [esp ecx 4], // esp is the base address for the array. ecx will be the index and we multiply by 4 because each cell is 4 bytes to get the actual cell address
    
sub ecx// decrement "i"
    
jnz initialize_memory // if "i" is not 0 keep looping - will jump back to initialize_memory exactly like a goto + if check
    
    
add esp4000 // free the memory 
I showed the 2 assembly code just for illustration purpose, I hope it may help to understand what happens behind the scenes when declaring variables/arrays.

This has the following implications:
-if we are in a heavy function like something called each frame(a task at 1.0 seconds does not qualify for heavy function) and we have an array of size 1000 or so, we can use static to avoid 0'ing the memory every time and saving very little time. Doing this is reasonable, but not performance-critical. 256 is definitely not big enough to worry about it.
-if we have a static variable and then we always assign a new value to it that's not any better than creating the variable with "new". We used static so we no longer have the continuous initialization with 0, but we still write to that memory on each function call.

There's also one more case where static can be useful when dealing with big arrays: when we do for example new array[40000] and we get a stack error. The error says we used too much memory and filled the stack. If we use static, then the array is no longer placed on the stack, but in the "data" section along with all the global variables(because static is implemented like a global variable, only with a restricted scope).
This is just FYI, if in any case someone needs so much memory to fill the entire stack then the plugin logic may need to be changed.

Compile this:
PHP Code:
#include <amxmodx>

public plugin_init()
{
    
func()
}

func()
{
    static 
a[12345]
    static 
b[12345]
    static 
c[12345]

and look at the output from the compiler.
When using static it says: Data size: 148140 bytes(which is exactly 12345 * 3 * 4, *4 because one var = 4 bytes). The static variables were placed in the data section.

Switch static to new and recompile: Data size: 0 bytes. nothing was placed in data section because we are not using any global/static variables.

So, when do you really want to use a static variable:
-for it's indented purpose: to save the value of the variable
-in pre frame functions when the array is really big: order of thousands of cells.
-if you are going to consume all the stack space(arguably).

Wasting precious time thinking if this variable should be new or static is useless, but if you think about the 3 cases above it should be clear enough.

Hope it makes sense and it clears the topic. Feel free to ask anything.
__________________

Last edited by HamletEagle; 07-23-2019 at 12:21.
HamletEagle is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-23-2019 , 16:32   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #293

Thanks for the detailed explanation. I understood most of it, while some of the stuff are way out of my field of knowledge, but I'm sure it will be helpful for anyone dealing with the "static or new" issue.

I'll use the 3 situations you pointed out on when to use "static" from now on.

One question though - what about declaring variables inside a loop? Look at the one "static" I left in my code on line 623. It's a "while" loop, so I can't declare it in the body of it.

--------------------------------------------------

When you have the time to continue reviewing, I would like to ask you to make a more in-depth review of the SQL part of the code, since this is my first time using SQL in a plugin and I have been getting some reports of SQL throwing errors. Here's one here.

I need to know if this is a problem from my plugin or something else. Thanks again.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
mi0epro
New Member
Join Date: Dec 2018
Old 07-23-2019 , 23:11   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #294

About the connections. I've had the same error and it was due to not freeing the handles on couple places.
PS: Aaand yep i just saw you are not freeing g_iSqlTuple in plugin_end.

Last edited by mi0epro; 07-23-2019 at 23:15.
mi0epro is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 07-26-2019 , 07:47   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #295

Quote:
Originally Posted by mi0epro View Post
About the connections. I've had the same error and it was due to not freeing the handles on couple places.
PS: Aaand yep i just saw you are not freeing g_iSqlTuple in plugin_end.
Did freeing the handle in "plugin_end" fix the problem for you?
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
mi0epro
New Member
Join Date: Dec 2018
Old 07-26-2019 , 09:56   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #296

It did fix it.
mi0epro is offline
HamletEagle
AMX Mod X Plugin Approver
Join Date: Sep 2013
Location: Romania
Old 08-01-2019 , 12:20   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #297

Quote:
Originally Posted by OciXCrom View Post
When you have the time to continue reviewing, I would like to ask you to make a more in-depth review of the SQL part of the code, since this is my first time using SQL in a plugin and I have been getting some reports of SQL throwing errors. Here's one here.

I need to know if this is a problem from my plugin or something else. Thanks again.
I never worked with the mysql amxx api, but I read the docs before reviewing and it looked pretty straightforward.

Quote:
Originally Posted by mi0epro View Post
It did fix it.
I really doubt that. https://github.com/alliedmodders/amx...e/sqlx.inc#L36 states that freeing/not freeing this has no effect over the connection, it just cleans the cached information. I would say to free it regardless tho.

I can't say for sure why it's not working for some people, but here are a few things you may try:

Look at line 239. If the query fails it will never reach
PHP Code:
SQL_FreeHandle(iQueries)
SQL_FreeHandle(iSqlConnection
Freeing the handles should be done even if the code enters in the if check body.
I do not know if this is the source of the problem reported before, but it certainly can be a problem when/if a query fails.
(this applies to all places where you are doing that, not gonna list them all).

Also, did you manage to reproduce the issue?
__________________

Last edited by HamletEagle; 08-01-2019 at 12:21.
HamletEagle is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 08-01-2019 , 16:13   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #298

This is my first time working with it as well.
I can't reproduce the issue as I don't have the stuff necessary to do it.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Dexon
Member
Join Date: Aug 2019
Old 08-17-2019 , 06:26   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #299

Hey! It works fine, but I can't see the rank on the chat next to my name when I write. Please help.
Dexon is offline
OciXCrom
Veteran Member
Join Date: Oct 2013
Location: Macedonia
Old 08-17-2019 , 07:27   Re: OciXCrom's Rank System [XP|Levels|Ranks]
Reply With Quote #300

Quote:
Originally Posted by Dexon View Post
Hey! It works fine, but I can't see the rank on the chat next to my name when I write. Please help.
There is no such option in this plugin because that would require an entire chat system which I already made - https://forums.alliedmods.net/showthread.php?t=297952 - you can use this plugin to display the rank in chat, there is an explanation in the first post of this thread on how to do it.
__________________
OciXCrom is offline
Send a message via Skype™ to OciXCrom
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:16.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode