<commit_before>//===--- CxxVectorSum.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2012 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 is a benchmark that tracks how quickly Swift can sum up a C++ vector
// as compared to the C++ implementation of such sum.

import TestsUtils
import CxxStdlibPerformance
import Cxx

public let benchmarks = [
  BenchmarkInfo(
      name: "CxxVectorOfU32.Sum.Cxx.RangedForLoop",
      runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
      tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.ForInLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.Reduce",
    runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
    tags: [.validation, .bridging, .cxxInterop])
]

// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
// establish an expected threshold of performance, which when exceeded should
// fail the benchmark.

let vectorSize = 100_000
let iterRepeatFactor = 10

@inline(never)
public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop(_ n: Int) {
  let sum = testVector32Sum(vectorSize, n * iterRepeatFactor)
  blackHole(sum)
}

@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) {
    let vectorOfU32 = makeVector32(vectorSize)
    var sum: UInt32 = 0
    for _ in 0..<(n * iterRepeatFactor) {
      for x in vectorOfU32 {
        sum = sum &+ x
      }
    }
    blackHole(sum)
}

// This function should have comparable performance to
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(_ n: Int) {
  let vectorOfU32 = makeVector32(vectorSize)
  var sum: UInt32 = 0
  for _ in 0..<(n * iterRepeatFactor) {
    var b = vectorOfU32.__beginUnsafe()
    let e = vectorOfU32.__endUnsafe()
    while !cmp(b, e) {
        sum = sum &+ b.pointee
        b = next(b)
    }
  }
  blackHole(sum)
}

@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) {
  let vectorOfU32 = makeVector32(vectorSize)
  var sum: UInt32 = 0
  for _ in 0..<(n * iterRepeatFactor) {
    var i = 0
    let e = vectorOfU32.size()
    while i != e {
        sum = sum &+ vectorOfU32[i]
        i = i &+ 1
    }
  }
  blackHole(sum)
}

@inline(never)<commit_msg>[interop] benchmark: add run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop that doesn't use C++ inline helpers<commit_after>//===--- CxxVectorSum.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2012 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 is a benchmark that tracks how quickly Swift can sum up a C++ vector
// as compared to the C++ implementation of such sum.

import TestsUtils
import CxxStdlibPerformance
import Cxx

public let benchmarks = [
  BenchmarkInfo(
      name: "CxxVectorOfU32.Sum.Cxx.RangedForLoop",
      runFunction: run_CxxVectorOfU32_Sum_Cxx_RangedForLoop,
      tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.ForInLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_ForInLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.RawIteratorLoop.WithCxxInlineHelpers",
    runFunction: run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop_WithCxxInlineHelpers,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.IndexAndSubscriptLoop",
    runFunction: run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop,
    tags: [.validation, .bridging, .cxxInterop]),
  BenchmarkInfo(
    name: "CxxVectorOfU32.Sum.Swift.Reduce",
    runFunction: run_CxxVectorOfU32_Sum_Swift_Reduce,
    tags: [.validation, .bridging, .cxxInterop])
]

// FIXME: compare CxxVectorOfU32SumInCxx to CxxVectorOfU32SumInSwift and
// establish an expected threshold of performance, which when exceeded should
// fail the benchmark.

let vectorSize = 100_000
let iterRepeatFactor = 10

@inline(never)
public func run_CxxVectorOfU32_Sum_Cxx_RangedForLoop(_ n: Int) {
  let sum = testVector32Sum(vectorSize, n * iterRepeatFactor)
  blackHole(sum)
}

@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_ForInLoop(_ n: Int) {
    let vectorOfU32 = makeVector32(vectorSize)
    var sum: UInt32 = 0
    for _ in 0..<(n * iterRepeatFactor) {
      for x in vectorOfU32 {
        sum = sum &+ x
      }
    }
    blackHole(sum)
}

// This function should have comparable performance to
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop_WithCxxInlineHelpers(_ n: Int) {
  let vectorOfU32 = makeVector32(vectorSize)
  var sum: UInt32 = 0
  for _ in 0..<(n * iterRepeatFactor) {
    var b = vectorOfU32.__beginUnsafe()
    let e = vectorOfU32.__endUnsafe()
    while !cmp(b, e) {
        sum = sum &+ b.pointee
        b = next(b)
    }
  }
  blackHole(sum)
}

// This function should have comparable performance to
// `run_CxxVectorOfU32_Sum_Cxx_RangedForLoop`.
@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_RawIteratorLoop(_ n: Int) {
  let vectorOfU32 = makeVector32(vectorSize)
  var sum: UInt32 = 0
  for _ in 0..<(n * iterRepeatFactor) {
    var b = vectorOfU32.__beginUnsafe()
    let e = vectorOfU32.__endUnsafe()
    while b != e {
        sum = sum &+ b.pointee
        b = b.successor()
    }
  }
  blackHole(sum)
}

@inline(never)
public func run_CxxVectorOfU32_Sum_Swift_IndexAndSubscriptLoop(_ n: Int) {
  let vectorOfU32 = makeVector32(vectorSize)
  var sum: UInt32 = 0
  for _ in 0..<(n * iterRepeatFactor) {
    var i = 0
    let e = vectorOfU32.size()
    while i != e {
        sum = sum &+ vectorOfU32[i]
        i = i &+ 1
    }
  }
  blackHole(sum)
}

@inline(never)
{'fraction_non_alphanumeric': 0.1335290827740492, 'fraction_numerical': 0.028243847874720358, 'mean_word_length': 3.2577380952380954, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424783', 'n_tokens_mistral': 2622, 'n_tokens_neox': 2449, 'n_words': 579}