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

Conversation

SamMousa
Copy link
Contributor

@SamMousa SamMousa commented Jan 3, 2024

Q A
Is bugfix? ✔️
New feature?
Breaks BC?
Fixed issues #17191 #20077

Copy link

what-the-diff bot commented Jan 3, 2024

PR Summary

  • Improvement in the changelog documentation
    A new entry has been added to the log files to document a bug fix related to the mechanism used to determine whether a URL is relative or not.

  • Performance enhancement in URL classification
    The way we classify URLs as being relative or not has been altered. This change deploys a new format, using 'regular expressions', that helps speed up the overall performance of this task. In simpler words, we've made it quicker and more efficient for the system to understand and classify different kinds of web addresses.

Copy link

codecov bot commented Jan 3, 2024

Codecov Report

Attention: 1 lines in your changes are missing coverage. Please review.

Comparison is base (dea891e) 48.02% compared to head (c5a7e52) 18.72%.

Files Patch % Lines
framework/helpers/BaseUrl.php 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master   #20095       +/-   ##
===========================================
- Coverage   48.02%   18.72%   -29.30%     
===========================================
  Files         445      445               
  Lines       43890    43664      -226     
===========================================
- Hits        21078     8176    -12902     
- Misses      22812    35488    +12676     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -378,8 +378,7 @@ public static function home($scheme = false)
*/
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;
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.

@samdark samdark added this to the 2.0.50 milestone Jan 3, 2024
@samdark samdark merged commit b46e267 into yiisoft:master Jan 6, 2024
65 of 67 checks passed
@samdark
Copy link
Member

samdark commented Jan 6, 2024

Thank you!

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.

5 participants