Skip to content
This repository has been archived by the owner on Sep 4, 2022. It is now read-only.

[Request] Code Optimizations (kind of) #106

Open
MAGNAT2645 opened this issue Dec 14, 2019 · 2 comments
Open

[Request] Code Optimizations (kind of) #106

MAGNAT2645 opened this issue Dec 14, 2019 · 2 comments
Assignees
Labels
Enhancement New feature or request Solved Fixed issue, added request, etc.
Milestone

Comments

@MAGNAT2645
Copy link
Contributor

MAGNAT2645 commented Dec 14, 2019

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.

  1. 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.

  2. 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.

@MAGNAT2645 MAGNAT2645 added the Enhancement New feature or request label Dec 14, 2019
@Batfoxkid Batfoxkid pinned this issue Dec 14, 2019
@Batfoxkid
Copy link
Owner

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

@Batfoxkid Batfoxkid self-assigned this Jan 6, 2020
@MAGNAT2645
Copy link
Contributor Author

MAGNAT2645 commented Jan 6, 2020

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

@Batfoxkid Batfoxkid added this to the 1.19.6 milestone Jan 13, 2020
@Batfoxkid Batfoxkid unpinned this issue Jan 13, 2020
@Batfoxkid Batfoxkid added the Solved Fixed issue, added request, etc. label Jan 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Enhancement New feature or request Solved Fixed issue, added request, etc.
Projects
None yet
Development

No branches or pull requests

2 participants