20170127 tokyoserversideswiftmeetup資料
TRANSCRIPT
Swift C var kev = kevent(); kev.ident = UInt(socket); kev.filter = Int16(EVFILT_READ); kev.flags = UInt16(flags); kev.data = 0; ret = kevent(kq, &kev, 1, nil, 0, nil)
C
thread poolGCD pthread(C
mutexpthread_cond ( C 1
1
Swift O(1)data race)
mutex
class AsyncQueue<T> : AsyncQueueType<T> { var queue = [T]()
override func put(obj: T) throws { mutex.lock() defer { mutex.unlock() } queue += [obj] } override func get() throws -> T? { mutex.lock() defer { mutex.unlock() } queue.remove(at: 0) }
protocol GenericQueue{ associatedtype A }
class QueueA<B> : GenericQueue{ typealias A = B }
class QueueB<B> : GenericQueue{ // Queue typealias A = B }
typealias ClosureType = () -> Void
class QueueUser { func useQueue(queue: GenericQueue<ClosureType>) { // ❌ } }
Swift Array O(1)
Swift Array RangeReplaceableCollection extension
public struct Array<Element> : RandomAccessCollection, MutableCollection { extension Array : RangeReplaceableCollection {
Swift Array github: swift/stdlib/public/core/Arrays.swift.gyb
gyb = Generate Your Boilerplate Python
Python
Arrays.swift.gyb extension ${Self} : RangeReplaceableCollection, _ArrayProtocol { public mutating func append<S : Sequence>(contentsOf newElements: S) where S.Iterator.Element == Element { let oldCount = self.count let capacity = self.capacity let newCount = oldCount + newElements.underestimatedCount if newCount > capacity { self.reserveCapacity( Swift.max(newCount, _growArrayCapacity(capacity))) } _arrayAppendSequence(&self._buffer, newElements) }
_growArrayCapacity
internal func _growArrayCapacity(_ capacity: Int) -> Int { return capacity * 2 }
capacity 2 O(1)
Wikipedia Amortized analysis https://en.wikipedia.org/wiki/Amortized_analysis