Faster Span-based URL implementation#1844
Merged
jrflat merged 2 commits intoswiftlang:mainfrom Mar 26, 2026
Merged
Conversation
Contributor
Author
|
@swift-ci please test |
Span-based URL implementation
Contributor
Author
|
@swift-ci please test |
Contributor
Author
|
I'd also like to investigate changing |
parkera
approved these changes
Mar 26, 2026
This was referenced Mar 26, 2026
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.
Implement a new backing class for
URLfocused on performance and gated behind theFOUNDATION_SWIFT_URL_V2build flag.Motivation:
Many common URL operations such as path manipulation or resolution (currently) involve repeated string allocations, percent-decoding, and re-parsing. By working directly with
Span(s) of the underlying bytes and replacing just the encoded path during path manipulation (ensuring that nearby ranges and path-related flags are updated correctly), we see 5-8x speedup in use cases like appending or deleting path components.The new backing type also consolidates and shares code from the Swift
NSURL/CFURLimplementation, which improves parsing, validation, encoding, and resolution performance (2-3x speedup), and makes it easier to maintain consistent behavior across all the types.Modifications:
URL(URL.swift): WhenFOUNDATION_SWIFT_URL_V2is defined,struct URLuses the new_URLbacking class._URL(URL_Impl.swift): New backing class forstruct URLwhen the v2 build flag is enabled. Stores a_URLInfoand usesSpan<UInt8>in its methods where applicable to avoid intermediateStringallocations._URLInfo(URL_Info.swift): New storage struct holding the URL string, flags, and component ranges (of UTF8 bytes). Conforms to the existing_URLParseableand_URLHeaderprotocols (shared with__CFSmallURLImpland__CFBigURLImpl), so it plugs directly into the same parsing and resolution functions that SwiftCFURLuses. Providesparse(string:)andparse(filePath:)entry points, file path info construction, and areplacing(path:)method for efficient path-only mutations that update flags and shift query/fragment ranges without re-parsing.Span+Path.swift:
Span<UInt8>extensions for path and path component operations.URL_File.swift: Extracted
finalPathLength,parseFinalFileSystemRepresentation,parsePOSIXPath, andparseWindowsPathfromURL_C+File.swiftintoURLstatic methods to share between implementations.URL_Parsing.swift: Moved the free parsing, validation, and encoding functions into a
URLextension (indentation change). Removed#if FOUNDATION_FRAMEWORKguards to use these for_URLInfoon all platforms.URL_Resolution.swift: Removed
#if FOUNDATION_FRAMEWORKguards.URLEncoder.swift: Removed
#if FOUNDATION_FRAMEWORKguards.Result:
Ubuntu 24.04 `FOUNDATION_SWIFT_URL_V2` vs. Baseline Performance
When
FOUNDATION_SWIFT_URL_V2is enabled,URLoperations are significantly faster. The flag is not yet enabled, but has been tested. It can be enabled for swift-foundation in a separate PR after further cross-repo testing.When enabled,
URL,NSURL, andCFURLshare the same Swift code for parsing, percent-encoding, file path initialization, and resolution.Bug fixes/compatibility behaviors:
URLComponentsencoding to match originalCFURLbyte encoding behavior and the behavior of most other RFC 3986 and WHATWG parsers). Will resolve issues like Double encoding of query component #1190 when enabled.[and]unencoded in the path, query, and fragment via Allow square brackets in Swift CFURL path, query, and fragment #1802, matching WHATWG behavior.standardizednow preserves leading..segments in relative paths so they're not removed until resolution.%43:is not mistaken for a scheme).appendingPathExtensionrejects extensions containing invalid characters that would be problematic when decoded (e.g. spaces or Unicode directional override characters) instead of storing them percent-encoded.Testing:
FOUNDATION_SWIFT_URL_V2build flag (e.g.swift test -Xswiftc -DFOUNDATION_SWIFT_URL_V2).