Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
4 changes: 3 additions & 1 deletion Sources/Testing/Support/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ struct Graph<K, V> where K: Hashable {
let result: V?

if let key = keyPath.first {
if var child = children[key] {
// The `.take()` here ensures we remove the item from the graph before mutating,
// preventing this from causing a cascading COW update down the tree.
if var child = children[key].take() {
result = child.insertValue(newValue, at: keyPath.dropFirst(), intermediateValue: intermediateValue)
children[key] = child
} else {
Expand Down
47 changes: 46 additions & 1 deletion Tests/TestingTests/Support/GraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// See https://swift.org/CONTRIBUTORS.txt for Swift project authors
//

@testable import Testing
@_spi(Experimental) @_spi(ForToolsIntegrationOnly) @testable import Testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: anything experimental actually being used here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just Test.Clock, but also that doesn't need ForToolsIntegrationOnly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you have that backwards?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, turns out you only need one of the SPI tags to get the declaration. So I could pick either or leave both


@Suite("Graph<K, V> Tests")
struct GraphTests {
Expand Down Expand Up @@ -549,4 +549,49 @@ struct GraphTests {
}
#expect(Set(values) == [123, 124, 456, 457, 13579, 13580, 789, 790, 2468, 2469])
}

@Test
func `inserting many items with the same prefix is not quadratic`() {
// This test verifies that inserting various numbers of elements that
// all share a common prefix scales ~linearly.

var perItemTimes = [Int: Double]()
let clock = Test.Clock()

for itemCount in [500, 1000, 5000, 10000] {
var graph = Graph<String, Int?>()

// Start with a representative target/suite/test prefix
let sharedPrefix = ["ProjectTests", "ProjectSuite", "something(_:)"]
graph[sharedPrefix] = -1

let startTime = clock.now

// Insert many items that all share this prefix
for i in 0..<itemCount {
var path = sharedPrefix
path.append("Argument \(i)")
graph[path] = i
}

#expect(graph[sharedPrefix + ["Argument 0"]] == 0)
#expect(graph[sharedPrefix + ["Argument \(itemCount - 1)"]] == itemCount - 1)

let elapsedTime = startTime.duration(to: clock.now) / .seconds(1)
perItemTimes[itemCount] = elapsedTime / Double(itemCount)
}

let mean = perItemTimes.values.reduce(0, +) / Double(perItemTimes.count)
let variance = perItemTimes.values.reduce(0) { $0 + ($1 - mean) * ($1 - mean) }
let standardDeviation = (variance / (Double(perItemTimes.count) - 1)).squareRoot()

// Standard deviation should be less than 25% of mean

let description = perItemTimes
.sorted { $0.key < $1.key }
.map { "\($0)\t\($1)" }
.joined(separator: "\n")

#expect(standardDeviation < mean * 0.25, "Expected standard deviation to be <25% of mean, but was \(standardDeviation) (mean: \(mean))\n\(description)")
}
}
Loading