Skip to content

FlattenSequence/distance(from:to:) benchmarks. #71786

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

Merged
Merged
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
1 change: 1 addition & 0 deletions benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ set(SWIFT_BENCH_MODULES
single-source/ExistentialPerformance
single-source/Fibonacci
single-source/FindStringNaive
single-source/FlattenDistanceFromTo
single-source/FlattenList
single-source/FloatingPointConversion
single-source/FloatingPointParsing
Expand Down
117 changes: 117 additions & 0 deletions benchmark/single-source/FlattenDistanceFromTo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//===--- FlattenDistanceFromTo.swift --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2024 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import TestsUtils

public let benchmarks = [
BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.Array.04.04",
runFunction: { with(arrayArray04x04, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayArray04x04) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.Array.04x08",
runFunction: { with(arrayArray04x08, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayArray04x08) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.Array.08.04",
runFunction: { with(arrayArray08x04, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayArray08x04) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.Array.08.08",
runFunction: { with(arrayArray08x08, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayArray08x08) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.String.04.04",
runFunction: { with(arrayString04x04, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayString04x04) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.String.04.08",
runFunction: { with(arrayString04x08, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayString04x08) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.String.08.04",
runFunction: { with(arrayString08x04, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayString08x04) }),

BenchmarkInfo(
name: "FlattenDistanceFromTo.Array.String.08.08",
runFunction: { with(arrayString08x08, $0) },
tags: [.validation, .api],
setUpFunction: { blackHole(arrayString08x08) }),
]

// MARK: - Array Array

func makeArrayArray(_ outer: Int, _ inner: Int) -> FlattenSequence<[[UInt8]]> {
Array(repeating: Array(repeating: 123, count: inner), count: outer).joined()
}

let arrayArray04x04 = makeArrayArray(04, 04)
let arrayArray04x08 = makeArrayArray(04, 08)
let arrayArray08x04 = makeArrayArray(08, 04)
let arrayArray08x08 = makeArrayArray(08, 08)

@inline(never)
public func with(_ collection: FlattenSequence<[[UInt8]]>, _ iterations: Int) {
var value = 0 as Int

for _ in 0 ..< iterations {
for a in collection.indices {
for b in collection.indices {
value &+= collection.distance(from: a, to: b)
value &+= collection.distance(from: b, to: a)
}
}
}

blackHole(value == 0)
}

// MARK: - Array String

func makeArrayString(_ outer: Int, _ inner: Int) -> FlattenSequence<[String]> {
Array(repeating: String(repeating: "0", count: inner), count: outer).joined()
}

let arrayString04x04 = makeArrayString(04, 04)
let arrayString04x08 = makeArrayString(04, 08)
let arrayString08x04 = makeArrayString(08, 04)
let arrayString08x08 = makeArrayString(08, 08)

@inline(never)
public func with(_ collection: FlattenSequence<[String]>, _ iterations: Int) {
var value = 0 as Int

for _ in 0 ..< iterations {
for a in collection.indices {
for b in collection.indices {
value &+= collection.distance(from: a, to: b)
value &+= collection.distance(from: b, to: a)
}
}
}

blackHole(value == 0)
}
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ import Exclusivity
import ExistentialPerformance
import Fibonacci
import FindStringNaive
import FlattenDistanceFromTo
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add register(FlattenDistanceFromTo.benchmarks) at line 286 below.
With the PR as-is the benchmark runner still does not know about this addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The script could use some love too:

The script will automatically:

  1. Edit main.swift by importing and registering* your new Swift module.

*The cake auto-registration is a lie.

import FlattenList
import FloatingPointConversion
import FloatingPointParsing
Expand Down Expand Up @@ -282,6 +283,7 @@ register(Exclusivity.benchmarks)
register(ExistentialPerformance.benchmarks)
register(Fibonacci.benchmarks)
register(FindStringNaive.benchmarks)
register(FlattenDistanceFromTo.benchmarks)
register(FlattenList.benchmarks)
register(FloatingPointConversion.benchmarks)
register(FloatingPointParsing.benchmarks)
Expand Down