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

Conversation

spouliot
Copy link
Contributor

@spouliot spouliot commented Jun 17, 2022

Note that the call to native code still always happen (not cached) since the application could use class_addProtocol to add conformance to a protocol at runtime.

So the cache is limited to the .net specific reflection code that is present (only) when the dynamic registrar is included inside applications. This is the default for macOS apps, but not iOS / tvOS or MacCatalyst apps.

The linker/trimmer will remove the caching code when the dynamic registrar is removed. IOW this PR should not have any impact, performance or size, for most iOS apps (where the dynamic registrar is removed by default).

Fix #14065

Running Dope on macOS, a 2 minutes benchmark, shows the following times (in seconds and percentage) spent calling this API:

Before

Screen Shot 2022-06-15 at 9 21 22 PM

  • RemoveFromSuperview 7.99s (6.4%)
    • NSObject.ConformsToProtocol 3.26s (2.6%)

After

Screen Shot 2022-06-15 at 9 24 42 PM

  • RemoveFromSuperview 4.67s (3.8%)
    • NSObject.ConformsToProtocol 0.32s (.26%)

So a 10x improvements on ConformsToProtocol which helps a lot the code path calling RemoveFromSuperview.

Note that the call to native code still _always_ happen (not cached) since the application could use `class_addProtocol` to add conformance to a protocol at runtime.

So the cache is limited to the .net specific reflection code that is present (only) when the dynamic registrar is included inside applications. This is the default for macOS apps, but not iOS / tvOS or MacCatalyst apps.

The linker/trimmer will remove the caching code when the dynamic registrar is removed. IOW this PR should not have any impact, performance or size, for most iOS apps (where the dynamic registrar is removed by default).

Fix dotnet#14065

Running Dope on macOS, a 2 minutes benchmark, shows the following times (in seconds and percentage) spent calling this API:

**Before**

* `RemoveFromSuperview` 7.99s (6.4%)
  * `NSObject.ConformsToProtocol` 3.26s (2.6%)

**After**

* `RemoveFromSuperview` 4.67s (3.8%)
  * `NSObject.ConformsToProtocol` 0.32s (.26%)

So a 10x improvements on `ConformsToProtocol` which helps a lot the code path calling `RemoveFromSuperview`.
Copy link
Contributor

@chamons chamons left a comment

Choose a reason for hiding this comment

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

This is some deep magic, and really needs @rolfbjarne 's 👀 , but seems reasonable to me.

Cute map = new (); btw.

Copy link
Member

@rolfbjarne rolfbjarne left a comment

Choose a reason for hiding this comment

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

This is great!

Just a few minor things

@rolfbjarne
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

✅ Generator diff

Generator diff is empty

Pipeline on Agent
Hash: ddf76f4b8daee8fedc72049f63278174aa482fe2 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

// 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.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

✅ Generator diff

Generator diff is empty

Pipeline on Agent
Hash: d03f9362806fe3c75fe7b54508a2259df6522e6b [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne
Copy link
Member

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS Mac Catalina (10.15) passed 💻

All tests on macOS Mac Catalina (10.15) passed.

Pipeline on Agent
Hash: 54246cb1365b40cc8f5eb163d688268295616831 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

📚 [PR Build] Artifacts 📚

Packages generated

View packages

Pipeline on Agent XAMBOT-1035.Monterey
Hash: 54246cb1365b40cc8f5eb163d688268295616831 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

✅ API diff for current PR / commit

Legacy Xamarin (No breaking changes)
  • iOS (no change detected)
  • tvOS (no change detected)
  • watchOS (no change detected)
  • macOS (no change detected)
NET (empty diffs)
  • iOS: (empty diff detected)
  • tvOS: (empty diff detected)
  • MacCatalyst: (empty diff detected)
  • macOS: (empty diff detected)

✅ API diff vs stable

Legacy Xamarin (No breaking changes)
.NET (No breaking changes)
Legacy Xamarin (stable) vs .NET

✅ Generator diff

Generator diff is empty

Pipeline on Agent
Hash: 54246cb1365b40cc8f5eb163d688268295616831 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

💻 [PR Build] Tests on macOS M1 - Mac Big Sur (11.5) passed 💻

All tests on macOS M1 - Mac Big Sur (11.5) passed.

Pipeline on Agent
Hash: 54246cb1365b40cc8f5eb163d688268295616831 [PR build]

@vs-mobiletools-engineering-service2
Copy link
Collaborator

❌ [CI Build] Tests failed on VSTS: simulator tests iOS ❌

Tests failed on VSTS: simulator tests iOS.

Test results

1 tests failed, 147 tests passed.

Failed tests

  • fsharp/watchOS 32-bits - simulator/Debug: Crashed

Pipeline on Agent XAMBOT-1023.Monterey'
Merge 54246cb into e3bc284

@rolfbjarne rolfbjarne added community Community contribution ❤ performance If an issue or pull request is related to performance labels Jun 22, 2022
@rolfbjarne
Copy link
Member

Test failure is unrelated (https://github.com/xamarin/maccore/issues/2558).

@rolfbjarne rolfbjarne merged commit 7995740 into dotnet:main Jun 22, 2022
@spouliot spouliot deleted the cache-nsobject-conformstoprotocol branch June 22, 2022 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Community contribution ❤ performance If an issue or pull request is related to performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[net6/Xamarin.iOS] NSObject.ConformsToProtocol is very costly and invoked very often
6 participants