Everybody hates lag, right? There is two main reasons for lag: Hardware and plugins.Well upgrading hardware isn't always that cheap and usually out of our possibilities so all we have left is to optimise software and especially plugins. This thread is about how to do it right and how you shouldn't do it. Loops are evil When loops are executing the thread it runs in will wait for it to stop executing. It's bad idea to call slow/heavy functions in it. Lets look at this example: PHP: // Bad// In context of PluginTaskpublic function onRun($currentTick){ foreach($this->getPlugin()->getServer()->getOnlinePlayers() as $player) { $popup = $player->getName().' | '.$player->getLevel()->getName().' | Time: '.date('G.i', time()); $player->sendPopup($popup); }} As you can see above name and name of level will always differ from player to player but not Time so we don't have to format it and call time function on each loop. This is how you should do. PHP: // Good// In context of PluginTaskpublic function onRun($currentTick){ $date = date('G.i', time()); foreach($this->getPlugin()->getServer()->getOnlinePlayers() as $player) { $popup = $player->getName().' | '.$player->getLevel()->getName().' | Time: '.$date; $player->sendPopup($popup); }} Recommended interval to show constant popup: 10 - 30 ticks. (Needs more testing) Strings Spoiler: Premature optimization Single or Double quotes? String that's wrapped with double quotes will be parsed to find variables and convert their value to readable text. Example: PHP: $website = 'http://forums.pocketmine.net/';echo "Visit <a href='$website'>PocketMine Forums</a>"; Output (HTML Code): Code: Visit <a href='http://forums.pocketmine.net/'>PocketMine Forums</a> Output if you used single quotes instead of double: Code: Visit <a href='$website'>PocketMine Forums</a> When a string is specified in double quotes or with heredoc, variables are parsed within it. Even if string wrapped with double quotes or with heredoc are empty it still be parsed to search for variable and it takes a really tiny bit amount of time but if you have huge code with strings this time is increased. Next time you create a string, think if you really need double quotes or not. Concatening variables is faster than just putting them in a double-quotation mark string. Facts/Tips/Tricks Did you know, string can be as large as up to 2GB (2147483647 bytes maximum) ? A string literal can be specified in two more ways: heredoc syntax nowdoc syntax Have you ever tried to increment string? (Source) PHP: $a = 'fact_2';echo ++$a;$a = '2nd_fact';echo ++$a;$a = 'a_fact';echo ++$a;$a = 'a_fact?';echo ++$a; Guess what. Code: fact_3 2nd_facu a_facu a_fact? Surprised? Using an increment operator on a string that ends with a number has the effect of increasing the letter (the one following it in the alphabet, e.g. t -> u). Regardless if the string starts with digits or not, the last character is changed. But there is no effect if the string ends with a non-alphanumeric character. This is well documented by the official documentation about the incrementing/decrementing operators but most of you may not have read it because you probably thought nothing special was there. I must admit I thought the same until a short time ago. $string[0] / $string{0} Strings may also be accessed as arrays but this returns character from index. Example: PHP: $string = 'PocketMine';print $string{0}.$string{6}; // PM Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead.
I know. But you're a bit premature optimization here PHP 6 never came out. And in PHP 7, using curly braces still works. About single/double quotes, double quotes existed for a reason. The difference you mentioned is extremely negligible. In my recent tests, I obtained a result of {176,170} vs {175, 180}. The maximum difference is 17:18. This is extremely negligible, given that it takes about 0.017 second to run 1000000 times. Many other things would create lag before this one creates lag. That's why I pointed out that you are suggesting premature optimization. Rather than replacing all your quotes, you should be spending your time to find out real major issues that lag the server.
About single and double quotes: https://3v4l.org/uunHu/perf#tabs double quotes https://3v4l.org/2uARm/perf#tabs single quotes So you see, there isn't really a lot of difference.
You may also be interested in the difference between $string[1] and $string{1}": https://3v4l.org/mocTH/perf#tabs https://3v4l.org/chvpN/perf#tabs
Check this. Executing date() and time() on each loop: https://3v4l.org/kn35L/perf#tabs Using variable: https://3v4l.org/jLNAK/perf#tabs
Yep. That's why these lines exist. https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/utils/Utils.php#L132 https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/utils/Utils.php#L174