Repository navigation
Do you want a change that makes URL parsing approximately 2 times faster? #1149
Replies: 1 comment
|
duplicate |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Hello. I looked at the speed of
httpx2.URL(...), and I found that one line uses a large part of the parse time. I made a change for it, but I would like to know if you want this change before it goes further.The contributing guide asks for an "Ideas" discussion first. This repository has only the "Announcements" and "Q&A" categories, so I write here. Please move this if there is a better place.
What I found
urlparsemust reject the ASCII control characters that a URL cannot contain. To find them, it examines each character of the URL with a generator in Python:A profile of
httpx2.URL(...)shows that this generator uses approximately 43 percent of the parse time. The code also reads the string three times when the URL contains an incorrect character.urlparsedoes the same test for each keyword component.What I suggest
The ASCII characters that are not printable are exactly the C0 control characters and DEL. One compiled regex search can replace the generator. The match object gives the character and also its position. The two additional passes are not necessary.
Two smaller changes to
quotegive the remainder of the increase.urlparsecalls this function five times for each URL.PERCENT_ENCODED_REGEX.finditer(...)and notre.finditer(PERCENT_ENCODED_REGEX, ...). The second form repeats there._compilecache operation at each call.percent_encodedimmediately if the string contains no%character. Most strings do not contain this character.The public API does not change. The error messages and the character positions do not change.
Measurements
The measurements use 40 rounds. Each variant runs in its own process. The order of the two variants changes at each round. The values are medians:
The two sets of samples do not overlap. In each case, the slowest of the 40 new samples is faster than the fastest of the 40 old samples. The increase is larger for a long URL, because the previous code reads each character in Python.
Is the change safe
The change is correct only if
[\x00-\x1f\x7f]matches the same characters as the previous condition. Three tests show this:The coverage stays at 100 percent.
My question
Pull request #1139 has the change, the tests and the benchmarks. I did not open a discussion first, and I am sorry about that.
Do you want this change? I am happy to make it smaller, to divide it into parts, or to close it.
All reactions