Skip to content

Commit 8571962

Browse files
authored
Merge pull request #3 from karwa/perf
Perf tweaks
2 parents 8955de8 + 973ef14 commit 8571962

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

Sources/UniqueID/Time.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ internal func _get_system_timestamp() -> UInt64 {
2929
if #available(macOS 10.12, *) {
3030
var time = timespec()
3131
clock_gettime(CLOCK_REALTIME, &time)
32-
timestamp = (UInt64(time.tv_sec) &* 10_000_000) &+ (UInt64(time.tv_nsec) / 100)
32+
timestamp =
33+
(UInt64(bitPattern: Int64(time.tv_sec)) &* 10_000_000) &+ (UInt64(bitPattern: Int64(time.tv_nsec)) / 100)
3334
} else {
3435
var time = timeval()
3536
gettimeofday(&time, nil)
36-
timestamp = (UInt64(time.tv_sec) &* 10_000_000) &+ (UInt64(time.tv_usec) &* 10)
37+
timestamp =
38+
(UInt64(bitPattern: Int64(time.tv_sec)) &* 10_000_000) &+ (UInt64(bitPattern: Int64(time.tv_usec)) &* 10)
3739
}
3840
return timestamp & 0x0FFF_FFFF_FFFF_FFFF
3941
}

Sources/UniqueID/UniqueID+v4.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,23 @@ extension UniqueID {
7373
///
7474
@inlinable
7575
public static func random<RNG>(using rng: inout RNG) -> UniqueID where RNG: RandomNumberGenerator {
76-
var randomBytes: (UInt64, UInt64) = (rng.next(), rng.next())
77-
return withUnsafeMutableBytes(of: &randomBytes) { bytes in
78-
// octet 6 = time_hi_and_version (high octet).
79-
// high 4 bits = version number.
80-
bytes[6] = (bytes[6] & 0xF) | 0x40
81-
// octet 8 = clock_seq_high_and_reserved.
82-
// high 2 bits = variant (10 = standard).
83-
bytes[8] = (bytes[8] & 0x3F) | 0x80
84-
return UniqueID(bytes: bytes.load(as: UniqueID.Bytes.self))
76+
var bytes = UniqueID.null.bytes
77+
withUnsafeMutableBytes(of: &bytes) { dest in
78+
var random = rng.next()
79+
Swift.withUnsafePointer(to: &random) {
80+
dest.baseAddress!.copyMemory(from: UnsafeRawPointer($0), byteCount: 8)
81+
}
82+
random = rng.next()
83+
Swift.withUnsafePointer(to: &random) {
84+
dest.baseAddress!.advanced(by: 8).copyMemory(from: UnsafeRawPointer($0), byteCount: 8)
85+
}
8586
}
87+
// octet 6 = time_hi_and_version (high octet).
88+
// high 4 bits = version number.
89+
bytes.6 = (bytes.6 & 0xF) | 0x40
90+
// octet 8 = clock_seq_high_and_reserved.
91+
// high 2 bits = variant (10 = standard).
92+
bytes.8 = (bytes.8 & 0x3F) | 0x80
93+
return UniqueID(bytes: bytes)
8694
}
8795
}

Sources/UniqueID/UniqueID+v6.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,6 @@ internal var _uuidv6GeneratorState = UUIDv6GeneratorState()
7171
guard pthread_mutexattr_init(&attrs) == 0 else { fatalError("Failed to create pthread_mutexattr_t") }
7272
// Use adaptive spinning before calling in to the kernel (GNU extension).
7373
let _ = pthread_mutexattr_settype(&attrs, CInt(PTHREAD_MUTEX_ADAPTIVE_NP))
74-
// Bump lock-holder's priority if waited on by higher-priority threads.
75-
// Prevents priority inversion.
76-
let _ = pthread_mutexattr_setprotocol(&attrs, CInt(PTHREAD_PRIO_INHERIT))
7774
guard pthread_mutex_init(mutex, &attrs) == 0 else { fatalError("Failed to create pthread_mutex_t") }
7875
pthread_mutexattr_destroy(&attrs)
7976

0 commit comments

Comments
 (0)