Skip to content

Commit

Permalink
[SyntaxText] Always use memcmp
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
rintaro committed Apr 24, 2024
1 parent cbff435 commit 99d402c
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions Sources/SwiftSyntax/SyntaxText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ private import Darwin
private import Glibc
#elseif canImport(Musl)
private import Musl
#elseif canImport(CRT)
private import CRT
#endif
#else
#if canImport(Darwin)
Expand All @@ -25,6 +27,8 @@ import Darwin
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(CRT)
import CRT
#endif
#endif

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

/// Hash the contents of this ``SyntaxText`` into `hasher`.
Expand Down Expand Up @@ -270,19 +274,3 @@ extension String {
}
}
}

private func compareMemory(
_ s1: UnsafePointer<UInt8>,
_ s2: UnsafePointer<UInt8>,
_ count: Int
) -> Bool {
precondition(count >= 0)
#if canImport(Darwin)
return Darwin.memcmp(s1, s2, count) == 0
#elseif canImport(Glibc)
return Glibc.memcmp(s1, s2, count) == 0
#else
return UnsafeBufferPointer(start: s1, count: count)
.elementsEqual(UnsafeBufferPointer(start: s2, count: count))
#endif
}

0 comments on commit 99d402c

Please sign in to comment.