fix: remove repeated default ports from a URL host in linear time - #123
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #123 +/- ##
==========================================
+ Coverage 97.97% 99.07% +1.09%
==========================================
Files 2 2
Lines 99 108 +9
==========================================
+ Hits 97 107 +10
+ Misses 2 1 -1 ☔ View full report in Codecov by Harness. |
removeDefaultPort stripped one ":80" (or ":443") at a time and re-parsed the whole remaining host after each strip. With lenient colon parsing (Go 1.25, or GODEBUG urlstrictcolons=0 since Go 1.26), url.Parse accepts a host such as ":80:80:...:80", so a single $ref of a few hundred KB kept jsonreference.New busy for tens of seconds: the cost was quadratic in the length of the host. The trailing repetitions are now counted in one scan and stripped at once. The shortened host is parsed at most twice: if it no longer parses, a single repetition stays, which is where the one-by-one removal stopped. Every shortened host but the last still ends with the default port, which url.Parse already accepted on the original host, so the result is the same as before and normalizing stays idempotent. With the default strict colon parsing of Go 1.26 such hosts are rejected by url.Parse before normalization, so v1.0.2 and later are only exposed when an application opts out with urlstrictcolons=0. v1.0.1 (go 1.25.0) is exposed by default when built with Go 1.25. FuzzRemoveDefaultPort checks the new implementation against the former one, and TestRemoveDefaultPortLinear pins the cost on 100 000 repetitions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
fredbi
force-pushed
the
fix/quadratic-stripping
branch
from
September 25, 2026 20:25
d84edb8 to
85b93ab
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
removeDefaultPort stripped one ":80" (or ":443") at a time and re-parsed the whole remaining host after each strip. With lenient colon parsing (Go 1.25, or GODEBUG urlstrictcolons=0 since Go 1.26), url.Parse accepts a host such as ":80:80:...:80", so a single $ref of a few hundred KB kept jsonreference.New busy for tens of seconds: the cost was quadratic in the length of the host.
The trailing repetitions are now counted in one scan and stripped at once. The shortened host is parsed at most twice: if it no longer parses, a single repetition stays, which is where the one-by-one removal stopped. Every shortened host but the last still ends with the default port, which url.Parse already accepted on the original host, so the result is the same as before and normalizing stays idempotent.
With the default strict colon parsing of Go 1.26 such hosts are rejected by url.Parse before normalization, so v1.0.2 and later are only exposed when an application opts out with urlstrictcolons=0. v1.0.1 (go 1.25.0) is exposed by default when built with Go 1.25.
FuzzRemoveDefaultPort checks the new implementation against the former one, and TestRemoveDefaultPortLinear pins the cost on 100 000 repetitions.
Change type
Please select: 🆕 New feature or enhancement|🔧 Bug fix'|📃 Documentation update
Short description
Fixes
Full description
Checklist