Skip to content

Commit 52d9e6f

Browse files
author
Robert Quander
committed
found the actual culprit that destroyed my performance - 10x'd it now, seems much more stable, too
1 parent bd06d5f commit 52d9e6f

File tree

3 files changed

+202
-165
lines changed

3 files changed

+202
-165
lines changed

service/Sources/App/Models/IdentifierGenerator.swift

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,24 @@ import Foundation
22

33
final class IdentifierGenerator {
44
static func generate(from username: String) -> UUID {
5-
var usernameBytes = [UInt8](username.utf8)
5+
var usernameBytes = [UInt8](username.utf8.prefix(16))
66
while usernameBytes.count < 16 {
77
usernameBytes.append(95)
88
}
9-
10-
if usernameBytes.count > 16 {
11-
usernameBytes = Array(usernameBytes.prefix(16))
12-
}
13-
let hex = usernameBytes.map { String(format: "%02x", $0) }.joined()
149

15-
let formatted = "\(hex.prefix(8))-\(hex.dropFirst(8).prefix(4))-\(hex.dropFirst(12).prefix(4))-\(hex.dropFirst(16).prefix(4))-\(hex.dropFirst(20))"
10+
let uuid = uuidFromBytes(usernameBytes)
11+
return uuid
12+
}
1613

14+
private static func uuidFromBytes(_ bytes: [UInt8]) -> UUID {
15+
let hex = String(bytes.lazy.map { hexLookup[Int($0)] }.joined())
16+
let formatted = hexToUUIDString(hex)
1717
return UUID(uuidString: formatted) ?? UUID()
1818
}
1919

20-
}
20+
private static let hexLookup: [String] = (0...255).map { String(format: "%02x", $0) }
21+
22+
private static func hexToUUIDString(_ hex: String) -> String {
23+
return "\(hex.prefix(8))-\(hex.dropFirst(8).prefix(4))-\(hex.dropFirst(12).prefix(4))-\(hex.dropFirst(16).prefix(4))-\(hex.dropFirst(20).prefix(12))"
24+
}
25+
}

service/Sources/App/Models/PostIDGenerator.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ import Foundation
22

33
enum PostIDGenerator {
44
static func secretGenerator(from date: Date) -> UInt64 {
5-
let formatter = DateFormatter()
6-
formatter.dateFormat = "yyyyMMddHHmm"
7-
let dateString = formatter.string(from: date)
8-
let noise = UInt64(dateString) ?? 0
9-
return noise
5+
var calendar = Calendar(identifier: .gregorian)
6+
calendar.timeZone = TimeZone(identifier: "UTC")!
7+
let comps = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: date)
8+
9+
let str = String(format: "%04d%02d%02d%02d%02d",
10+
comps.year!, comps.month!, comps.day!,
11+
comps.hour!, comps.minute!)
12+
return UInt64(str) ?? 0
1013
}
1114

1215
static func derivePostID(profileID: UUID, timestamp: Date, counter: UInt8) -> UUID {
1316
let noise = secretGenerator(from: timestamp)
1417
let combinedNoise = (noise << 8) | UInt64(counter)
15-
var left = Array(profileID.uuidString.utf8)
16-
var right = String(combinedNoise)
1718

18-
while right.count < 32 {
19-
right.append("0")
20-
}
19+
var left = Array(profileID.uuidString.utf8.prefix(32))
20+
while left.count < 32 { left.append(0) }
2121

22+
let right = String(format: "%032llu", combinedNoise)
2223
let center = Array(right.utf8.prefix(32))
23-
while left.count < 32 {
24-
left.append(0)
25-
}
2624

2725
let combined = zip(left, center).map { $0 ^ $1 }
28-
let hex = combined.map { String(format: "%02x", $0) }.joined()
26+
let hex = combined.map { hexLookup[Int($0)] }.joined()
2927
let uuidString = "\(hex.prefix(8))-\(hex.dropFirst(8).prefix(4))-\(hex.dropFirst(12).prefix(4))-\(hex.dropFirst(16).prefix(4))-\(hex.dropFirst(20).prefix(12))"
3028

3129
return UUID(uuidString: uuidString) ?? UUID()
3230
}
31+
32+
private static let hexLookup: [String] = (0...255).map { String(format: "%02x", $0) }
3333
}

0 commit comments

Comments
 (0)