Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: performance issue in debug info calculation #146

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions HCaptcha/Classes/HCaptchaDebugInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// HCaptchaDebugInfo.m
// HCaptcha
//
// Copyright © 2022 HCaptcha. All rights reserved.
// Copyright © 2024 HCaptcha. All rights reserved.
//

import Foundation
Expand All @@ -16,11 +16,11 @@ extension String {
}
}

private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer<CC_MD5_CTX>) {
private func updateInfoFor(_ image: String, _ ctx: UnsafeMutablePointer<CC_MD5_CTX>, depth: UInt32 = 16) {
var count: UInt32 = 0
if let imagePtr = (image as NSString).utf8String {
let classes = objc_copyClassNamesForImage(imagePtr, &count)
for cls in UnsafeBufferPointer<UnsafePointer<CChar>>(start: classes, count: Int(count)) {
for cls in UnsafeBufferPointer<UnsafePointer<CChar>>(start: classes, count: Int(min(depth, count))) {
CC_MD5_Update(ctx, cls, CC_LONG(strlen(cls)))
}
classes?.deallocate()
Expand Down Expand Up @@ -53,6 +53,9 @@ class HCaptchaDebugInfo {
}

private class func buildDebugInfo() -> [String] {
let depth: UInt32 = 16
var depsCount = 0
var sysCount = 0
let depsCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let sysCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
let appCtx = UnsafeMutablePointer<CC_MD5_CTX>.allocate(capacity: 1)
Expand All @@ -67,6 +70,16 @@ class HCaptchaDebugInfo {
let systemFramework = image.contains("/Library/PrivateFrameworks/") ||
image.contains("/System/Library/Frameworks/")

if systemFramework && sysCount < depth {
sysCount += 1
} else if !systemFramework && depsCount < depth {
depsCount += 1
} else if sysCount < depth || depsCount < depth {
continue
} else {
break
}

let md5Ctx = systemFramework ? sysCtx : depsCtx
updateInfoFor(image, md5Ctx)
}
Expand Down
Loading