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

Use Core Data prefetching when StreamRuntimeCheck._isDatabasePrefetchingEnabled #3495

Merged
merged 7 commits into from
Nov 20, 2024

Conversation

laevandus
Copy link
Contributor

@laevandus laevandus commented Nov 18, 2024

🔗 Issue Links

Part of IOS-23

🎯 Goal

  • Add a feature flag for turning on data prefetching on the Core Data level

📝 Summary

StreamRuntimeCheck._isDatabasePrefetchingEnabled turns on setting:

  • returnsObjectsAsFaults to false
  • Populating relationshipKeyPathsForPrefetching

🛠 Implementation

There have been crash reports happening when Core Data is fulfilling a fault. Instruments shows that some of the fault related requests take very long time sometimes. If the runtime check is enabled, these slow requests are not reproducible anymore. The aim of PR is to add this feature flag and see if it remedies these issues. Moreover, it makes sense to use this configuration because most of our asModel() functions end up accessing all the data of the Core Data model (including many relationships). For such cases, Apple also recommends setting faulting to false and using prefetching (1, 2).

Tried our demo app and saw 25% reduction (4004 to 3034) of fault related requests (even when requests created by us have returnsObjectsAsFaults=false and relationship key paths set, it does remove all the fault related requests. Core Data internally makes many requests as well when merging contexts etc).

Example of the slow fault related request:
slow_fault

🧪 Manual Testing Notes

Set StreamRuntimeChecks._isDatabasePrefetchingEnabled = true and play around with the demo app. Does anything feel off?

☑️ Contributor Checklist

  • I have signed the Stream CLA (required)
  • This change should be manually QAed
  • Changelog is updated with client-facing changes
  • Changelog is updated with new localization keys
  • New code is covered by unit tests
  • Comparison screenshots added for visual changes
  • Affected documentation updated (docusaurus, tutorial, CMS)

@laevandus laevandus requested a review from a team as a code owner November 18, 2024 13:17
@laevandus laevandus added the 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK label Nov 18, 2024
Comment on lines +48 to +57
extension NSFetchRequestPrefetching where Self: NSManagedObject {
/// Turns off Core Data object faulting and sets prefetched relationship keypaths.
///
/// Note: Reduces additional Core Data fetches when most of the data is accessed from fetched objects.
static func applyPrefetchingState(to request: NSFetchRequest<Self>) {
guard StreamRuntimeCheck._isDatabasePrefetchingEnabled else { return }
request.returnsObjectsAsFaults = false
request.relationshipKeyPathsForPrefetching = Self.prefetchedRelationshipKeyPaths()
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method is not in the NSFetchRequest extension because extending generic Obj-C classes is complicated.

It would be nicer to write request.applyPrefetchingState(), but did not find any good way for it which enables each subclass to define its relationship key paths. Moreover, load methods in this file make it even more complicated.

Copy link

1 Message
📖 There seems to be app changes but CHANGELOG wasn't modified.
Please include an entry if the PR includes user-facing changes.
You can find it at CHANGELOG.md.

Generated by 🚫 Danger

@Stream-SDK-Bot
Copy link
Collaborator

SDK Size

title develop branch diff status
StreamChat 6.92 MB 6.96 MB +37 KB 🟢
StreamChatUI 4.95 MB 4.95 MB 0 KB 🟢

@Stream-SDK-Bot
Copy link
Collaborator

SDK Performance

target metric benchmark branch performance status
MessageList Hitches total duration 10 ms 11.68 ms -16.8% 🔽 🔴
Duration 2.6 s 2.54 s 2.31% 🔼 🟢
Hitch time ratio 4 ms per s 4.61 ms per s -15.25% 🔽 🔴
Frame rate 75 fps 78.45 fps 4.6% 🔼 🟢
Number of hitches 1 1.4 -40.0% 🔽 🔴

@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Nov 18, 2024

SDK Size

title develop branch diff status
StreamChat 6.99 MB 7.01 MB +21 KB 🟢
StreamChatUI 4.95 MB 4.95 MB 0 KB 🟢

Copy link
Member

@nuno-vieira nuno-vieira left a comment

Choose a reason for hiding this comment

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

LGTM ✅ Lets test drive this and see if there are any improvements 👍

Copy link
Contributor

@martinmitrevski martinmitrevski left a comment

Choose a reason for hiding this comment

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

What about the increase of hitches in the checks, do you think there might be a performance impact? Although, by default this thing is off, so weird that we get this regression, maybe something with the check itself @testableapple?
In any case, this should be tested a bit with the flag on.

@Stream-SDK-Bot
Copy link
Collaborator

SDK Performance

target metric benchmark branch performance status
MessageList Hitches total duration 10 ms 5.01 ms 49.9% 🔼 🟢
Duration 2.6 s 2.55 s 1.92% 🔼 🟢
Hitch time ratio 4 ms per s 1.97 ms per s 50.75% 🔼 🟢
Frame rate 75 fps 78.24 fps 4.32% 🔼 🟢
Number of hitches 1 0.6 40.0% 🔼 🟢

@laevandus laevandus added the 🤞 Ready For QA A PR that is Ready for QA label Nov 19, 2024
@testableapple testableapple added 🧪 QAing and removed 🤞 Ready For QA A PR that is Ready for QA labels Nov 19, 2024
@Stream-SDK-Bot
Copy link
Collaborator

Stream-SDK-Bot commented Nov 19, 2024

SDK Size

title develop branch diff status
StreamChat 7.02 MB 7.06 MB +36 KB 🟢
StreamChatUI 4.96 MB 4.96 MB 0 KB 🟢

@testableapple testableapple added 🟢 QAed A PR that was QAed and removed 🧪 QAing labels Nov 19, 2024
@laevandus laevandus enabled auto-merge (squash) November 20, 2024 07:49
@laevandus laevandus merged commit b5fa385 into develop Nov 20, 2024
13 checks passed
@laevandus laevandus deleted the feature/data-prefetching branch November 20, 2024 17:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🟢 QAed A PR that was QAed 🌐 SDK: StreamChat (LLC) Tasks related to the StreamChat LLC SDK
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants