[CSHARP-1066] Change BsonElement from a class to a struct Created: 16/Sep/14  Updated: 02/Apr/15  Resolved: 02/Dec/14

Status: Closed
Project: C# Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 2.0

Type: Improvement Priority: Minor - P4
Reporter: Robert Stam Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Minor Change

 Description   

If BsonElement were a struct instead of a class then BsonDocument could use less memory (fewer heap objects). BsonDocument stores the elements in a List:

private List<BsonElement> _elements;

Currently, BsonElement is a class, so a document with N elements uses N + 1 heap objects for the elements (1 for the List and one for each element). If BsonElement were a struct it would use only 1 heap object (for the List), because as a struct the memory for all the elements would be internal to the List (true for all Lists of structs).

This change is potentially backward breaking for anyone who has written code that uses elements directly. In particular, as a struct we would want the BsonElement class to be immutable, which means we would remove the ability to change the value of an existing element.

We would appreciate comments from our users letting us know if this small breaking change would affect you or not.



 Comments   
Comment by Robert Stam [ 02/Dec/14 ]

When we say you can't change the value of an existing element, that doesn't mean you can't modify BsonDocuments.

You can do this:

document[1] = value;

What you can't do any more (because BsonElement is now immutable) is:

document.GetElement(1).Name = name;
document.GetElement(1).Value = value;

Comment by Githook User [ 02/Dec/14 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@robertstam.org'}

Message: CSHARP-1066: Change BsonElement from a class to a struct.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/9a049d53e7967af04a8c85be6205a92543804433

Generated at Wed Feb 07 21:38:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.