PhoneNumberKit icon indicating copy to clipboard operation
PhoneNumberKit copied to clipboard

Memory consumption is higher than it needs to be

Open bmwalters opened this issue 5 years ago • 4 comments

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

  1. Run the PhoneBook sample app in a release build. Observe the memory usage the Xcode memory gauge shows at rest.
  2. Comment out the line that instantiates PhoneNumberKit in ViewController.swift, fix errors, and run again.
  3. 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.json into a Swift source file that is compiled along with the project, replacing the runtime JSON loading code in MetadataManager. This would likely also help with start-up time.

bmwalters avatar Jan 07 '20 18:01 bmwalters

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

kuyazee avatar Jan 08 '20 01:01 kuyazee

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.

github-actions[bot] avatar May 28 '22 03:05 github-actions[bot]

Hi All Is this issue fixed ?. Any update on this would really help.

deepesh-vasthimal-cko avatar Sep 07 '22 10:09 deepesh-vasthimal-cko

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.

bguidolim avatar Sep 07 '22 14:09 bguidolim

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.

github-actions[bot] avatar Nov 08 '22 03:11 github-actions[bot]