Skip to content

Commit 4f3753a

Browse files
committed
Add support for GYB in benchmarks
1 parent 4f52f84 commit 4f3753a

File tree

10 files changed

+663
-87
lines changed

10 files changed

+663
-87
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ set(SWIFT_BENCH_MODULES
7979
single-source/PolymorphicCalls
8080
single-source/PopFront
8181
single-source/PopFrontGeneric
82+
single-source/Prefix
8283
single-source/Prims
8384
single-source/ProtocolDispatch
8485
single-source/ProtocolDispatch2

benchmark/scripts/generate_harness/generate_harness.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import glob
2020
import os
2121
import re
22+
import subprocess
2223

2324
import jinja2
2425

@@ -38,6 +39,13 @@
3839
lstrip_blocks=True)
3940

4041
if __name__ == '__main__':
42+
# Generate Your Boilerplate single-source
43+
gyb_files = glob.glob(os.path.join(single_source_dir, '*.swift.gyb'))
44+
gyb_script = os.path.realpath(os.path.join(perf_dir, '../utils/gyb'))
45+
for f in gyb_files:
46+
print(f[:-4])
47+
subprocess.call([gyb_script, '--line-directive', '', '-o', f[:-4], f])
48+
4149
# CMakeList single-source
4250
test_files = glob.glob(os.path.join(single_source_dir, '*.swift'))
4351
tests = sorted(os.path.basename(x).split('.')[0] for x in test_files)

benchmark/single-source/DropLast.swift

Lines changed: 128 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,77 +10,161 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
////////////////////////////////////////////////////////////////////////////////
14+
// WARNING: This file is manually generated from .gyb template and should not
15+
// be directly modified. Instead, make changes to Suffix.swift.gyb and run
16+
// scripts/generate_harness/generate_harness.py to regenerate this file.
17+
////////////////////////////////////////////////////////////////////////////////
18+
1319
import TestsUtils
1420

15-
let reps = 1
1621
let sequenceCount = 4096
1722
let prefixCount = 1024
1823
let dropCount = sequenceCount - prefixCount
19-
let sumCount = prefixCount * (prefixCount-1) / 2
24+
let sumCount = prefixCount * (prefixCount - 1) / 2
25+
2026

2127
@inline(never)
2228
public func run_DropLastCountableRange(_ N: Int) {
23-
let s = 0 ..< sequenceCount
29+
let s = 0..<sequenceCount
2430
for _ in 1...20*N {
25-
for _ in 1...reps {
26-
var result = 0
27-
for element in s.dropLast(dropCount) {
28-
result += element
29-
}
30-
CheckResults(result == sumCount,
31-
"IncorrectResults in DropLastCountableRange: \(result) != \(sumCount)")
31+
var result = 0
32+
for element in s.dropLast(dropCount) {
33+
result += element
3234
}
35+
CheckResults(result == sumCount,
36+
"IncorrectResults in DropLastCountableRange: \(result) != \(sumCount)")
3337
}
3438
}
35-
36-
fileprivate struct MySequence: Sequence {
37-
let range: CountableRange<Int>
38-
public func makeIterator() -> IndexingIterator<CountableRange<Int>> {
39-
return range.makeIterator()
40-
}
41-
}
42-
4339
@inline(never)
4440
public func run_DropLastSequence(_ N: Int) {
45-
let s = MySequence(range: 0 ..< sequenceCount)
41+
let s = sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }
4642
for _ in 1...20*N {
47-
for _ in 1...reps {
48-
var result = 0
49-
for element in s.dropLast(dropCount) {
50-
result += element
51-
}
52-
CheckResults(result == sumCount,
53-
"IncorrectResults in DropLastSequence: \(result) != \(sumCount)")
43+
var result = 0
44+
for element in s.dropLast(dropCount) {
45+
result += element
5446
}
47+
CheckResults(result == sumCount,
48+
"IncorrectResults in DropLastSequence: \(result) != \(sumCount)")
5549
}
5650
}
57-
5851
@inline(never)
5952
public func run_DropLastAnySequence(_ N: Int) {
60-
let s = AnySequence(0 ..< sequenceCount)
53+
let s = AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })
6154
for _ in 1...20*N {
62-
for _ in 1...reps {
63-
var result = 0
64-
for element in s.dropLast(dropCount) {
65-
result += element
66-
}
67-
CheckResults(result == sumCount,
68-
"IncorrectResults in DropLastAnySequence: \(result) != \(sumCount)")
55+
var result = 0
56+
for element in s.dropLast(dropCount) {
57+
result += element
6958
}
59+
CheckResults(result == sumCount,
60+
"IncorrectResults in DropLastAnySequence: \(result) != \(sumCount)")
61+
}
62+
}
63+
@inline(never)
64+
public func run_DropLastAnySeqCntRange(_ N: Int) {
65+
let s = AnySequence(0..<sequenceCount)
66+
for _ in 1...20*N {
67+
var result = 0
68+
for element in s.dropLast(dropCount) {
69+
result += element
70+
}
71+
CheckResults(result == sumCount,
72+
"IncorrectResults in DropLastAnySeqCntRange: \(result) != \(sumCount)")
73+
}
74+
}
75+
@inline(never)
76+
public func run_DropLastAnyCollection(_ N: Int) {
77+
let s = AnyCollection(0..<sequenceCount)
78+
for _ in 1...20*N {
79+
var result = 0
80+
for element in s.dropLast(dropCount) {
81+
result += element
82+
}
83+
CheckResults(result == sumCount,
84+
"IncorrectResults in DropLastAnyCollection: \(result) != \(sumCount)")
7085
}
7186
}
72-
7387
@inline(never)
7488
public func run_DropLastArray(_ N: Int) {
75-
let s = Array(0 ..< sequenceCount)
89+
let s = Array(0..<sequenceCount)
90+
for _ in 1...20*N {
91+
var result = 0
92+
for element in s.dropLast(dropCount) {
93+
result += element
94+
}
95+
CheckResults(result == sumCount,
96+
"IncorrectResults in DropLastArray: \(result) != \(sumCount)")
97+
}
98+
}
99+
@inline(never)
100+
public func run_DropLastCountableRangeLazy(_ N: Int) {
101+
let s = (0..<sequenceCount).lazy
102+
for _ in 1...20*N {
103+
var result = 0
104+
for element in s.dropLast(dropCount) {
105+
result += element
106+
}
107+
CheckResults(result == sumCount,
108+
"IncorrectResults in DropLastCountableRangeLazy: \(result) != \(sumCount)")
109+
}
110+
}
111+
@inline(never)
112+
public func run_DropLastSequenceLazy(_ N: Int) {
113+
let s = (sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }).lazy
114+
for _ in 1...20*N {
115+
var result = 0
116+
for element in s.dropLast(dropCount) {
117+
result += element
118+
}
119+
CheckResults(result == sumCount,
120+
"IncorrectResults in DropLastSequenceLazy: \(result) != \(sumCount)")
121+
}
122+
}
123+
@inline(never)
124+
public func run_DropLastAnySequenceLazy(_ N: Int) {
125+
let s = (AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })).lazy
126+
for _ in 1...20*N {
127+
var result = 0
128+
for element in s.dropLast(dropCount) {
129+
result += element
130+
}
131+
CheckResults(result == sumCount,
132+
"IncorrectResults in DropLastAnySequenceLazy: \(result) != \(sumCount)")
133+
}
134+
}
135+
@inline(never)
136+
public func run_DropLastAnySeqCntRangeLazy(_ N: Int) {
137+
let s = (AnySequence(0..<sequenceCount)).lazy
138+
for _ in 1...20*N {
139+
var result = 0
140+
for element in s.dropLast(dropCount) {
141+
result += element
142+
}
143+
CheckResults(result == sumCount,
144+
"IncorrectResults in DropLastAnySeqCntRangeLazy: \(result) != \(sumCount)")
145+
}
146+
}
147+
@inline(never)
148+
public func run_DropLastAnyCollectionLazy(_ N: Int) {
149+
let s = (AnyCollection(0..<sequenceCount)).lazy
150+
for _ in 1...20*N {
151+
var result = 0
152+
for element in s.dropLast(dropCount) {
153+
result += element
154+
}
155+
CheckResults(result == sumCount,
156+
"IncorrectResults in DropLastAnyCollectionLazy: \(result) != \(sumCount)")
157+
}
158+
}
159+
@inline(never)
160+
public func run_DropLastArrayLazy(_ N: Int) {
161+
let s = (Array(0..<sequenceCount)).lazy
76162
for _ in 1...20*N {
77-
for _ in 1...reps {
78-
var result = 0
79-
for element in s.dropLast(dropCount) {
80-
result += element
81-
}
82-
CheckResults(result == sumCount,
83-
"IncorrectResults in DropLastArray: \(result) != \(sumCount)")
163+
var result = 0
164+
for element in s.dropLast(dropCount) {
165+
result += element
84166
}
167+
CheckResults(result == sumCount,
168+
"IncorrectResults in DropLastArrayLazy: \(result) != \(sumCount)")
85169
}
86170
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- DropLast.swift ---------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
% # Ignore the following warning. This _is_ the correct file to edit.
14+
////////////////////////////////////////////////////////////////////////////////
15+
// WARNING: This file is manually generated from .gyb template and should not
16+
// be directly modified. Instead, make changes to Suffix.swift.gyb and run
17+
// scripts/generate_harness/generate_harness.py to regenerate this file.
18+
////////////////////////////////////////////////////////////////////////////////
19+
20+
import TestsUtils
21+
22+
let sequenceCount = 4096
23+
let prefixCount = 1024
24+
let dropCount = sequenceCount - prefixCount
25+
let sumCount = prefixCount * (prefixCount - 1) / 2
26+
27+
%{
28+
# Name and Expression pairs for Sequences to test.
29+
30+
Sequences = [
31+
('CountableRange', '0..<sequenceCount'),
32+
('Sequence',
33+
'sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil }'),
34+
('AnySequence',
35+
'AnySequence(sequence(first: 0) { $0 < sequenceCount - 1 ? $0 &+ 1 : nil })'),
36+
('AnySeqCntRange', 'AnySequence(0..<sequenceCount)'),
37+
('AnyCollection', 'AnyCollection(0..<sequenceCount)'),
38+
('Array', 'Array(0..<sequenceCount)'),
39+
]
40+
def lazy ((Name, Expr)) : return (Name + 'Lazy', '(' + Expr + ').lazy')
41+
42+
Sequences = Sequences + map(lazy, Sequences)
43+
}%
44+
45+
% for (Name, Expr) in Sequences:
46+
@inline(never)
47+
public func run_DropLast${Name}(_ N: Int) {
48+
let s = ${Expr}
49+
for _ in 1...20*N {
50+
var result = 0
51+
for element in s.dropLast(dropCount) {
52+
result += element
53+
}
54+
CheckResults(result == sumCount,
55+
"IncorrectResults in DropLast${Name}: \(result) != \(sumCount)")
56+
}
57+
}
58+
% end

0 commit comments

Comments
 (0)