 
 extension NSLock {
     func sync<T>(_ closure: () -> T) -> T {
-        lock(); defer { unlock() }
+        lock(); defer { unlock() };
         return closure()
     }
 }
 
-private final class Handlers<T> { // boxed handlers
-    var fulfill = [(T) -> Void]()
-    var reject = [(Error) -> Void]()
+private struct Handlers<T> { // boxed handlers
+    var fulfill = ContiguousArray<(T) -> Void>()
+    var reject = ContiguousArray<(Error) -> Void>()
 }
 
 private enum Result<T> {
     case fulfilled(T), rejected(Error)
-    /// Returns a `value` if the result is success.
 
     var value: T? {
         if case let .fulfilled(val) = self { return val } else { return nil }
     }
 
-    /// Returns an `error` if the result is failure.
     var error: Error? {
         if case let .rejected(err) = self { return err } else { return nil }
     }

        let _fulfill: (T) -> Void = { value in queue.async { fulfill(value) } }
        let _reject: (Error) -> Void = { error in queue.async { reject(error) } }

        if let result = _register(fulfill: _fulfill, reject: _reject) {
            // already resolved
            switch result {
            case let .fulfilled(value): _fulfill(value)
            case let .rejected(error): _reject(error)
            }
        }
    }

    /// Either registered observers of returns result if resolved.
    private func _register(fulfill: @escaping (T) -> Void, reject: @escaping (Error) -> Void) -> Result<T>? {
        _lock.lock(); defer { _lock.unlock() }
        handlers?.fulfill.append(fulfill)

/// Promises start in a *pending* state and either get *fulfilled* with a
/// value or get *rejected* with an error.
public final class Promise<T> {
    private var handlers: Handlers<T>? // nil when finished
    private var result: Result<T>? // nil when pending

