Files
Tim Kientzle 0f6fb60b73 Speed up is [T]
This PR converts array downcast tests to pure test-only in the new `is` runtime
support.  This should produce a 25% speedup for `any is [Int]` as soon as #93155
begins sending these type tests to the new runtime.

Note that this does not impact `array as [Int]` where the source is statically
known to be an array type.  Sema and SILGen collaborate to send those to
specialized ArrayCast operations.  Those operations still
allocate/convert/destroy, but they can be specialized, which gives them an edge
over the general path even with these optimizations.  Those ArrayCast operations
could also benefit from a no-allocations testing path, but that would require
more Sema and SILGen surgery.
2026-10-11 10:38:29 -07:00

45 lines
1.3 KiB
Swift

//===--- ArrayCastTest.swift ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2026 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
//
//===----------------------------------------------------------------------===//
// Measures `x is [Int]` where `x` is an `[Any]` whose elements are all Int
// except the last, which is a String. The test must examine every element
// before it can fail, and it should not need to build a result array to do so.
import TestsUtils
public let benchmarks = [
BenchmarkInfo(
name: "ArrayCastTest.AnyToInt.Fail",
runFunction: run_ArrayCastTestAnyToIntFail,
tags: [.validation, .api, .Array, .runtime],
setUpFunction: { blackHole(anyArray) }),
]
let anyArray: [Any] = {
var a: [Any] = (0..<10).map { $0 as Any }
a.append("not an Int")
return a
}()
@inline(never)
func isIntArray(_ x: Any) -> Bool {
return x is [Int]
}
@inline(never)
public func run_ArrayCastTestAnyToIntFail(_ n: Int) {
let x = anyArray
for _ in 0..<n {
check(!isIntArray(identity(x)))
}
}