PhoneNumberKit
PhoneNumberKit copied to clipboard
Memory consumption is higher than it needs to be
Overview
The file PhoneNumberMetadata.json is 333KB. I would thus consider this a reasonable amount of memory for PhoneNumberKit to consume when loaded into memory. Instead, instantiating a PhoneNumberKit object consumes 2MB of memory. This can be a problem in resource-constrained environments such as app extensions.
How to reproduce
- Run the PhoneBook sample app in a release build. Observe the memory usage the Xcode memory gauge shows at rest.
- Comment out the line that instantiates
PhoneNumberKitinViewController.swift, fix errors, and run again. - Observe that the memory usage has decreased by ~2MB.
I tried using the memory debugging command line tools to get better info without success.
Possible cause
It could be that the MetadataTerritory structs within MetadataManager are copied more than is needed.
An experiment that supports this is this experimental patch, which changes MetadataManager to refer to territories within the main territories array by index instead of including the struct in the hash tables.
With this patch, following the above steps shows only a ~1MB increase in memory usage instead of ~2MB.
Possible solutions
- Optimize MetadataManager as in the above patch.
- Code generation solution (e.g. Swiftgen) that processes
PhoneNumberMetadata.jsoninto a Swift source file that is compiled along with the project, replacing the runtime JSON loading code inMetadataManager. This would likely also help with start-up time.
It could be that the MetadataTerritory structs within MetadataManager are copied more than is needed.
What if we changed the MetadataTerritory to a class so memory references will be used instead. Everything inside it is immutable anyway maybe that can also work
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hi All Is this issue fixed ?. Any update on this would really help.
Hi all!
This is not fixed, and I was not aware of this.
It's an interesting topic to investigate, I'll definitely take a look. Or if someone wants to open a PR for this, I'll be happy to review it.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.