Skip to content

[run-tests.php] Multiple minor improvements for performance and readability #8963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 22, 2022

Conversation

Ayesh
Copy link
Member

@Ayesh Ayesh commented Jul 9, 2022

  • Improve non-optimal nested if/elseif/else blocks with happy path optimizations
    Simplifies and improves the readability of multiple if/elseif/else blocks by removing them when an earlier branch exists the execution flow by either returning or terminating the script.
  • Minor optimizations in if blocks by placing simple expressions first
  • Merge multiple unset() calls to a single call
  • Replace backtick operator string literals with shell_exec() calls
    The rationale is that shell_exec() is identical to the backtick operator (both of which are disabled when shell_exec function is disabled) makes it very clear that it is a shell execution, and eases security audits too.
  • Combine multiple str_replace calls to a single strtr call
    Makes the replacement easier to see, neatly aligned, and only takes one function call.
    This is safe because none of the combined replacement values contain tokens that would be recursively replaced.

@Ayesh Ayesh force-pushed the run-test-improvements branch from a6868e7 to 5833053 Compare July 9, 2022 17:36
}

if (is_array($pattern_match)) {
$test_files = array_merge($test_files, $pattern_match);
}
} elseif (is_dir($testfile)) {
find_files($testfile);
} elseif (substr($testfile, -5) == '.phpt') {
Copy link
Contributor

Choose a reason for hiding this comment

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

Doing this if case before the is_dir() one would reduce file IO

Copy link
Member Author

Choose a reason for hiding this comment

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

You are right the substr call (which I'm planning to replace with a str_ends_with) is better checked first, but I'm afraid this would slightly alter the test discovery flow? For example when there is a directory named foo.phpt (despite the very small odds), that I believe is still covered in the original flow of checks, but not with the suggested change.

@Ayesh Ayesh force-pushed the run-test-improvements branch from 5833053 to 87425af Compare July 9, 2022 18:23
@Ayesh Ayesh marked this pull request as ready for review July 9, 2022 18:50
@Ayesh Ayesh force-pushed the run-test-improvements branch from 87425af to 23ca93e Compare July 10, 2022 19:46
@Ayesh
Copy link
Member Author

Ayesh commented Jul 10, 2022

This PR is now rebased to master branch, now that #8965 is merged.

Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

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

LGTM

}

if (preg_match("/^$wanted_re\$/s", $output)) {
$wanted_re = strtr($wanted_re, [
Copy link
Member

Choose a reason for hiding this comment

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

str_replace also can take arrays to do replacements in one go

Copy link
Member Author

Choose a reason for hiding this comment

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

I thought of strtr because it takes an array of search => replace pairs as a single array, as opposed to the two separate arrays that str_replace accepts.

@Ayesh Ayesh force-pushed the run-test-improvements branch from 23ca93e to 59d0b60 Compare July 16, 2022 08:06
Ayesh and others added 6 commits July 21, 2022 22:03
…s with happy path optimizations

Simplifies and improves the readability of multiple `if`/`elseif`/`else` blocks by removing them when an earlier branch exists the execution flow by either returning or terminating the script.
…_exec()` calls

The rationale is that `shell_exec()` is identical to the backtick operator (both of which are disabled when `shell_exec` function is disabled) makes it very clear that it is a shell execution, and eases security audits too.
…tr` call

Makes the replacement easier to see, neatly aligned, and only takes one function call.
This is safe because none of the combined replacement values contain tokens that would be recursively replaced.

This also improves the readability on how the regular expressions in `EXPECTF` matcher is constructed.

Co-authored-by: Michael Voříšek <mvorisek@mvorisek.cz>
@Ayesh Ayesh force-pushed the run-test-improvements branch from 59d0b60 to 383d5ae Compare July 21, 2022 16:34
@Girgias Girgias merged commit 0490f08 into php:master Jul 22, 2022
@Ayesh
Copy link
Member Author

Ayesh commented Jul 22, 2022

Thank you.

@Ayesh Ayesh deleted the run-test-improvements branch July 22, 2022 15:14
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.

4 participants