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

perf: remove heap allocation in parse_host #1021

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

dsherret
Copy link
Contributor

@dsherret dsherret commented Feb 6, 2025

Before:

test long           ... bench:         302 ns/iter (+/- 42) = 142 MB/s

After:

test long           ... bench:         271 ns/iter (+/- 7) = 158 MB/s

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

Attention: Patch coverage is 80.00000% with 12 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@79ff014). Learn more about missing BASE report.

Files with missing lines Patch % Lines
url/src/host.rs 80.95% 4 Missing ⚠️
url/src/parser.rs 77.77% 4 Missing ⚠️
idna/src/uts46.rs 70.00% 3 Missing ⚠️
url/src/lib.rs 75.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1021   +/-   ##
=======================================
  Coverage        ?   80.25%           
=======================================
  Files           ?       24           
  Lines           ?     4299           
  Branches        ?        0           
=======================================
  Hits            ?     3450           
  Misses          ?      849           
  Partials        ?        0           

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

@dsherret dsherret marked this pull request as ready for review February 6, 2025 18:43
@@ -80,15 +79,34 @@ impl Host<String> {
///
/// <https://url.spec.whatwg.org/#host-parsing>
pub fn parse(input: &str) -> Result<Self, ParseError> {
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 didn't add, change, or remove from the public API, but please verify this as well in case I missed something.

url/src/host.rs Outdated
Comment on lines 181 to 182
// SAFETY: If borrowed, then the original string is ascii and we can return the
// original Cow in order to save an allocation
Copy link
Collaborator

Choose a reason for hiding this comment

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

My reading of the code suggests this is correct right now, but:

There is no guarantee that idna::domain_to_ascii_cow could not return a Cow::Borrowed(x) where x.len() < domain.len() in the future (where this code would break or possibly be unsafe). To avoid that refactoring hazard, we should hoist this up into idna.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hoisted. Not to sure about the new function name.

@dsherret dsherret requested a review from lucacasonato February 7, 2025 15:24
/// and using Punycode as necessary.
///
/// This process may fail.
pub fn domain_to_ascii_from_cow(
Copy link
Collaborator

Choose a reason for hiding this comment

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

@hsivonen Please check if you find this new public API of idna to be acceptable.

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.

2 participants