]> git.ipfire.org Git - thirdparty/wireguard-apple.git/commitdiff
TunnelsManager: cache access to configuration object
authorJason A. Donenfeld <Jason@zx2c4.com>
Mon, 4 Feb 2019 20:30:33 +0000 (21:30 +0100)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 6 Feb 2019 00:52:31 +0000 (01:52 +0100)
Supposedly we never change it once per object, so we do the objective C
hack of adding it cached to the extension. This prevents 1000s of calls
to the keychain and improves the speed of imports.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
WireGuard/WireGuard/Tunnel/TunnelsManager.swift

index 93f75ff7510984c828f0b9245db4191992493ece..6bcf6f78e313c202d4812f292c0fa808c77d8e81 100644 (file)
@@ -526,7 +526,15 @@ class TunnelContainer: NSObject {
 }
 
 extension NETunnelProviderManager {
+    private static var cachedConfigKey: UInt8 = 0
     var tunnelConfiguration: TunnelConfiguration? {
-        return (protocolConfiguration as? NETunnelProviderProtocol)?.asTunnelConfiguration(called: localizedDescription)
+        if let cached = objc_getAssociatedObject(self, &NETunnelProviderManager.cachedConfigKey) as? TunnelConfiguration {
+            return cached
+        }
+        let config = (protocolConfiguration as? NETunnelProviderProtocol)?.asTunnelConfiguration(called: localizedDescription)
+        if config != nil {
+            objc_setAssociatedObject(self, &NETunnelProviderManager.cachedConfigKey, config, objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN_NONATOMIC)
+        }
+        return config
     }
 }