Skip to content

[benchmark] Add Queue benchmark that tests popLast generically and concretely #14393

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
Feb 3, 2018
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 @@ -113,6 +113,7 @@ set(SWIFT_BENCH_MODULES
single-source/Prims
single-source/ProtocolDispatch
single-source/ProtocolDispatch2
single-source/Queue
single-source/RC4
single-source/RGBHistogram
single-source/RangeAssignment
Expand Down
132 changes: 132 additions & 0 deletions benchmark/single-source/Queue.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//===--- RangeAssignment.swift --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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 QueueGeneric = BenchmarkInfo(
name: "QueueGeneric",
runFunction: run_QueueGeneric,
tags: [.validation, .api],
setUpFunction: { buildWorkload() },
tearDownFunction: nil)

public let QueueConcrete = BenchmarkInfo(
name: "QueueConcrete",
runFunction: run_QueueConcrete,
tags: [.validation, .api],
setUpFunction: { buildWorkload() },
tearDownFunction: nil)

// TODO: remove when there is a native equivalent in the std lib
extension RangeReplaceableCollection where Self: BidirectionalCollection {
@_inlineable
public mutating func popLast() -> Element? {
if isEmpty { return nil}
else { return removeLast() }
}
}

public struct Queue<Storage: RangeReplaceableCollection>
where Storage: BidirectionalCollection {
public typealias Element = Storage.Element

internal var _in: Storage
internal var _out: Storage

public init() {
_in = Storage()
_out = Storage()
}
}

extension Queue {
public mutating func enqueue(_ newElement: Element) {
_in.append(newElement)
}

public mutating func dequeue() -> Element? {
if _out.isEmpty {
_out.append(contentsOf: _in.reversed())
_in.removeAll()
}
return _out.popLast()
}
}

func testQueue<Elements: Collection>(elements: Elements)
where Elements.Element: Equatable {
var q = Queue<[Elements.Element]>()
for x in elements { q.enqueue(x) }
let results = sequence(state: q) { $0.dequeue() }
let i = results.reduce(0, { i,_ in i &+ 1 })
for x in elements { q.enqueue(x) }
let j = results.reduce(i, { i,_ in i &+ 1 })
CheckResults(j == elements.count*2)
}

let n = 10_000
let workload = (0..<n).map { "\($0): A long enough string to defeat the SSO, or so I hope." }

public func buildWorkload() {
let contents = workload
_ = contents.reduce(0) { $0 + $1.count }
}

@inline(never)
func run_QueueGeneric(_ scale: Int) {
for _ in 0..<scale {
testQueue(elements: workload)
}
}

public struct ConcreteQueue {
internal var _in: [String]
internal var _out: [String]

public init() {
_in = Array()
_out = Array()
}
}

extension ConcreteQueue {
public mutating func enqueue(_ newElement: String) {
_in.append(newElement)
}

public mutating func dequeue() -> String? {
if _out.isEmpty {
_out.append(contentsOf: _in.reversed())
_in.removeAll()
}
return _out.popLast()
}
}

func testConcreteQueue(elements: [String]) {
var q = ConcreteQueue()
for x in elements { q.enqueue(x) }
let results = sequence(state: q) { $0.dequeue() }
let i = results.reduce(0, { i,_ in i &+ 1 })
for x in elements { q.enqueue(x) }
let j = results.reduce(i, { i,_ in i &+ 1 })
CheckResults(j == elements.count*2)
}


@inline(never)
func run_QueueConcrete(_ scale: Int) {
for _ in 0..<scale {
testConcreteQueue(elements: workload)
}
}

3 changes: 3 additions & 0 deletions benchmark/utils/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ import Prims
import PrimsSplit
import ProtocolDispatch
import ProtocolDispatch2
import Queue
import RC4
import RGBHistogram
import RangeAssignment
Expand Down Expand Up @@ -243,6 +244,8 @@ registerBenchmark(Prims)
registerBenchmark(PrimsSplit)
registerBenchmark(ProtocolDispatch)
registerBenchmark(ProtocolDispatch2)
registerBenchmark(QueueGeneric)
registerBenchmark(QueueConcrete)
registerBenchmark(RC4Test)
registerBenchmark(RGBHistogram)
registerBenchmark(RangeAssignment)
Expand Down