[JAVA-342] On INSERT a List of documents should NOT be converted to a raw Array Created: 01/May/11  Updated: 26/Nov/12  Resolved: 13/Sep/12

Status: Closed
Project: Java Driver
Component/s: Performance
Affects Version/s: 2.5.3
Fix Version/s: 2.10.0

Type: Improvement Priority: Major - P3
Reporter: Anatoly Polinsky Assignee: Jeffrey Yemin
Resolution: Done Votes: 0
Labels: insert, performance
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

N/A



 Description   

When inserting a list of documents via DBCollection.insert( List<DBObject> list ), this List gets converted into an Array:

public WriteResult insert(List<DBObject> list) throws MongoException

{ return insert( list.toArray( new DBObject[list.size()] ) , getWriteConcern() ); }

Later on, when it gets to the real implementation of the insert in DBApiLayer.MyCollection.insert, each element of this array is inserted into BSONEncoder ( via OutMessage ). Further this array is never used.

So, it seems that there is no real benefit to convert the initial List into an Array. And for a performance sake, it should be left alone as a List.

For example, in my tests, for a 1,000,000 documents 665 bytes each, "list.toArray( new DBObject[list.size()] )" takes 40 milliseconds. While it may not seem like a lot, it all can add up given a bigger document size / more documents / etc.. And, unless I missed something, there is no good reason for this conversion in the first place.

/Anatoly



 Comments   
Comment by auto [ 13/Sep/12 ]

Author:

{u'date': u'2012-09-04T11:26:46-07:00', u'email': u'jeff.yemin@10gen.com', u'name': u'Jeff Yemin'}

Message: JAVA-342: Converting array to List instead of vice versa using Arrays.asList(T ...), to avoid double copy
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/c2a691f9111bd01ee77affd52f99c1ab0bbcd87b

Comment by Jeffrey Yemin [ 28/Aug/12 ]

Instead of converting List<DBObject> to DBObject[], convert DBObject[] to List<DBObject> using Arrays.asList(T ... a), which is a shallow copy.

Generated at Thu Feb 08 08:52:03 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.