-
Notifications
You must be signed in to change notification settings - Fork 279
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
Open
hmelder
wants to merge
15
commits into
master
Choose a base branch
from
kvc-caching
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
… NSSize have the same layout
fredkiefer
reviewed
Sep 7, 2024
fredkiefer
reviewed
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 |
…ing with CoreGraphics
Test failures are unrelated. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 tolibobjc2
here: gnustep/libobjc2@f7a38aa.