Skip to content

Multiple micro-optimizations#603

Merged
cmb69 merged 12 commits intophp:masterfrom
Ayesh:modernize/micro-optimizations
Jul 3, 2022
Merged

Multiple micro-optimizations#603
cmb69 merged 12 commits intophp:masterfrom
Ayesh:modernize/micro-optimizations

Conversation

@Ayesh
Copy link
Member

@Ayesh Ayesh commented Jul 2, 2022

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() with ob_get_clean()
    ob_get_clean() is equivalent to ob_get_contents() followed by ob_clean().

  • Replace intval() calls with (int) type cast
    This is a micro-optimization because intval() is a function call, and the type cast is about 6 times fast.

  • Replace preg_replace call that could be done with an rtrim() call
    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.

  • Combine consecutive str_replace calls to a single str_replace call

  • Use short ternary operator where possible

  • Cascade various else statements where possible
    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.

  • Combine multiple isset() calls to a single isset()
    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.

  • Replace for loop with a foreach loop

  • Remove 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

Ayesh added 8 commits July 2, 2022 16:02
`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.
@Ayesh Ayesh changed the title Modernize/micro optimizations Multiple micro-optimizations Jul 2, 2022
Ayesh added 4 commits July 3, 2022 04:03
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.
@Ayesh
Copy link
Member Author

Ayesh commented Jul 2, 2022

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 JSON_ERROR_NONE checks too.

Additionally, there is a new change in !&lt;\?php(<br />)+!, that I dropped the character escape in <br /> by changing the delimiter from / to !, so we don't have to escape /.

Copy link
Member

@kamil-tekiela kamil-tekiela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@cmb69 cmb69 merged commit 1b83fd7 into php:master Jul 3, 2022
@cmb69
Copy link
Member

cmb69 commented Jul 3, 2022

Thank you!

This was referenced Jul 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants