Skip to content

Instantly share code, notes, and snippets.

@raphlinus
Last active May 28, 2022 05:03
Show Gist options
  • Save raphlinus/4e4b1937c9ca16edd6a19aac9ba24a30 to your computer and use it in GitHub Desktop.
Save raphlinus/4e4b1937c9ca16edd6a19aac9ba24a30 to your computer and use it in GitHub Desktop.
SwiftUI code for comparing virtual scrolling performance
// Copyright 2022 Google LLC.
// SPDX-License-Identifier: Apache-2.0
// Scrolling test case by Raph Levien
import SwiftUI
import CryptoKit
struct ContentView: View {
var body: some View {
ScrollView {
LazyVStack {
ForEach(1...10000, id: \.self) { i in
Text("\(i): " + iterhash(i))
}
}
}
}
}
let hex: [UInt8] = [0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66]
func format_hex(_ hash: SHA256, _ buf: inout [UInt8]) {
var j = 0
hash.finalize().forEach { byte in
buf[j] = hex[Int(byte) >> 4]
buf[j + 1] = hex[Int(byte) & 0xf]
j += 2
}
}
func iterhash(_ i: Int) -> String {
let s = "\(i)"
var h = SHA256()
h.update(data: s.data(using: .utf8)!)
var buf = [UInt8](repeating: 0, count: 64)
format_hex(h, &buf)
for _ in 1..<i {
h = SHA256()
h.update(data: buf)
format_hex(h, &buf)
}
return String(decoding: buf, as: UTF8.self)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment