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

Better way to declare in loops


Post New Thread Reply   
 
Thread Tools Display Modes
Miu
Veteran Member
Join Date: Nov 2013
Old 01-25-2016 , 04:54   Re: Better way to declare in loops
Reply With Quote #11

Quote:
Originally Posted by DeagLe.Studio View Post
Declaring the variable outside the "for" block:

...
You need to do something with the variable so it won't optimize it out
Miu is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 06:37   Re: Better way to declare in loops
Reply With Quote #12

Quote:
Originally Posted by Miu View Post
You need to do something with the variable so it won't optimize it out
Oh, I didnt know that.

But anyways guys, I always declare vars outside the loop. the thing is that I love to choose a good coding style if you know what I mean, and recently I saw a lot of people declaring the vars inside the loop I thought it'll be cool to do it (but only if compiler actually optimize it).
TheDS1337 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 #13

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
h3bus
AlliedModders Donor
Join Date: Nov 2013
Old 01-25-2016 , 08:13   Re: Better way to declare in loops
Reply With Quote #14

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 #15

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 #16

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
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 01-25-2016 , 11:21   Re: Better way to declare in loops
Reply With Quote #17

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 , 13:06   Re: Better way to declare in loops
Reply With Quote #18

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
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 01-25-2016 , 13:29   Re: Better way to declare in loops
Reply With Quote #19

Quote:
Originally Posted by h3bus View Post
I'm really not familiar with the new syntax. Does this mean decl has no equivalent in new syntax?
Exactly. There is no decl in new syntax.
__________________
Not currently working on SourceMod plugin development.
Powerlord is offline
friagram
Veteran Member
Join Date: Sep 2012
Location: Silicon Valley
Old 01-25-2016 , 15:46   Re: Better way to declare in loops
Reply With Quote #20

it's not all pre allocated, pretty often I do
decl array [size]
where size is the call count of a dynamic array or some config variable.

you can use static too, which should allocate it on runtime and keep it there...

decl still works too, as reading out a string or variable that is not initialized gives you the appropriate garbage output.
__________________
Profile - Plugins
Add me on steam if you are seeking sp/map/model commissions.
friagram is offline
Reply



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 12:07.


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