Skip to content

Improved zip API #18769

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

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 2 additions & 1 deletion benchmark/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ set(SWIFT_BENCH_MODULES
single-source/Hash
single-source/HashQuadratic
single-source/Histogram
single-source/InsertCharacter
single-source/InsertCharacter
single-source/Integrate
single-source/IterateData
single-source/Join
Expand Down Expand Up @@ -173,6 +173,7 @@ set(SWIFT_BENCH_MODULES
single-source/Walsh
single-source/WordCount
single-source/XorLoop
single-source/Zip
)

set(SWIFT_MULTISOURCE_SWIFT_BENCHES
Expand Down
295 changes: 295 additions & 0 deletions benchmark/single-source/Zip.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//

////////////////////////////////////////////////////////////////////////////////
// WARNING: This file is manually generated from .gyb template and should not
// be directly modified. Instead, make changes to Zip.swift.gyb and run
// scripts/generate_harness/generate_harness.py to regenerate this file.
////////////////////////////////////////////////////////////////////////////////

import TestsUtils

fileprivate let collectionCount = 4096
fileprivate let sumCount = collectionCount * (collectionCount - 1) / 2

public let Zip = [
BenchmarkInfo(name: "Zip2Iteration", runFunction: run_Zip2Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest2Iteration", runFunction: run_ZipLongest2Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "Zip3Iteration", runFunction: run_Zip3Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest3Iteration", runFunction: run_ZipLongest3Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "Zip4Iteration", runFunction: run_Zip4Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest4Iteration", runFunction: run_ZipLongest4Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "Zip5Iteration", runFunction: run_Zip5Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest5Iteration", runFunction: run_ZipLongest5Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "Zip6Iteration", runFunction: run_Zip6Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest6Iteration", runFunction: run_ZipLongest6Iteration, tags: [.validation, .api])
]

@inline(never)
public func run_Zip2Iteration (n: Int) {
let collection = zip(
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0)

for element in collection {
sums.0 += element.0
sums.1 += element.1
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
}
}

@inline(never)
public func run_ZipLongest2Iteration (n: Int) {
let collection = zipLongest(
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0)

for element in collection {
sums.0 += element.0!
sums.1 += element.1!
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
}
}

@inline(never)
public func run_Zip3Iteration (n: Int) {
let collection = zip(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0)

for element in collection {
sums.0 += element.0
sums.1 += element.1
sums.2 += element.2
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
}
}

@inline(never)
public func run_ZipLongest3Iteration (n: Int) {
let collection = zipLongest(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0)

for element in collection {
sums.0 += element.0!
sums.1 += element.1!
sums.2 += element.2!
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
}
}

@inline(never)
public func run_Zip4Iteration (n: Int) {
let collection = zip(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0)

for element in collection {
sums.0 += element.0
sums.1 += element.1
sums.2 += element.2
sums.3 += element.3
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
}
}

@inline(never)
public func run_ZipLongest4Iteration (n: Int) {
let collection = zipLongest(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0)

for element in collection {
sums.0 += element.0!
sums.1 += element.1!
sums.2 += element.2!
sums.3 += element.3!
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
}
}

@inline(never)
public func run_Zip5Iteration (n: Int) {
let collection = zip(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0, 0)

for element in collection {
sums.0 += element.0
sums.1 += element.1
sums.2 += element.2
sums.3 += element.3
sums.4 += element.4
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
CheckResults(sums.4 == sumCount)
}
}

@inline(never)
public func run_ZipLongest5Iteration (n: Int) {
let collection = zipLongest(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0, 0)

for element in collection {
sums.0 += element.0!
sums.1 += element.1!
sums.2 += element.2!
sums.3 += element.3!
sums.4 += element.4!
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
CheckResults(sums.4 == sumCount)
}
}

@inline(never)
public func run_Zip6Iteration (n: Int) {
let collection = zip(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0, 0, 0)

for element in collection {
sums.0 += element.0
sums.1 += element.1
sums.2 += element.2
sums.3 += element.3
sums.4 += element.4
sums.5 += element.5
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
CheckResults(sums.4 == sumCount)
CheckResults(sums.5 == sumCount)
}
}

@inline(never)
public func run_ZipLongest6Iteration (n: Int) {
let collection = zipLongest(
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount,
0..<collectionCount
)

for _ in 1...n {
var sums = (0, 0, 0, 0, 0, 0)

for element in collection {
sums.0 += element.0!
sums.1 += element.1!
sums.2 += element.2!
sums.3 += element.3!
sums.4 += element.4!
sums.5 += element.5!
}

CheckResults(sums.0 == sumCount)
CheckResults(sums.1 == sumCount)
CheckResults(sums.2 == sumCount)
CheckResults(sums.3 == sumCount)
CheckResults(sums.4 == sumCount)
CheckResults(sums.5 == sumCount)
}
}
78 changes: 78 additions & 0 deletions benchmark/single-source/Zip.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 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
//
//===----------------------------------------------------------------------===//

% # Ignore the following warning. This _is_ the correct file to edit.
////////////////////////////////////////////////////////////////////////////////
// WARNING: This file is manually generated from .gyb template and should not
// be directly modified. Instead, make changes to Zip.swift.gyb and run
// scripts/generate_harness/generate_harness.py to regenerate this file.
////////////////////////////////////////////////////////////////////////////////

import TestsUtils

fileprivate let collectionCount = 4096
fileprivate let sumCount = collectionCount * (collectionCount - 1) / 2

%{
upper_arity_bound = 6
arities = range(2, upper_arity_bound + 1)
}%
public let Zip = [
% for arity in arities:
BenchmarkInfo(name: "Zip${ arity }Iteration", runFunction: run_Zip${ arity }Iteration, tags: [.validation, .api]),
BenchmarkInfo(name: "ZipLongest${ arity }Iteration", runFunction: run_ZipLongest${ arity }Iteration, tags: [.validation, .api])${ "," if arity < upper_arity_bound else "" }
% end
]

% for arity in arities:
@inline(never)
public func run_Zip${ arity }Iteration (n: Int) {
let collection = zip(
${ ",\n".join([" 0..<collectionCount"] * arity) }
)

for _ in 1...n {
var sums = (${ ", ".join(["0"] * arity) })

for element in collection {
% for element in range(0, arity):
sums.${ element } += element.${ element }
% end
}

% for element in range(0, arity):
CheckResults(sums.${ element } == sumCount)
% end
}
}

@inline(never)
public func run_ZipLongest${ arity }Iteration (n: Int) {
let collection = zipLongest(
${ ",\n".join([" 0..<collectionCount"] * arity) }
)

for _ in 1...n {
var sums = (${ ", ".join(["0"] * arity) })

for element in collection {
% for element in range(0, arity):
sums.${ element } += element.${ element }!
% end
}

% for element in range(0, arity):
CheckResults(sums.${ element } == sumCount)
% end
}
}${ "\n" if arity < upper_arity_bound else "" }
% end
Loading