I realize this is a somewhat general question, but bear with me. Suppose that I have to insert a bunch of items into a data structure.
- I can either insert them in one at a time, or in one giant batch.
- The insert operation must take a lock on my data structure.
- The insertion happens from one thread.
- I don’t have any information about contention for the lock from other parts of the system.
Do you think it would be better to insert in batches or one-by-one or it doesn’t matter (…where “better” minimizes insertion time and lock hold time)?
If only one thread has access to your data structure, then you don’t need the lock at all.
If multiple threads need access then it’s a matter of how large is the “dosage”: