Skip to content

Instantly share code, notes, and snippets.

@lvalenta
Created July 1, 2022 08:52
Show Gist options
  • Save lvalenta/c557cda96123e838baeb65f22bca83b4 to your computer and use it in GitHub Desktop.
Save lvalenta/c557cda96123e838baeb65f22bca83b4 to your computer and use it in GitHub Desktop.
func orderFood<T: Server>(server: T, size: Int) {
prepareFood(server: server, size: size)
if !isFoodReady(server: server) {
fatalError("Food is not ready for some reason")
}
}
func isFoodReady(server: Server) -> Bool {
server.isFoodReady()
}
output.isFoodReady<A where A: output.Server>(server: A) -> Swift.Bool: //generic function
push rbp
mov rbp, rsp
push r13
sub rsp, 40
mov qword ptr [rbp - 40], rdx
mov rax, rsi
mov rsi, qword ptr [rbp - 40]
mov qword ptr [rbp - 32], rax
mov r13, rdi
mov rdi, qword ptr [rbp - 32]
mov qword ptr [rbp - 24], 0
mov qword ptr [rbp - 16], rdi
mov qword ptr [rbp - 24], r13
mov rax, qword ptr [rsi + 8]
call rax
add rsp, 40
pop r13
pop rbp
ret
output.isFoodReady(server: output.Server) -> Swift.Bool: //non-generic function
push rbp
mov rbp, rsp
push r13
sub rsp, 24
mov qword ptr [rbp - 16], 0
mov qword ptr [rbp - 16], rdi
mov rsi, qword ptr [rdi + 24]
mov qword ptr [rbp - 32], rsi
mov rax, qword ptr [rdi + 32]
mov qword ptr [rbp - 24], rax
call __swift_project_boxed_opaque_existential_1
mov rdi, qword ptr [rbp - 32]
mov rsi, qword ptr [rbp - 24]
mov r13, rax
mov rax, qword ptr [rsi + 8]
call rax
add rsp, 24
pop r13
pop rbp
ret
lea rsi, [rip + (full type metadata for output.DinnerServer)]
add rsi, 8
lea rdx, [rip + (protocol witness table for output.DinnerServer : output.Server in output)]
call (output.isFoodReady<A where A: output.Server>(server: A) -> Swift.Bool)
lea rax, [rip + (full type metadata for output.DinnerServer)]
add rax, 8
mov qword ptr [rbp - 16], rax
lea rax, [rip + (protocol witness table for output.DinnerServer : output.Server in output)]
mov qword ptr [rbp - 8], rax
lea rdi, [rbp - 40]
call (output.isFoodReady(server: output.Server) -> Swift.Bool)
lea rdi, [rbp - 40]
call __swift_destroy_boxed_opaque_existential_1
xor eax, eax
add rsp, 48
pop rbp
ret
func isFoodReady(server: any Server) -> Bool {
server.isFoodReady()
}
func measure(functions: [MeasureFunc], repeats: Int)
func oneRunOf(_ function: () -> ()) -> Double {
let timeBefore = CACurrentMediaTime()
function()
return CACurrentMediaTime() - timeBefore
}
final class TestingClass: Testable {
let variable1 = UUID()
let variable2 = UUID()
let variable3 = UUID()
let variable4 = UUID()
let variable5 = UUID()
}
func getAppendingToArray<T: Testable>(_ item: T) {
var array: Array<T> = []
for _ in 0..<100000 {
array.append(item)
}
}
func getAppendingToArrayDynamicDispatch(_ item: Testable) {
var array: Array<Testable> = []
for _ in 0..<100000 {
array.append(item)
}
}
func getAppendingAndCreatingArray<T: Testable>(ofType usedType: T.Type) {
var array: Array<T> = []
for _ in 0..<100000 {
array.append(usedType.init())
}
}
func getAppendingAndCreatingArrayDynamicDispatch(ofType usedType: Testable.Type) {
var array: Array<Testable> = []
for _ in 0..<100000 {
array.append(usedType.init())
}
}
final class RecursionStaticDispatch<T: Testable> {
let storage: T
let height: Int
init(item: T, height: Int) {
self.storage = item
self.height = height
recurseIfRecursionLessThanFive()
}
func recurseIfRecursionLessThanFive() {
if height < 5 {
let _ = RecursionStaticDispatch(item: storage, height: height + 1)
}
}
}
final class RecursionDynamicDispatch {
let storage: Testable
let height: Int
init(item: Testable, height: Int) {
self.storage = item
self.height = height
recurseIfRecursionLessThanFive()
}
func recurseIfRecursionLessThanFive() {
if height < 5 {
let _ = RecursionDynamicDispatch(item: storage, height: height + 1)
}
}
}
final class ExampleCoordinator {
typealias Dependencies = AllDependencies
(...)
}
final class ExampleCoordinator<Dependencies: AllDependencies> {
(...)
}
struct CleevioDeveloper: Developer { }
func invoiceForCustomer(developer: any Developer) { }
let developer = CleevioDeveloper()
invoiceForCustomer(developer: developer)
func invoiceForCustomer<T: Developer>(developer: T) { }
let developer: any Developer = CleevioDeveloper()
invoiceForCustomer(developer: developer)
protocol CanFly { }
final class ViewModel<T: CanFly> { (…) }
final class ViewController<T: CanFly>: UIViewController {
let viewModel: ViewModel<T>
(…)
}
func isFoodReady(server: Server) -> Bool {
server.isFoodReady()
}
func isFoodReady<T: Server>(server: T) {
server.isFoodReady()
}
func isFoodReady(server: DinnerServer) {
server.isFoodReady()
}
protocol Server {
func isFoodReady() -> Bool
func preparedIngredients(size: Int) -> ()
func finalizePreparation() -> ()
}
extension DinnerServer: Server {
func server.prepareIngredients(size: Int) {
var preparedIngredients = 0
while preparedIngredients < size {
preparedIngredients += 1
}
}
func finalizePreparation() { }
}
func prepareFood<T: Server>(server: T, size: Int) {
server.prepareIngredients(size: size)
server.finalizePreparation()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment