Skip to content

[RFC][DNM] Add isIdentical Method for Quick Comparisons to Data #1384

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
38 changes: 37 additions & 1 deletion Benchmarks/Benchmarks/Essentials/BenchmarkEssentials.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ let benchmarks = {
return box
})

Benchmark("DataIdenticalEmpty", closure: { benchmark, box in
blackHole(box.d1.isIdentical(to: box.d2))
}, setup: { () -> TwoDatasBox in
let d1 = Data()
let d2 = d1
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})

Benchmark("DataEqualInline", closure: { benchmark, box in
blackHole(box.d1 == box.d2)
}, setup: { () -> TwoDatasBox in
Expand All @@ -75,6 +84,15 @@ let benchmarks = {
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})

Benchmark("DataIdenticalInline", closure: { benchmark, box in
blackHole(box.d1.isIdentical(to: box.d2))
}, setup: { () -> TwoDatasBox in
let d1 = createSomeData(12) // Less than size of InlineData.Buffer
let d2 = d1
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})

Benchmark("DataNotEqualInline", closure: { benchmark, box in
blackHole(box.d1 != box.d2)
Expand All @@ -93,7 +111,16 @@ let benchmarks = {
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})


Benchmark("DataIdenticalLarge", closure: { benchmark, box in
blackHole(box.d1.isIdentical(to: box.d2))
}, setup: { () -> TwoDatasBox in
let d1 = createSomeData(1024 * 8)
let d2 = d1
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})

Benchmark("DataNotEqualLarge", closure: { benchmark, box in
blackHole(box.d1 != box.d2)
}, setup: { () -> TwoDatasBox in
Expand All @@ -112,6 +139,15 @@ let benchmarks = {
return box
})

Benchmark("DataIdenticalReallyLarge", closure: { benchmark, box in
blackHole(box.d1.isIdentical(to: box.d2))
}, setup: { () -> TwoDatasBox in
let d1 = createSomeData(1024 * 1024 * 8)
let d2 = d1
let box = TwoDatasBox(d1: d1, d2: d2)
return box
})

Benchmark("DataNotEqualReallyLarge", closure: { benchmark, box in
blackHole(box.d1 != box.d2)
}, setup: { () -> TwoDatasBox in
Expand Down
50 changes: 50 additions & 0 deletions Sources/FoundationEssentials/Data/Data.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2974,3 +2974,53 @@ extension Data : Codable {
}
}
}

extension Data {
/// Returns a boolean value indicating whether this data is identical to
/// `other`.
///
/// Two data values are identical if there is no way to distinguish between
/// them.
///
/// Comparing data this way includes comparing (normally) hidden
/// implementation details such as the memory location of any underlying
/// data storage object. Therefore, identical data are guaranteed to
/// compare equal with `==`, but not all equal data are considered
/// identical.
///
/// - Performance: O(1)
@_alwaysEmitIntoClient
public func isIdentical(to other: Self) -> Bool {
// See if both are empty
switch (self._representation, other._representation) {
case (.empty, .empty):
return true
case (.inline, .inline), (.slice, .slice), (.large, .large):
// Continue on to checks below
break
default:
return false
}

let length1 = self.count
let length2 = other.count

// Unequal length data can never be equal
guard length1 == length2 else {
return false
}

if length1 > 0 {
return self.withUnsafeBytes { (b1: UnsafeRawBufferPointer) in
return other.withUnsafeBytes { (b2: UnsafeRawBufferPointer) in
// If they have the same base address and same count, it is equal
let b1Address = b1.baseAddress!
let b2Address = b2.baseAddress!

return b1Address == b2Address
}
}
}
return true
}
}
18 changes: 18 additions & 0 deletions Tests/FoundationEssentialsTests/DataTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ extension Data {
}
}

func createSomeData(_ length: Int) -> Data {
var d = Data(repeating: 42, count: length)
// Set a byte to be another value just so we know we have a unique pointer to the backing
// For maximum inefficiency in the not equal case, set the last byte
d[length - 1] = UInt8.random(in: UInt8.min..<UInt8.max)
return d
}

@Suite("Data")
private final class DataTests {

Expand Down Expand Up @@ -197,6 +205,16 @@ private final class DataTests {
#expect(d1 == d2, "Data should be equal")
}

@Test func identical() {
let d1 = createSomeData(1024 * 8)
let d2 = createSomeData(1024 * 8)

#expect(d1.isIdentical(to: d1), "Data should be identical")
#expect(d2.isIdentical(to: d2), "Data should be identical")
#expect(!(d1.isIdentical(to: d2)), "Data should be identical")
#expect(!(d2.isIdentical(to: d1)), "Data should be identical")
}

@Test func dataInSet() {
let d1 = dataFrom("Hello")
let d2 = dataFrom("Hello")
Expand Down