Multiple micro-optimizations#603
Merged
cmb69 merged 12 commits intophp:masterfrom Jul 3, 2022
Ayesh:modernize/micro-optimizations
Merged
Multiple micro-optimizations#603cmb69 merged 12 commits intophp:masterfrom Ayesh:modernize/micro-optimizations
cmb69 merged 12 commits intophp:masterfrom
Ayesh:modernize/micro-optimizations
Conversation
`ob_get_clean()` is equivalent to `ob_get_contents()` followed by `ob_clean()`.
This is a micro-optimization because `intval()` is a function call, and the type cast is about 6 times fast.
In `./error.php`, there is a `preg_replace('!/+$!', '', $URI);` call that essentially is equivalent to `rtrim()`, that both calls removing trailing slash characters in `$URI`.
The `rtim()` call is more legible and faster.
Improves code readability.
Cleans up the code by removing unnecessary `else` blocks and moving the code to the parent context if the previous `if` block exits the function by either terminating the script, or with a `return` statement.
`isset()` accepts multiple parameters and returns `true` only if all of the parameters are `isset`. It makes sense to combine multiple individual `isset` calls to a single call for better readability.
kamil-tekiela
requested changes
Jul 2, 2022
Regular expression special characters are context-sensitive. For example, special characters such as `.` are not considered special within square braces (`[]`). This removes several of such instances that certain characters are escaped, but it is not strictly necessary within the context. This improves the readability of the expression. See more information at [PHP.Watch: Writing better Regular Expressions in PHP](https://php.watch/articles/php-regex-readability#reduce-escape)
Remove unnecessary `json_last_error() == JSON_ERROR_NONE` where the decoded object is inspected already.
Member
Author
|
Thanks a lot for the review @kamil-tekiela. I made the regex changes as you mentioned (by restoring the original version), and dropped the redundant Additionally, there is a new change in |
Member
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR contains multiple commits with micro-optimizations, hopefully to reduce the review burden reviewing multiple fully fledged PRs.
If any of them don't seem to make sense, I'll happily take them off.
Thank you.
Replace
ob_get_contents();ob_clean()withob_get_clean()ob_get_clean()is equivalent toob_get_contents()followed byob_clean().Replace
intval()calls with(int)type castThis is a micro-optimization because
intval()is a function call, and the type cast is about 6 times fast.Replace
preg_replacecall that could be done with anrtrim()callIn
./error.php, there is apreg_replace('!/+$!', '', $URI);call that essentially is equivalent tortrim(), that both calls removing trailing slash characters in$URI.The
rtim()call is more legible and faster.Combine consecutive
str_replacecalls to a singlestr_replacecallUse short ternary operator where possible
Cascade various
elsestatements where possibleCleans up the code by removing unnecessary
elseblocks and moving the code to the parent context if the previousifblock exits the function by either terminating the script, or with areturnstatement.Combine multiple
isset()calls to a singleisset()isset()accepts multiple parameters and returnstrueonly if all of the parameters areisset. It makes sense to combine multiple individualissetcalls to a single call for better readability.Replace
forloop with aforeachloopRemove unnecessary character escapes in regular expressions
Regular expression special characters are context-sensitive. For example, special characters such as
.are not considered special within square braces ([]).This removes several of such instances that certain characters are escaped, but it is not strictly necessary within the context. This improves the readability of the expression.
A write-up is at PHP.Watch: Writing better Regular Expressions in PHP
Remove unnecessary break statement
Remove unnecessary PHP close tags