Skip to content

Commit bd19a8f

Browse files
committed
[SyntaxText] Always use memcmp
`compareMemory(_:_:_:)` was to abstract `memcmp` or `Collection.elementsEqual(_:)` depending on the platform or build settings. Now that we consistently imports "platform" system module, so `memcmp` is reliably available.
1 parent a18c2c7 commit bd19a8f

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

Sources/SwiftSyntax/SyntaxText.swift

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ private import Darwin
1717
private import Glibc
1818
#elseif canImport(Musl)
1919
private import Musl
20+
#elseif canImport(ucrt)
21+
private import ucrt
2022
#endif
2123
#else
2224
#if canImport(Darwin)
@@ -25,6 +27,8 @@ import Darwin
2527
import Glibc
2628
#elseif canImport(Musl)
2729
import Musl
30+
#elseif canImport(ucrt)
31+
import ucrt
2832
#endif
2933
#endif
3034

@@ -131,7 +135,7 @@ public struct SyntaxText: Sendable {
131135
while start <= stop {
132136
// Force unwrappings are safe because we know 'self' and 'other' are both
133137
// not empty.
134-
if compareMemory(self.baseAddress! + start, other.baseAddress!, other.count) {
138+
if memcmp(self.baseAddress! + start, other.baseAddress!, numericCast(other.count)) == 0 {
135139
return start..<(start + other.count)
136140
} else {
137141
start += 1
@@ -194,11 +198,11 @@ extension SyntaxText: Hashable {
194198
// SwiftSyntax use cases, comparing the same SyntaxText instances is
195199
// extremely rare, and checking it causes extra branch.
196200
// The most common usage is comparing parsed text with a static text e.g.
197-
// `token.text == "func"`. In such cases `compareMemory`(`memcmp`) is
198-
// optimized to a `cmp` or similar opcode if either operand is a short static
199-
// text. So the same-baseAddress shortcut doesn't give us a huge performance
200-
// boost even if they actually refer the same memory.
201-
return compareMemory(lBase, rBase, lhs.count)
201+
// `token.text == "func"`. In such cases `memcmp` is optimized to a `cmp` or
202+
// similar opcode if either operand is a short static text. So the
203+
// same-baseAddress shortcut doesn't give us a huge performance boost even
204+
// if they actually refer the same memory.
205+
return memcmp(lBase, rBase, numericCast(lhs.count)) == 0
202206
}
203207

204208
/// Hash the contents of this ``SyntaxText`` into `hasher`.
@@ -270,19 +274,3 @@ extension String {
270274
}
271275
}
272276
}
273-
274-
private func compareMemory(
275-
_ s1: UnsafePointer<UInt8>,
276-
_ s2: UnsafePointer<UInt8>,
277-
_ count: Int
278-
) -> Bool {
279-
precondition(count >= 0)
280-
#if canImport(Darwin)
281-
return Darwin.memcmp(s1, s2, count) == 0
282-
#elseif canImport(Glibc)
283-
return Glibc.memcmp(s1, s2, count) == 0
284-
#else
285-
return UnsafeBufferPointer(start: s1, count: count)
286-
.elementsEqual(UnsafeBufferPointer(start: s2, count: count))
287-
#endif
288-
}

0 commit comments

Comments
 (0)