Skip to content

Instantly share code, notes, and snippets.

@bobgodwinx
bobgodwinx / libdispatch-efficiency-tips.md
Created December 22, 2023 10:47 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse

@bobgodwinx
bobgodwinx / ImageFilter.swift
Created December 4, 2022 14:59
ChatGPT_Applying_Instagram_Filters_in_Swift
import UIKit
import Foundation
import CoreImage
import MetalPerformanceShaders
// MARK: - Filter Types
enum FilterType {
case clarendon
case gingham
case juno
/// `ViewController`
class ViewController: UIViewController {
private let bag = DisposeBag()
private let indicator = PublishRelay<RxMBProgressHUD.State>()
private let viewModel: ViewModelType
init(_ viewModel: ViewModelType = ViewModel()) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
configure()
/// `ViewModel`
class ViewModel: ViewModelType {
let source: Driver<UIImage?>
let finished: Completable
init(_ model: ModelType = Model()) {
self.source = model.datasource
.map { UIImage.gif(data: $0.data) }
.asDriver(onErrorJustReturn: nil)
protocol ViewModelType {
var source: Driver<UIImage?> {get}
var finished: Completable {get}
}
/// `Model`
struct Model: ModelType {
let datasource: Observable<Resource>
init() {
let url = URL(string: "https://media.giphy.com/media/5z08WdHr0h9SHZekve/giphy.gif")!
let request = URLRequest(url: url)
self.datasource = URLSession.shared.rx
.response(request: request)
typealias Resource = (response: HTTPURLResponse, data: Data)
protocol ModelType {
var datasource: Observable<Resource> {get}
}
class RxMBProgressHUD: ObserverType {
static let shared = RxMBProgressHUD()
enum State {
case hide(view: UIView?, animated: Bool)
case show(view: UIView?, animated: Bool)
}
typealias E = RxMBProgressHUD.State
@bobgodwinx
bobgodwinx / OST02
Created June 25, 2018 06:22
OST02.config
github "ReactiveX/RxSwift" ~> 4.2.0
@bobgodwinx
bobgodwinx / OST01
Last active June 25, 2018 06:18
OST01.config
target 'LegacyApp' do
use_frameworks!
pod 'RxSwift', '~> 4.2.0'
pod 'RxCocoa', '~> 4.2.0'
end