[CSHARP-1064] Have pre-created BsonValue instances for common values Created: 16/Sep/14  Updated: 20/Jan/16  Resolved: 26/May/15

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

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


 Description   

We could reduce pressure on the GC by having pre-created instances of many commonly used BsonValues. For example:

BsonBoolean: true, false
BsonDouble, -100.0 to 100.0 in 1.0 increments
BsonInt32/BsonInt64: -100 to 100
BsonString: empty string

The way these would be taken advantage of would be in the implicit conversions from primitive types to BsonValues. For example:

BsonValue a = true; // implicit conversion returns existing BsonBoolean instance for True
BsonValue b = 10.0; // implicit conversion returns existing BsonDouble instance for 10.0
BsonValue c = -1; // implicit conversion returns existing BsonInt32 instance for -1



 Comments   
Comment by Githook User [ 26/May/15 ]

Author:

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

Message: CSHARP-1064: Modified serializers (and several other places where BsonValues are created) to use implicit conversions so that they can take advantage of pre-created instances.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/427e417e7995af64ba49871ebb8208e31a049ba4

Comment by Githook User [ 26/May/15 ]

Author:

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

Message: CSHARP-1064: Have pre-created BsonValue instances for common values.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/01c34eb20b3e813d39ccede6eede2d33ad918be4

Comment by Roman Kuzmin [ 17/Sep/14 ]

I do not think it is harmful but I do not expect significant improvements either. It is difficult to tell without benchmarks. And proper benchmarks which reflect practical use cases are not that easy.

Comment by Robert Stam [ 17/Sep/14 ]

The idea with numbers is that certain constants appear frequently. Particularly 0 and 1 (0.0 and 1.0 for doubles). But it is quite cheap to extend the concept to a few hundred values (e.g. -100 to 100) as long as they are consecutive, so we don't have to do a dictionary lookup.

Why do you say this approach is not necessarily more effective? Do you think it would be harmful in any way?

Comment by Roman Kuzmin [ 17/Sep/14 ]

It looks reasonable perhaps for bool and an empty string but for numbers it is rather odd, especially for double. And this approach is not necessarily more effective.

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