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

New KeyValuePairs view #175

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
286 changes: 286 additions & 0 deletions Benchmarks/Sources/WebURLBenchmark/KeyValuePairs.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
// Copyright The swift-url Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import Benchmark
import WebURL

#if swift(>=5.7)

/// Benchmarks the WebURL `KeyValuePairs` view.
///
let KeyValuePairs = BenchmarkSuite(name: "KeyValuePairs") { suite in

// Iteration.

suite.benchmark("Iteration.Small.Forwards") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&format=json&client=mobile")!
try state.measure {
for component in url.queryParams {
blackHole(component)
}
}
blackHole(url)
}

// Get (non-encoded).

suite.benchmark("Get.NonEncoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&format=json&client=mobile")!
try state.measure {
blackHole(url.queryParams["format"]!)
}
blackHole(url)
}

// Get2 (non-encoded).

suite.benchmark("Get2.NonEncoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&format=json&client=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client"])
}
blackHole(url)
}

// Get3 (non-encoded).

suite.benchmark("Get3.NonEncoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&format=json&client=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client", "baz"])
}
blackHole(url)
}

// Get4 (non-encoded).

suite.benchmark("Get4.NonEncoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&format=json&client=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client", "baz", "client"])
}
blackHole(url)
}

// Get (encoded).

suite.benchmark("Get.Encoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&form%61t=json&client=mobile")!
try state.measure {
blackHole(url.queryParams["format"]!)
}
blackHole(url)
}

// Get2 (encoded).

suite.benchmark("Get2.Encoded") { state in
var url = WebURL("http://example.com/?foo=bar&baz=qux&form%61t=json&cli%65nt=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client"])
}
blackHole(url)
}

// Get3 (encoded).

suite.benchmark("Get3.Encoded") { state in
var url = WebURL("http://example.com/?foo=bar&%62az=qux&form%61t=json&cli%65nt=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client", "baz"])
}
blackHole(url)
}

// Get4 (encoded).

suite.benchmark("Get4.Encoded") { state in
var url = WebURL("http://example.com/?foo=bar&%62az=qux&form%61t=json&cli%65nt=mobile")!
try state.measure {
blackHole(url.queryParams["format", "client", "baz", "client"])
}
blackHole(url)
}

// Get (long-keys)(non-encoded).

suite.benchmark("Get.LongKeys.NonEncoded") { state in
var url = WebURL("http://example.com/?foofoofoofoofoofoo=bar&bazbazbazbazbaz=qux&formatformatformatformat=json&clientclientclientclientclient=mobile")!
try state.measure {
blackHole(url.queryParams["formatformatformatformat"]!)
}
blackHole(url)
}

// Get2 (long-keys)(non-encoded).

suite.benchmark("Get2.LongKeys.NonEncoded") { state in
var url = WebURL("http://example.com/?foofoofoofoofoofoo=bar&bazbazbazbazbaz=qux&formatformatformatformat=json&clientclientclientclientclient=mobile")!
try state.measure {
blackHole(url.queryParams["formatformatformatformat", "clientclientclientclientclient"])
}
blackHole(url)
}

// Get (long-keys)(encoded).

suite.benchmark("Get.LongKeys.Encoded") { state in
var url = WebURL("http://example.com/?foofoofoofoofoofoo=bar&bazbazbazbazbaz=qux&form%61tform%61tform%61tform%61t=json&clientclientclientclientclient=mobile")!
try state.measure {
blackHole(url.queryParams["formatformatformatformat"]!)
}
blackHole(url)
}

// Get2 (long-keys)(encoded).

suite.benchmark("Get2.LongKeys.Encoded") { state in
var url = WebURL("http://example.com/?foofoofoofoofoofoo=bar&bazbazbazbazbaz=qux&form%61tform%61tform%61tform%61t=json&client%63lientclientclientclient=mobile")!
try state.measure {
blackHole(url.queryParams["formatformatformatformat", "clientclientclientclientclient"])
}
blackHole(url)
}

// Append One.

suite.benchmark("Append.One.Encoded") { state in
var url = WebURL("http://example.com/#f")!
try state.measure {
url.queryParams.append(key: "format", value: "🦆")
}
blackHole(url)
}

suite.benchmark("Append.One.NonEncoded") { state in
var url = WebURL("http://example.com/#f")!
try state.measure {
url.queryParams.append(key: "format", value: "json")
}
blackHole(url)
}

// Append Many.

suite.benchmark("Append.Many.Encoded") { state in
var url = WebURL("http://example.com/#f")!
try state.measure {
url.queryParams += [
("foo", "bar"),
("format", "🦆"),
("client", "mobile")
]
}
blackHole(url)
}

suite.benchmark("Append.Many.NonEncoded") { state in
var url = WebURL("http://example.com/#f")!
try state.measure {
url.queryParams += [
("foo", "bar"),
("format", "json"),
("client", "mobile")
]
}
blackHole(url)
}

// Remove All Where.

suite.benchmark("RemoveAllWhere") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple#f")!
try state.measure {
url.queryParams.removeAll(where: { $0.key.hasPrefix("f") })
}
blackHole(url)
}

suite.benchmark("RemoveAllWhere-2") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple#f")!
try state.measure {
url.queryParams.removeAll(where: { $0.key.hasPrefix("f") })
}
blackHole(url)
}

suite.benchmark("RemoveAllWhere-4") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple#f")!
try state.measure {
url.queryParams.removeAll(where: { $0.key.hasPrefix("f") })
}
blackHole(url)
}

suite.benchmark("RemoveAllWhere-8") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple&foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple#f")!
try state.measure {
url.queryParams.removeAll(where: { $0.key.hasPrefix("f") })
}
blackHole(url)
}


// Set.

suite.benchmark("Set.Single") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&fruit=apple#f")!
try state.measure {
url.queryParams.set(key: "format", to: "xml")
}
blackHole(url)
}

suite.benchmark("Set.Multiple") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&format=plist&format#f")!
try state.measure {
url.queryParams.set(key: "format", to: "xml")
}
blackHole(url)
}

suite.benchmark("Set.Append") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&format=plist&format#f")!
try state.measure {
url.queryParams.set(key: "new", to: "appended")
}
blackHole(url)
}

suite.benchmark("Set.Remove.Single") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&format=plist&format#f")!
try state.measure {
url.queryParams["cream"] = nil
}
blackHole(url)
}

suite.benchmark("Set.Remove.Multiple") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&format=plist&format#f")!
try state.measure {
url.queryParams["format"] = nil
}
blackHole(url)
}

suite.benchmark("Set.Remove.None") { state in
var url = WebURL("http://example.com/?foo=bar&client=mobile&format=json&cream=cheese&fish&chips&format=plist&format#f")!
try state.measure {
url.queryParams["doesNotExist"] = nil
}
blackHole(url)
}
}

#endif
28 changes: 20 additions & 8 deletions Benchmarks/Sources/WebURLBenchmark/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,23 @@ internal func blackHole<T>(_ x: T) {
// - Cannot-be-a-base URLs
// - file: URLs

Benchmark.main([
Constructor.HTTP,
ComponentSetters,
PathComponents,
PercentEncoding,
FoundationCompat.NSURLToWeb,
FoundationCompat.WebToNSURL,
])
#if swift(>=5.7)
Benchmark.main([
Constructor.HTTP,
ComponentSetters,
PathComponents,
PercentEncoding,
KeyValuePairs,
FoundationCompat.NSURLToWeb,
FoundationCompat.WebToNSURL,
])
#else
Benchmark.main([
Constructor.HTTP,
ComponentSetters,
PathComponents,
PercentEncoding,
FoundationCompat.NSURLToWeb,
FoundationCompat.WebToNSURL,
])
#endif
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ let package = Package(
.target(
name: "WebURL",
dependencies: ["IDNA"],
exclude: ["WebURL.docc"]
exclude: ["WebURL.docc", "WebURL+KeyValuePairs.swift"]
),
.target(
name: "WebURLTestSupport",
Expand All @@ -92,7 +92,8 @@ let package = Package(
),
.testTarget(
name: "WebURLTests",
dependencies: ["WebURL", "WebURLTestSupport", "Checkit"]
dependencies: ["WebURL", "WebURLTestSupport", "Checkit"],
exclude: ["KeyValuePairs"]
),
.testTarget(
name: "WebURLDeprecatedAPITests",
Expand Down
6 changes: 4 additions & 2 deletions [email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ let package = Package(

.target(
name: "WebURL",
dependencies: ["IDNA"]
dependencies: ["IDNA"],
exclude: ["WebURL+KeyValuePairs.swift"]
),
.target(
name: "WebURLTestSupport",
Expand All @@ -91,7 +92,8 @@ let package = Package(
),
.testTarget(
name: "WebURLTests",
dependencies: ["WebURL", "WebURLTestSupport", "Checkit"]
dependencies: ["WebURL", "WebURLTestSupport", "Checkit"],
exclude: ["KeyValuePairs"]
),
.testTarget(
name: "WebURLDeprecatedAPITests",
Expand Down
Loading