Skip to content
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

fix: improve isRelative performance #20095

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.50 under development
------------------------

- Bug #17181: Improved `BaseUrl::isRelative($url)` performance (sammousa, bizley, rob006)
- Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3)
- Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3)
- Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw)
Expand Down
3 changes: 1 addition & 2 deletions framework/helpers/BaseUrl.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,7 @@
*/
public static function isRelative($url)
{
$urlComponents = parse_url($url, PHP_URL_SCHEME);
return strncmp($url, '//', 2) && empty($urlComponents);
return preg_match('~^[[:alpha:]][[:alnum:]+-.]*://|^//~', $url) === 0;

Check warning on line 381 in framework/helpers/BaseUrl.php

View check run for this annotation

Codecov / codecov/patch

framework/helpers/BaseUrl.php#L381

Added line #L381 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

Can we get away here with just using \w with additional +-. ? It adds underscore to the list but should not be problematic I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree it wouldn't be problematic, but why make it less correct? What's the benefit?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, not sure why but in my head this feels faster? 🤣 strange how the mind works, hehe. Ok, @rob006 could you take a look?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we should choose pattern that is faster - IMO it is not worth to have more strict check here to cover edge cases like 123://example.com, if it will be slower for basically all real-world use cases.

Also, IMO it should be '~^[[:alpha:]][[:alnum:]+-.]+://|^//~', to not accept URLs like ://example.com

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Both your examples are already not accepted. The first character has to be alpha, then 0 or more alphanumerics (and a few special chars).

Have you got data to show that this regex is slow? It is simple and has an anchor at the start, I don't see why it would be slow. I'm skeptical any change would be speeding it up immensely, since most time is spent in compilation not evaluation of the expression.

}

/**
Expand Down
Loading