Skip to content

Reduce base-path ".." removal in merge_path to a linear pass - #242

Merged
hsbt merged 1 commit into
masterfrom
claude/sweet-shtern-562d78
Aug 5, 2026
Merged

Reduce base-path ".." removal in merge_path to a linear pass#242
hsbt merged 1 commit into
masterfrom
claude/sweet-shtern-562d78

Conversation

@hsbt

@hsbt hsbt commented Aug 5, 2026

Copy link
Copy Markdown
Member

URI.join and URI#merge degrade quadratically as the base path grows. Joining against a base built from repeated a/../ takes several seconds once it reaches a few hundred thousand segments, because the RFC2396 Section 5.2 6a step scans with Array#index and deletes with slice! inside a loop.

I replaced that loop with a single left-to-right pass over the base path, matching the linear stack already used for the relative path. The output is unchanged for every input, including the case where a leading .. discards the whole base path rather than being kept as the relative side would.

I verified this by diffing the new pass against the old implementation over exhaustive segment combinations and hundreds of thousands of random deep inputs, both on merge_path directly and end to end through URI.join. Every result was identical.

The RFC2396 5.2 6a step used Array#index/slice! in a loop, making
URI.join / URI#merge O(n^2) in the number of base-path segments, so a
640KB base of repeated "a/../" took several seconds. Rewrite it as a
single left-to-right pass while preserving the exact previous output,
including the leading-".." case where the whole base path is dropped.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@hsbt
hsbt merged commit e09241e into master Aug 5, 2026
52 checks passed
@hsbt
hsbt deleted the claude/sweet-shtern-562d78 branch August 5, 2026 01:23
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.

1 participant