On Wed, 16 Oct 2013 11:12:47 GMT, Jack Applegame wrote:

On Wed, 16 Oct 2013 12:58:43 +0200, Sönke Ludwig wrote:

However, when implemented, it will not be a particularly efficient
operation (the whole BSON object needs to be written again into a new
buffer, which is O(n) + an allocation). Better is to make such
modifications on an AA before converting to BSON.

What about efficiency of adding new fields?
Is this code:

Bson obj = serializeToBson([
    "field1": serializeToBson(value1),
    "field2": serializeToBson(value2)
]);

more efficient then:

Bson obj = Bson.emptyObject;
obj.field1 = serializeToBson(value1);
obj.field2 = serializeToBson(value2);

For many fields the first variant will (ideally*) be faster because for every field assignment obj.field = ...; the whole object needs to be iterated through first to see if the key already exists, so that's O(n²) for n fields added. The AA will have O(1) insertion costs most of the time, making it O(n). For just two fields the difference will not be so significant, though, or may even be in favor of variant #2.

It has to be said that constructing BSON objects in general (and serializeToBson in particular) still needs to be tuned for performance. But the insertion complexity will always stay as bad it is because of the internal data representation, which is optimized for fast and allocation free BSON decoding.

* Due to the current implementation of serializeToBson, the first variant will always be slower. But this will be improved resulting in the expected O(n) complexity.