From 66b750e3a20450619f1f7d351af4bc0b2a051113 Mon Sep 17 00:00:00 2001 From: Alex Babrykovich Date: Sun, 13 Oct 2024 23:47:12 +0200 Subject: [PATCH] fix: improve logging performance by switch from print to NSLog (#165) * fix: improve logging performance by switch from print to NSLog * bump: version to 2.9.1 --- CHANGELOG.md | 4 ++ Example/Podfile.lock | 6 +-- HCaptcha.podspec | 2 +- HCaptcha/Classes/HCaptchaDebugInfo.swift | 61 ++++++++++-------------- HCaptcha/Classes/HCaptchaLog.swift | 12 ++--- 5 files changed, 38 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1135ab..e980379 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# 2.9.1 + +- Fix: performance issue on building a timestamp prefix in `print` (`HCaptchaLog`) + # 2.9.0 - Fix: emit `.htmlLoadError` if `HCaptcha` WebView is not loaded in specified timeout (5 sec default) diff --git a/Example/Podfile.lock b/Example/Podfile.lock index 596f427..1900b0e 100644 --- a/Example/Podfile.lock +++ b/Example/Podfile.lock @@ -1,7 +1,7 @@ PODS: - AppSwizzle (1.3.1) - - HCaptcha/Core (2.9.0) - - HCaptcha/RxSwift (2.9.0): + - HCaptcha/Core (2.9.1) + - HCaptcha/RxSwift (2.9.1): - HCaptcha/Core - RxSwift (~> 6.2.0) - RxBlocking (6.2.0): @@ -37,7 +37,7 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: AppSwizzle: db36e436f56110d93e5ae0147683435df593cabc - HCaptcha: 7f68727bcf0b01d7022585485e3c577bdfb11d87 + HCaptcha: e97d412ae88e01c09cbbdb1582a710f389403ae8 RxBlocking: 0b29f7d2079109a8de49c411381bed7c33ef1eeb RxCocoa: 4baf94bb35f2c0ab31bc0cb9f1900155f646ba42 RxRelay: e72dbfd157807478401ef1982e1c61c945c94b2f diff --git a/HCaptcha.podspec b/HCaptcha.podspec index 4d0b89a..319bfd1 100644 --- a/HCaptcha.podspec +++ b/HCaptcha.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'HCaptcha' - s.version = '2.9.0' + s.version = '2.9.1' s.summary = 'HCaptcha for iOS' s.swift_version = '5.0' diff --git a/HCaptcha/Classes/HCaptchaDebugInfo.swift b/HCaptcha/Classes/HCaptchaDebugInfo.swift index ffb12c5..fc0c003 100644 --- a/HCaptcha/Classes/HCaptchaDebugInfo.swift +++ b/HCaptcha/Classes/HCaptchaDebugInfo.swift @@ -7,23 +7,15 @@ import Foundation import CommonCrypto -import ObjectiveC.runtime import UIKit -extension String { +private extension String { func jsSanitize() -> String { return self.replacingOccurrences(of: ".", with: "_") } -} -private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer, depth: UInt32 = 16) { - var count: UInt32 = 0 - if let imagePtr = (image as NSString).utf8String { - let classes = objc_copyClassNamesForImage(imagePtr, &count) - for cls in UnsafeBufferPointer>(start: classes, count: Int(min(depth, count))) { - CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls))) - } - classes?.deallocate() + var isSystemFramework: Bool { + return self.contains("/System/Library/") || self.contains("/usr/lib/") } } @@ -41,7 +33,6 @@ private func bundleShortVersion() -> String { } class HCaptchaDebugInfo { - public static let json: String = HCaptchaDebugInfo.buildDebugInfoJson() private class func buildDebugInfoJson() -> String { @@ -53,9 +44,6 @@ class HCaptchaDebugInfo { } private class func buildDebugInfo() -> [String] { - let depth: UInt32 = 16 - var depsCount = 0 - var sysCount = 0 let depsCtx = UnsafeMutablePointer.allocate(capacity: 1) let sysCtx = UnsafeMutablePointer.allocate(capacity: 1) let appCtx = UnsafeMutablePointer.allocate(capacity: 1) @@ -63,29 +51,32 @@ class HCaptchaDebugInfo { CC_MD5_Init(sysCtx) CC_MD5_Init(appCtx) - for framework in Bundle.allFrameworks { - guard let frameworkPath = URL(string: framework.bundlePath) else { continue } - let frameworkBin = frameworkPath.deletingPathExtension().lastPathComponent - let image = frameworkPath.appendingPathComponent(frameworkBin).absoluteString - let systemFramework = image.contains("/Library/PrivateFrameworks/") || - image.contains("/System/Library/Frameworks/") + let loadedCount = Int(min(objc_getClassList(nil, 0), 1024)) + if loadedCount > 0 { + let classes = UnsafeMutablePointer.allocate(capacity: loadedCount) + defer { classes.deallocate() } - if systemFramework && sysCount < depth { - sysCount += 1 - } else if !systemFramework && depsCount < depth { - depsCount += 1 - } else if sysCount < depth || depsCount < depth { - continue - } else { - break - } + _ = objc_getClassList(AutoreleasingUnsafeMutablePointer(classes), Int32(loadedCount)) - let md5Ctx = systemFramework ? sysCtx : depsCtx - updateInfoFor(image, md5Ctx) - } + for idx in 0..