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

[foundation] Cache parts of NSObject.ConformsToProtocol #15294

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Changes from 1 commit
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
30 changes: 30 additions & 0 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,36 @@ public virtual bool ConformsToProtocol (NativeHandle protocol)
if (!Runtime.DynamicRegistrationSupported)
return false;

// the linker/trimmer will remove the following code if the dynamic registrar is removed from the app
var classHandle = ClassHandle;
bool new_map = false;
lock (protocol_cache) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Theoretically, the class map and the protocol map could use separate locks, but not sure how often this is called concurrently for it to matter.

Alternatively, ConcurrentDictionary - if it's already linked in - could also be a good choice.

Copy link
Member

Choose a reason for hiding this comment

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

We don't use ConcurrentDictionary anywhere else, so that would mean it could never be linked away.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Theoretically, the class map and the protocol map could use separate locks

Yes, but that did not help the benchmarks (which is totally not concurrent [1] since the calls all comes from the main/ui thread) and I prefer doing the simplest PR to achieve my objectives 😄

[1] but, beside my benchmark, this has to be safe to call from many threads (hence the lock)

Alternatively, ConcurrentDictionary

Same as @rolfbjarne mentioned, in addition to my non-concurrency comment.

Now if a different test case comes up where concurrency has a measurable impact then it would be worth to consider both suggestions.

#if NET
ref var map = ref CollectionsMarshal.GetValueRefOrAddDefault (protocol_cache, classHandle, out var exists);
if (!exists)
map = new ();
ref var result = ref CollectionsMarshal.GetValueRefOrAddDefault (map, protocol, out exists);
if (!exists)
result = DynamicConformsToProtocol (protocol);
#else
if (!protocol_cache.TryGetValue (classHandle, out var map)) {
map = new ();
new_map = true;
protocol_cache.Add (classHandle, map);
}
if (new_map || !map.TryGetValue (protocol, out var result)) {
result = DynamicConformsToProtocol (protocol);
map.Add (protocol, result);
}
#endif
return result;
}
}

static Dictionary<NativeHandle, Dictionary<NativeHandle, bool>> protocol_cache = new ();

bool DynamicConformsToProtocol (NativeHandle protocol)
{
object [] adoptedProtocols = GetType ().GetCustomAttributes (typeof (AdoptsAttribute), true);
foreach (AdoptsAttribute adopts in adoptedProtocols){
if (adopts.ProtocolHandle == protocol)
Expand Down