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

Possible performance improvements? #8

Open
krackers opened this issue Jun 11, 2023 · 0 comments
Open

Possible performance improvements? #8

krackers opened this issue Jun 11, 2023 · 0 comments

Comments

@krackers
Copy link

One benefit of the naivemethod_exchangeImp approach is that we can use standard objc_msgSend when calling the original method, which means we benefit from the method cache. However, with ZKSwizzle the ZKOriginalImplementation calls class_getInstanceMethod which bypasses the cache. Moreover, the need to call classFromInfo (which I believe was added so we can statically resolve the caller to namespace to avoid issues if we do multiple swizzlings) is probably some additional overhead in a hotloop.

There's also the

[NSStringFromClass(cls) isEqualToString:NSStringFromClass([self class])]

which I'm not sure why we convert to strings to check instead of comparing the class pointers directly.

While I doubt these matter much for most cases, in the case where you are swizzling a method that is in a hotloop, these might impact performance.

I guess you could at least eliminate the overhead from classFromInfo by offering a version of the macro where you manually pass in the siwzzled class name (sadly C doesn't have a macro like _CLASS_, nor does it have consteval support). However with this method of swizzling, I don't think it's possible to make use of the method cache because just calling objc_msgsend will end up sending the wrong _cmd like you noted in the readme. One possible way around this is to cache things ourselves, but that's probably introducing more overhead and complexity. Another option would be to have a bunch of globals (or a hash map) for the old imp which we set when calling method_exchangeImplementations, so we can then directly call the imp without even needing to look it up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant