Skip to content

Instantly share code, notes, and snippets.

View jacobsapps's full-sized avatar

Jacob Bartlett jacobsapps

View GitHub Profile
private func loadImages() {
let urls = (0..<50_000).compactMap { _ in
Bundle.main.url(forResource: "monalisa", withExtension: "jpg")
}
for url in urls {
autoreleasepool {
if let data = try? Data(contentsOf: url),
let image = UIImage(data: data) {
print(image.size)
}
struct Container {
private var backingString: String = "Hello"
var value: String {
get {
backingString
}
_modify {
yield &backingString
}
withUnsafeTemporaryAllocation(of: UInt8.self, capacity: size) { tempBuffer in
for i in 0..<tempBuffer.count {
tempBuffer[i] = UInt8(i * 2)
}
for byte in tempBuffer {
print(byte) // 0, 2, 4, 6, 8, 10, 12, 14
}
}
let size = MemoryLayout<Int64>.size // 8
let rawPointer = malloc(size)! // UnsafeMutableRawPointer
memset(rawPointer, 0, size)
for i in 0..<size {
rawPointer.assumingMemoryBound(to: UInt8.self)[i] = UInt8(i)
}
let buffer = UnsafeRawBufferPointer(start: rawPointer, count: size)
for byte in buffer {
print(byte) // 0, 1, 2, 3, 4, 5, 6, 7
import Darwin
func loadFramework(at path: String) throws -> UnsafeMutableRawPointer? {
guard let handle = dlopen(path, RTLD_NOW) else {
if let error = dlerror() {
throw NSError(domain: "dlopen", code: 1, userInfo: [NSLocalizedDescriptionKey: String(cString: error)])
}
throw NSError(domain: "dlopen", code: 2, userInfo: [NSLocalizedDescriptionKey: "Unknown error"])
}
return handle
extension MainActor {
public static func assumeIsolated<T : Sendable>(
_ operation: @MainActor () throws -> T,
file: StaticString = #fileID, line: UInt = #line
) rethrows -> T {
typealias YesActor = @MainActor () throws -> T
typealias NoActor = () throws -> T
let executor: Builtin.Executor = unsafe Self.shared.unownedExecutor.executor
guard _taskIsCurrentExecutor(executor) else {
import Dispatch
extension Task {
static func sync(_ code: sending () async throws(Failure) -> Success) throws(Failure) -> Success {
let semaphore = DispatchSemaphore(value: 0)
nonisolated(unsafe) var result: Result<Success, Failure>? = nil
withoutActuallyEscaping(code) {
nonisolated(unsafe) let sendableCode = $0
let coreTask = Task<Void, Never>.detached(priority: .userInitiated) { @Sendable () async -> Void in
do {
List {
ForEach(0..<1_000) { _ in
PostCellView()
.listRowInsets(EdgeInsets())
.listRowSeparator(.hidden)
.background(Color.clear)
}
}
.listStyle(PlainListStyle())
.scrollContentBackground(.hidden)
ScrollView {
LazyVStack {
ForEach(0..<1_000) { _ in
PostCellView()
.frame(height: Double.random(in: 150...300))
.clipped()
}
}
.scrollIndicators(.visible)
}
ScrollView {
LazyVStack {
ForEach(0..<1_000) { _ in
PostCellView()
}
}
}