-
I noticed that adding contacts with the library takes longer time compared to my previous implementation. Could it be that the library adds each individual contact with one query at a time instead of adding contacts in "chunks"?
I have numbered the "contentProviderOperations" (1. - 8.) to show that I have to do 8 operations to add a contact. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 17 replies
-
Hey! Yes, currently the API inserts each RawContact and any associated data rows as part of a single batch of operations. You can see this in https://github.com/vestrel00/contacts-android/blob/main/core/src/main/java/contacts/core/Insert.kt It looks something like this (pseudocode), for (rawContact in rawContacts) {
results[rawContact] = contentResolver.applyBatch(insertRawContactAndData(rawContact))
} This keeps the code clean and simple. One raw contact equals one batch of operations executed atomically by itself. Another benefit of doing it this way is that failure to insert one RawContact will not affect insertion of another RawContact. For example, if you are using the I wrote the API this way because the most typical usage of insertions is when the end-user is creating a RawContact. Typically, this is done one at a time in a full-screen activity where the end-user can fill in RawContact data such as name, organization, etc... I always try to balance code complexity vs performance WITH RESPECT TO the typical use case. In this case, I agree that your own insertion code is more performant than this library because it can potentially insert multiple RawContacts in a single batch. However, I don't really see that being a common use case. Also, if someone really wanted to perform a mass insertion of RawContacts (maybe 1,000 RawContacts), then they can still use this library. They can chunk the insertions into separate threads/coroutines so that multiple insertions can occur at the same time. It is still not as performant as your own code but should do the trick.
Could you provide some real numbers, in milliseconds? How much faster are we talking about? Could you provide some benchmarks of inserting 15 RawContacts using your own code and also using this library? When performing benchmarks, please insert the same amount of data per raw contact to keep the speed comparison fair. If the different in speed is less than 15ms (1ms per RawContact) or some arbitrary low number, then I do not see value in pursuing this discussion further. Always think about the ROI (return on investment) 😁 |
Beta Was this translation helpful? Give feedback.
-
I also changed the title of the discussion to focus on just "insert" because you didn't mention deletion anywhere. Furthermore, the This |
Beta Was this translation helpful? Give feedback.
-
Aside from batching several different RawContacts in a single batch operation, I do see room for speed improvements when inserting RawContacts. Currently, for each RawContact, the Also, if there are GroupMemberships data to be inserted, the API will also internally perform several queries on Accounts and Groups to make sure that the memberships are valid (belonging to groups that actually exist for the particular chosen Account and that there are no duplicate memberships); https://github.com/vestrel00/contacts-android/blob/0.3.1/core/src/main/java/contacts/core/Insert.kt#L477 I could provide library users an option to disable those guards (but default to enabled) in order to speed up insertion of multiple RawContacts (still in separate batch operations). The two things I mentioned above might be more of a contributing factor to why you are seeing a noticeable difference in speed in your code vs this library (more than because I'm performing inserts of RawContacts and associated data in separate batches). I'll create issues for these later today and work on them ASAP. Once I complete the issues, I will create an alpha tag of the library so that you can try out the new functions to see if that helps improve insertion speeds for your use case 😁 |
Beta Was this translation helpful? Give feedback.
@marrale, I'm excited to tell you that I have pushed up the alpha tag;
0.3.2-alpha1
!!Please feel free to use it. Just note that I will not publish a formal release for it. Everything in that tag and perhaps more will be incorporated in the upcoming 0.3.2 release.
Please use the following code snippet to insert large quantities of RawContacts in the fastest way possible when using this library,
Apart from the other optimizations I made, I also implemented your suggestion. Please use
commitInChunks
instead of