Raised This Month: $ Target: $400
 0% 

Better way to declare in loops


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 01-25-2016 , 01:49   Re: Better way to declare in loops
Reply With Quote #1

It doesn't look like the variable even made it into the code in the second snippet at all...
__________________
ddhoward is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-25-2016 , 02:16   Re: Better way to declare in loops
Reply With Quote #2

They probably do optimize that (can't be bothered to check the C++ source) since the JIT does a more or less 1-to-1 mapping of SM bytecode to assembly/machine code.

It's better to assume that the compiler is dumb and does a simple 1-to-1 mapping of pawn to bytecode with no optimizations in place. Having said that, you should only declare things outside of loop if the loop is to be ran an insane number of times (insane being relative, of course). If the loop is only ran several times (on the order of magnitude of 100), depending on complexity it's sometimes better to just leave the declaration in the loop. Otherwise you may introduce subtle bugs in your plugin just for a 0.0000001% speedup or something (premature optimization trap).

Realistically speaking though, integer declarations are pretty much negligible. It's only sizable string or array declarations that you should be looking at moving outside of loops or to global scope.

Last edited by Potato Uno; 01-25-2016 at 02:16.
Potato Uno is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-25-2016 , 07:14   Re: Better way to declare in loops
Reply With Quote #3

PHP Code:
public void OnPluginStart()
{
    for (
int i 0100i++)
    {
        
char buf[512];
        
PrintToServer("%s"buf);
    }

Lysis reversal:
PHP Code:
public void:OnPluginStart()
{
    new 
i;
    while (
100)
    {
        new 
String:buf[512];
        
PrintToServer("%s"buf);
        
i++;
    }
    return 
void:0;

SPEdit disassembly:
PHP Code:
OnPluginStart
23 instruction(s)
starts at code address 0xb68
---
0xb6c: break                           
0xb70: break                           
0xb74push.c 0x0                       0
0xb7c
jump 0xb90                       ; +0x14
0xb84
: break                           
0xb88inc.s 0xfffffffc                 i
0xb90
load.s.pri 0xfffffffc            i
0xb98
: const.alt 0x64                   100
0xba0
jsgeq 0xbf8                      ; +0x58
0xba8
: break                           
0xbacstack 0xfffffe00                 ; -512
0xbb4
zero.pri                        
0xbb8
addr.alt 0xfffffdfc              buf
0xbc0
fill 0x200                       512
0xbc8
: break                           
0xbccpush.adr 0xfffffdfc              buf
0xbd4
push.c 0x8d4                     2260
0xbdc
sysreq.n 2 0x2                   PrintToServer 2
0xbe8
stack 0x200                      512
0xbf0
jump 0xb84                       ; -0x6c
0xbf8
stack 0x4                        4
0xc00
zero.pri                        
0xc04
retn 
So yeah no optimization
Miu is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 11:21   Re: Better way to declare in loops
Reply With Quote #4

Quote:
Originally Posted by Miu View Post
PHP Code:
public void OnPluginStart()
{
    for (
int i 0100i++)
    {
        
char buf[512];
        
PrintToServer("%s"buf);
    }

Lysis reversal:
PHP Code:
public void:OnPluginStart()
{
    new 
i;
    while (
100)
    {
        new 
String:buf[512];
        
PrintToServer("%s"buf);
        
i++;
    }
    return 
void:0;

SPEdit disassembly:
PHP Code:
OnPluginStart
23 instruction(s)
starts at code address 0xb68
---
0xb6c: break                           
0xb70: break                           
0xb74push.c 0x0                       0
0xb7c
jump 0xb90                       ; +0x14
0xb84
: break                           
0xb88inc.s 0xfffffffc                 i
0xb90
load.s.pri 0xfffffffc            i
0xb98
: const.alt 0x64                   100
0xba0
jsgeq 0xbf8                      ; +0x58
0xba8
: break                           
0xbacstack 0xfffffe00                 ; -512
0xbb4
zero.pri                        
0xbb8
addr.alt 0xfffffdfc              buf
0xbc0
fill 0x200                       512
0xbc8
: break                           
0xbccpush.adr 0xfffffdfc              buf
0xbd4
push.c 0x8d4                     2260
0xbdc
sysreq.n 2 0x2                   PrintToServer 2
0xbe8
stack 0x200                      512
0xbf0
jump 0xb84                       ; -0x6c
0xbf8
stack 0x4                        4
0xc00
zero.pri                        
0xc04
retn 
So yeah no optimization
Thank you!

Now I'll just continue declaring it outside the loop block
TheDS1337 is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 01-25-2016 , 08:13   Re: Better way to declare in loops
Reply With Quote #5

Wow

Nice to know... I wonder why such an implementation.
Is there cases where a variable cannot be statically allocated function-wide (stack pointer pushed at beginnign on function and restored at the end)? Those would explain that choice.
h3bus is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-25-2016 , 08:56   Re: Better way to declare in loops
Reply With Quote #6

Quote:
Originally Posted by h3bus View Post
Wow

Nice to know... I wonder why such an implementation.
Is there cases where a variable cannot be statically allocated function-wide (stack pointer pushed at beginnign on function and restored at the end)? Those would explain that choice.
of course not, but the allocation isn't the issue anyway, the implicit zeroing of the entire array is, which can't be moved outside of the loop
Miu is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-25-2016 , 10:39   Re: Better way to declare in loops
Reply With Quote #7

Quote:
Originally Posted by Miu View Post
of course not, but the allocation isn't the issue anyway, the implicit zeroing of the entire array is, which can't be moved outside of the loop
You can move it to the global scope if you're using it in a function that is called frequently. (Most notorious example is OnGameFrame, lesser knowns are some sdkhook callbacks.)

Then the zeroing is done on compile time (according to asherkin).

Last edited by Potato Uno; 01-25-2016 at 10:40.
Potato Uno is offline
Miu
Veteran Member
Join Date: Nov 2013
Old 01-26-2016 , 06:06   Re: Better way to declare in loops
Reply With Quote #8

Quote:
Originally Posted by Potato Uno View Post
You can move it to the global scope if you're using it in a function that is called frequently. (Most notorious example is OnGameFrame, lesser knowns are some sdkhook callbacks.)

Then the zeroing is done on compile time (according to asherkin).
not wat I was talking about but that's silly anyway, just use decl
Miu is offline
Potato Uno
Veteran Member
Join Date: Jan 2014
Location: Atlanta, Georgia
Old 01-26-2016 , 08:37   Re: Better way to declare in loops
Reply With Quote #9

Quote:
Originally Posted by Miu View Post
not wat I was talking about but that's silly anyway, just use decl
decl is cancer that should not be touched (read post above yours).
Potato Uno is offline
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 01-25-2016 , 13:06   Re: Better way to declare in loops
Reply With Quote #10

Quote:
Originally Posted by Miu View Post
of course not, but the allocation isn't the issue anyway, the implicit zeroing of the entire array is, which can't be moved outside of the loop
I'm really not familiar with the new syntax. Does this mean decl has no equivalent in new syntax?
h3bus is offline
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 08:50.


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