Replies: 2 comments 6 replies
-
Since the engagement on this topic has been 'light' I thought I would post some of my findings and the exploration avenues I've taken over the past few days; TestingI've taken the approach of simply poking Realm with different scenarios to see how it reacts and measuring the time to initialise. I wanted to understand the cost of the 'magic' behind realm so we can take a more educated approach to optimising the start times, or at least know where to start looking. Lets start with some base measurements: Creating an empty project, importing Realm with SPM and simply calling
Model Structures
Database Size
Brief AnalysisThere are several interesting findings we can take from the results above:
ConclusionThere is a lot of 'magic' going on when initialising an instance of Realm, and a lot of it is opaque. We have found that as our use of Realm has grown (currently 254 models of varying complexity) it's contribution to the slow down of app launch has dramatically increased (note the times we are seeing reported from the app in the original post). When I started this journey I was looking for ways to optimise our use of Realm to speed up app launch times, but with these findings it's looking like we only have two viable options:
Next StepsMaybe we can be hopeful and find that there is a bug contributing to an increase in Realm If everything is working as expected then we will need to start exploring the alternate options mentioned above, or maybe there is something I've not yet considered so any suggestions would be great. But from our point of view the amount of time Realm is contributing to app startup times for our users is becoming a problem, especially for users on older/slower devices, so doing nothing, at least for us, isn't an option. Thanks for reading, I hope this was useful for someone! |
Beta Was this translation helpful? Give feedback.
-
Our benchmark for Realm init uses the schema for our test suite with ~400 classes and a total of ~6000 properties and on a iPhone Xs (4 generations old but far from a total potato) schema init takes ~100ms, creating a new Realm file takes 10ms, and then reopening an existing file (with no data, but we generally don't touch any of the actual data while opening) takes 5ms. This isn't wildly different from the numbers you're seeing, but it is faster. Can you maybe share some of the model definitions you're testing with? There's a few performance traps related to them (e.g. Migrations are not run at all unless you change the schema version in the config. A no-op migration does have a bit of overhead, so if you're doing something like bumping the schema version with every release even if the schema hasn't changed then you may want to avoid doing that. When a migration does run most of the time spent is usually going to be in whatever you write in your migration block; obviously if you loop over all of the objects in your Realm and transform them, that can take a while if you have a lot of data. The schema changes which have significant potential to be slow even without you writing something in the migration block that does a lot of work are changing a property from non-optional to optional and adding an index to an existing property. If you do have slow migrations and don't want to have a slow startup even when the migration happens, I think there inherently isn't really any solution other than deferring the point in app startup where you need to read from the realm. You can use Enabling compact on launch can obviously have a significant effect on your startup time, but you appear to not be using that. |
Beta Was this translation helpful? Give feedback.
-
Overview
We are currently doing an investigation in to our app startup times and are looking to improve them where we can. We have added some logging in and around this area and have noted that the realm migration/initialisation is taking quite a significant amount (80-90%) of that time (anywhere from 0.4-4 seconds for the 90th percentile & 0.25-3.2 seconds for the 50th percentile).
I've been looking around to see how our results compare to others out in the wild and I've not found much in the way of data to compare to. I've also looked around for strategies to improve initialisation/migration performance and again can't find much out there.
What I'm looking for is either some strategies to improve initialisation/migration performance (or just the performance of realm in general) and any metrics as to what 'good' looks like.
Implementation Details
Our migration currently has 18 migration steps which are locked behind version checks e.g:
*This is greatly simplified but encapsulates the general process
For most users most of the time these migration steps are no-op. We release once per week which could be a contributing factor, but most of the time we don't add any more migration steps. This could then be linked to initialisation, but I'm not sure what the contributing factors could be here without checking out realm and profiling it. Finally, our realm size on disk is typically <50mb on the high end, but normally much smaller.
I'm hoping someone with a bit more knowledge around realm (I've only been using it for a few weeks after starting a new job) can help, or at least point me in to the right direction towards some docs/articles. I don't mind if the suggestions are for far reaching changes like splitting up in to multiple realms (if that is even a recommendation), at least I know what I will be dealing with.
EDIT: For reference anything over 400ms to render the first frame is on the slow side in Apples eyes so anything we can do would be great.
Beta Was this translation helpful? Give feedback.
All reactions