1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
//===--- SortLargeExistentials.swift --------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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
//
//===----------------------------------------------------------------------===//
// This test is a variant of the SortLettersInPlace.
import TestsUtils
public let benchmarks = [
BenchmarkInfo(
name: "SortLargeExistentials",
runFunction: run_SortLargeExistentials,
tags: [.validation, .api, .algorithm, .cpubench],
legacyFactor: 100),
]
protocol LetterKind {
var value: String { get }
func lessthan(_ rhs: LetterKind) -> Bool
}
// A struct which exceeds the size of the existential inline buffer.
struct Letter : LetterKind {
let value: String
// Make this struct a large struct which does not fit into the 3-word
// existential inline buffer. Also provide an answer to ...
var a: Int = 42
var b: Int = 42
var c: Int = 42
var d: Int = 42
init(_ value: String) {
self.value = value
}
func lessthan(_ rhs: LetterKind) -> Bool {
return value < rhs.value
}
}
let lettersTemplate : [LetterKind] = [
Letter("k"), Letter("a"), Letter("x"), Letter("i"), Letter("f"), Letter("l"),
Letter("o"), Letter("w"), Letter("h"), Letter("p"), Letter("b"), Letter("u"),
Letter("n"), Letter("c"), Letter("j"), Letter("t"), Letter("y"), Letter("s"),
Letter("d"), Letter("v"), Letter("r"), Letter("e"), Letter("q"), Letter("m"),
Letter("z"), Letter("g"),
Letter("k"), Letter("a"), Letter("x"), Letter("i"), Letter("f"), Letter("l"),
Letter("o"), Letter("w"), Letter("h"), Letter("p"), Letter("b"), Letter("u"),
Letter("n"), Letter("c"), Letter("j"), Letter("t"), Letter("y"), Letter("s"),
Letter("d"), Letter("v"), Letter("r"), Letter("e"), Letter("q"), Letter("m"),
Letter("z"), Letter("g"),
Letter("k"), Letter("a"), Letter("x"), Letter("i"), Letter("f"), Letter("l"),
Letter("o"), Letter("w"), Letter("h"), Letter("p"), Letter("b"), Letter("u"),
Letter("n"), Letter("c"), Letter("j"), Letter("t"), Letter("y"), Letter("s"),
Letter("d"), Letter("v"), Letter("r"), Letter("e"), Letter("q"), Letter("m"),
Letter("z"), Letter("g"),
Letter("k"), Letter("a"), Letter("x"), Letter("i"), Letter("f"), Letter("l"),
Letter("o"), Letter("w"), Letter("h"), Letter("p"), Letter("b"), Letter("u"),
Letter("n"), Letter("c"), Letter("j"), Letter("t"), Letter("y"), Letter("s"),
Letter("d"), Letter("v"), Letter("r"), Letter("e"), Letter("q"), Letter("m"),
Letter("z"), Letter("g"),
Letter("k"), Letter("a"), Letter("x"), Letter("i"), Letter("f"), Letter("l"),
Letter("o"), Letter("w"), Letter("h"), Letter("p"), Letter("b"), Letter("u"),
Letter("n"), Letter("c"), Letter("j"), Letter("t"), Letter("y"), Letter("s"),
Letter("d"), Letter("v"), Letter("r"), Letter("e"), Letter("q"), Letter("m"),
Letter("z"), Letter("g")
]
@inline(never)
public func run_SortLargeExistentials(_ n: Int) {
for _ in 1...n {
var letters = lettersTemplate
letters.sort {
return $0.lessthan($1)
}
// Check whether letters are sorted.
check(letters[0].value <= letters[letters.count/2].value)
}
}
|