Batfoxkid / FreakFortressBat

No longer supported.
https://github.com/Batfoxkid/Freak-Fortress-2-Rewrite
GNU General Public License v3.0
30 stars 20 forks source link

[Request] Code Optimizations (kind of) #106

Open MAGNAT2645 opened 4 years ago

MAGNAT2645 commented 4 years ago

Description

You can add some optimizations to all of FF2 code, i think. These changes aren't so large but they can improve your code execution (i mean, execution speed). It's just a little advice, you don't have to use it if you don't want to.

Applications

You can optimize string checks, string formats and reduce var usage (by freeing the variable from memory earlier than at the end of the function).

Code Snippet

1) You can replace Format functions with FormatEx wherever the output buffer is not used as the same input buffer. Like:

Format( output, sizeof( output ), "%s", output ); // Don't replace this one because output buffer is the same input buffer

Format( output, sizeof( output ), "%s", input ); // You can replace this with FormatEx because input and output buffers aren't same strings (different variables)

FormatEx( output, sizeof( output ), "%s", input ); // This looks fine now
FormatEx( output, sizeof( output ), "%d %f", iMyVar, 35.0 ); // This looks fine too because output buffer is not used as the input buffer

This method can make your code faster a bit.

2) You can replace some string codes. if ( strlen( string ) ) can be replaced with if ( string[0] != 0 ) (or even if ( string[0] != '\0' ) if you use it to check that string isn't empty. strcopy( buffer, sizeof( buffer ), "" ); and Format( buffer, sizeof( buffer ), "" ); can be replaced with buffer[0] = 0 (or even buffer[0] = '\0') if you want to empty string. These methods can make your code faster a bit too.

3) Last thing is that you can use code blocks without any condition (just to restrict local vars). Vars that exist inside block will be freed at the block end. So you can put some vars into separate blocks and these vars will exist only while block is executing. (they won't exist until the end of the whole function)

int Test() {
    int iLocalVar = 0;

    RunTest( iLocalVar );

    AnotherTest1();
    AnotherTest2();
    <large code...>
}

can be replaced with:

int Test() {
    {
        int iLocalVar = 0; // Var exists only inside this block
        RunTest( iLocalVar ); // Var will be freed from memory at the end of block
    }

    AnotherTest1();
    AnotherTest2();
    <large code...>
}

Other Information

There are some advanced mini-lessons about how SourcePawn works and described some hidden features. But ONLY in Russian! (you can use google translation or i can try to explain you something from those lessons) Here.

Batfoxkid commented 4 years ago

With experiments, found out that static char doesn't work with FormatEx. I may end up not using static whenever I need formatting.

MAGNAT2645 commented 4 years ago

This might be because static vars store their values into memory and keep them even after function end.