From 12e3867ccb1583bd507cf0c992f74f08dcfaea95 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Mon, 19 Sep 2022 21:25:07 -0700 Subject: [PATCH] [Collections] Allow determining if internal checking is enabled In the benchmark target, complain loudly if consistency checks are enabled. --- Benchmarks/Sources/benchmark-tool/main.swift | 12 ++++++++++++ Sources/DequeModule/Deque+Testing.swift | 6 ++++++ .../OrderedSet/OrderedSet+Testing.swift | 6 ++++++ .../PersistentDictionary+Debugging.swift | 6 ++++-- .../PriorityQueueModule/Heap+Invariants.swift | 8 ++++++-- Sources/_CollectionsUtilities/Debugging.swift | 19 +++++++++++++++++++ 6 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 Sources/_CollectionsUtilities/Debugging.swift diff --git a/Benchmarks/Sources/benchmark-tool/main.swift b/Benchmarks/Sources/benchmark-tool/main.swift index 9d39fff8..51232829 100644 --- a/Benchmarks/Sources/benchmark-tool/main.swift +++ b/Benchmarks/Sources/benchmark-tool/main.swift @@ -11,6 +11,18 @@ import CollectionsBenchmark import Benchmarks +import DequeModule + +if Deque._isConsistencyCheckingEnabled { + complain(""" + *** INTERNAL CONSISTENCY CHECKING IS ENABLED *** + + Performance guarantees aren't valid in this configuration, + and benchmarking data will be largely useless. Proceed at + your own risk. + + """) +} var benchmark = Benchmark(title: "Collection Benchmarks") benchmark.registerCustomGenerators() diff --git a/Sources/DequeModule/Deque+Testing.swift b/Sources/DequeModule/Deque+Testing.swift index 00692981..b9dc6d9c 100644 --- a/Sources/DequeModule/Deque+Testing.swift +++ b/Sources/DequeModule/Deque+Testing.swift @@ -9,10 +9,16 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + // This file contains exported but non-public entry points to support clear box // testing. extension Deque { + public static var _isConsistencyCheckingEnabled: Bool { + _isCollectionsInternalCheckingEnabled + } + /// The maximum number of elements this deque is currently able to store /// without reallocating its storage buffer. /// diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift index edac2d8b..84443339 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift @@ -9,12 +9,18 @@ // //===----------------------------------------------------------------------===// +import _CollectionsUtilities + extension OrderedSet._UnstableInternals { @_spi(Testing) public var capacity: Int { base._capacity } @_spi(Testing) public var minimumCapacity: Int { base._minimumCapacity } @_spi(Testing) public var scale: Int { base._scale } @_spi(Testing) public var reservedScale: Int { base._reservedScale } @_spi(Testing) public var bias: Int { base._bias } + + public static var isConsistencyCheckingEnabled: Bool { + _isCollectionsInternalCheckingEnabled + } } extension OrderedSet { diff --git a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift index 1206ec5f..d6d0ef13 100644 --- a/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift +++ b/Sources/PersistentCollections/PersistentDictionary/PersistentDictionary+Debugging.swift @@ -12,12 +12,14 @@ import _CollectionsUtilities extension PersistentDictionary { + public static var _isConsistencyCheckingEnabled: Bool { + _isCollectionsInternalCheckingEnabled + } + public func _dump(iterationOrder: Bool = false) { _root.dump(iterationOrder: iterationOrder) } -} -extension PersistentDictionary { public static var _maxDepth: Int { _Level.limit } diff --git a/Sources/PriorityQueueModule/Heap+Invariants.swift b/Sources/PriorityQueueModule/Heap+Invariants.swift index 418bfe8c..ab1d891a 100644 --- a/Sources/PriorityQueueModule/Heap+Invariants.swift +++ b/Sources/PriorityQueueModule/Heap+Invariants.swift @@ -9,10 +9,14 @@ // //===----------------------------------------------------------------------===// -import Swift +import _CollectionsUtilities extension Heap { - #if COLLECTIONS_INTERNAL_CHECKS + public static var _isConsistencyCheckingEnabled: Bool { + _isCollectionsInternalCheckingEnabled + } + + #if COLLECTIONS_INTERNAL_CHECKSx /// Visits each item in the heap in depth-first order, verifying that the /// contents satisfy the min-max heap property. @inlinable diff --git a/Sources/_CollectionsUtilities/Debugging.swift b/Sources/_CollectionsUtilities/Debugging.swift new file mode 100644 index 00000000..0006548e --- /dev/null +++ b/Sources/_CollectionsUtilities/Debugging.swift @@ -0,0 +1,19 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2022 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 +// +//===----------------------------------------------------------------------===// + +@inlinable @inline(__always) +public var _isCollectionsInternalCheckingEnabled: Bool { +#if COLLECTIONS_INTERNAL_CHECKS + return true +#else + return false +#endif +}