Skip to content

Add SortArrayInClass benchmark. #26663

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 1 commit into from
Aug 23, 2019
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 @@ -151,6 +151,7 @@ set(SWIFT_BENCH_MODULES
single-source/SetTests
single-source/SevenBoom
single-source/Sim2DArray
single-source/SortArrayInClass
single-source/SortIntPyramids
single-source/SortLargeExistentials
single-source/SortLettersInPlace
Expand Down
85 changes: 85 additions & 0 deletions benchmark/single-source/SortArrayInClass.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//===--- SortArrayInClass.swift -------------------------------------------===//
//
// 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
//
// This benchmark is derived from user code that encoutered a major
// performance problem in normal usage. Contributed by Saleem
// Abdulrasool (compnerd).
//
//===----------------------------------------------------------------------===//

import TestsUtils

// Specifically tests efficient access to Array subscript when the
// array is a class property, but also generally tests quicksort in a
// class which needs a slew of array optimizations, uniqueness, bounds
// and exclusivity optimizations.
public let SortArrayInClass = [
BenchmarkInfo(
name: "SortArrayInClass",
runFunction: run_SortArrayInClass,
tags: [.abstraction, .safetychecks, .exclusivity, .algorithm, .api, .Array])
]

let LARGE_ARRAY_SIZE = 10000

class Sorter {
var array: [Int]
init(size: Int) {
array = Array((0..<size).reversed())
}

private func _swap(i: Int, j: Int) {
let t = array[i]
// This currently copies the entire array. Assigning to a
// temporary, or using swapAt would avoid the copy, but users
// shouldn't need to know that.
array[i] = array[j]
array[j] = t
}

private func _quicksort(left: Int, right: Int) {

if left < right {
let pivot = array[left + ((right - left) / 2)]
var left_new = left
var right_new = right

repeat {
while array[left_new] < pivot {
left_new += 1
}
while pivot < array[right_new] {
right_new -= 1
}
if left_new <= right_new {
_swap(i:left_new, j:right_new)
left_new += 1
right_new -= 1
}
} while left_new <= right_new

_quicksort(left: left, right: right_new)
_quicksort(left: left_new, right:right)
}
}

func quicksort() {
_quicksort(left:0, right:array.count - 1);
}
}

public func run_SortArrayInClass(_ N: Int) {
for _ in 1...N {
// The array needs to be reinitialized before each sort, so it
// can't be a setup/tearDown function.
let sorter = Sorter(size:LARGE_ARRAY_SIZE)
sorter.quicksort()
}
}
1 change: 1 addition & 0 deletions benchmark/utils/TestsUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public enum BenchmarkCategory : String {
case runtime, refcount, metadata
// Other general areas of compiled code validation.
case abstraction, safetychecks, exceptions, bridging, concurrency, existential
case exclusivity

// Algorithms are "micro" that test some well-known algorithm in isolation:
// sorting, searching, hashing, fibonaci, crypto, etc.
Expand Down
2 changes: 2 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ import SequenceAlgos
import SetTests
import SevenBoom
import Sim2DArray
import SortArrayInClass
import SortIntPyramids
import SortLargeExistentials
import SortLettersInPlace
Expand Down Expand Up @@ -325,6 +326,7 @@ registerBenchmark(SequenceAlgos)
registerBenchmark(SetTests)
registerBenchmark(SevenBoom)
registerBenchmark(Sim2DArray)
registerBenchmark(SortArrayInClass)
registerBenchmark(SortIntPyramids)
registerBenchmark(SortLargeExistentials)
registerBenchmark(SortLettersInPlace)
Expand Down