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

NSKeyValueCoding: Safe-Caching for -[NSObject valueForKey:] #445

Merged
merged 19 commits into from
Oct 29, 2024

Conversation

hmelder
Copy link
Contributor

@hmelder hmelder commented Sep 7, 2024

It turns out that valueForKey: is a very expensive operation, and a major bottleneck for Key-Value Observing and other operations such as sorting an array by key.

The accessor search patterns for Key-Value observing are discussed in the Apple Key-Value Coding Programming Guide. The return value may be encapsulated into an NSNumber or NSValue object, depending on the Objective-C type encoding of the return value. This means that once valueForKey: found an existing accessor, the Objective-C type encoding of the accessor is retrieved. We then go through a huge switch case to determine the right way to invoke the IMP and potentially encapsulate the return type. The resulting object is then returned.
The algorithm for setValue:ForKey: is similar.

We can speed this up by caching the IMP of the accessor in a hash table. However, without proper versioning, this quickly becomes very dangerous. The user might exchange implementations, or add new ones expecting the
search pattern invariant to still hold. If we clamp onto an IMP, this invariant no longer holds.

Apple actually just caches the IMP without versioning, as there is no support for safe-caching in objc4.

We will make use of libobjc2's safe caching to avoid this.
Note that the caching is opaque. You will only need to redirect all
valueForKey: calls to the function below.

Note that the Implementation requires method_getTypedSelector_np which was added to libobjc2 here: gnustep/libobjc2@f7a38aa.

@hmelder hmelder requested a review from rfm as a code owner September 7, 2024 10:19
@fredkiefer
Copy link
Member

Any idea why the new type test fails for gcc and would it pass without your changes to GSRuntime?

@hmelder
Copy link
Contributor Author

hmelder commented Sep 7, 2024

Any idea why the new type test fails for gcc and would it pass without your changes to GSRuntime?

Not sure. I'd need to setup a GCC environment to debug this.

Failed test:     (2024-09-07 10:06:20.004 +0000)   types.m:312 ... Ivar returns NSPoint
Failed test:     (2024-09-07 10:06:20.004 +0000)   types.m:314 ... Ivar returns NSRange
Failed test:     (2024-09-07 10:06:20.004 +0000)   types.m:316 ... Ivar returns NSRect
Failed test:     (2024-09-07 10:06:20.004 +0000)   types.m:318 ... Ivar returns NSSize
Failed test:     (2024-09-07 10:06:20.004 +0000)   types.m:320 ... Ivar returns MyStruct

@hmelder
Copy link
Contributor Author

hmelder commented Sep 10, 2024

Test failures are unrelated.

@hmelder
Copy link
Contributor Author

hmelder commented Sep 24, 2024

@rfm, @fredkiefer, it would be great if you could take another look at it.

@hmelder
Copy link
Contributor Author

hmelder commented Oct 24, 2024

@rfm it would be great if you could take another look at this PR :)
I've moved some known CoreGraphics struct types into their proper header to improve compatibility with Apple Frameworks.

Copy link
Contributor

@rfm rfm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All seems to be reasonable to me.

@hmelder hmelder merged commit 6eef1c3 into master Oct 29, 2024
8 checks passed
@hmelder hmelder deleted the kvc-caching branch October 29, 2024 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants