[CSHARP-2504] Reduce memory consumption because of lambda closures in BsonWriter.WriteName Created: 06/Feb/19  Updated: 28/Oct/23  Resolved: 07/Jan/21

Status: Closed
Project: C# Driver
Component/s: Performance, Serialization
Affects Version/s: 2.7.3
Fix Version/s: 2.12.0

Type: Improvement Priority: Major - P3
Reporter: Daniel Hegener Assignee: Boris Dogadov
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: PNG File image-2019-02-06-13-41-14-545.png     PNG File image-2019-02-06-14-13-47-136.png    
Issue Links:
Duplicate
is duplicated by CSHARP-1942 Reducing delegate allocations Closed

 Description   

The profiler output of a session that involved serialization of more than 2 million complex documents shows the following result - 36GB of memory allocated only by the BsonWriter.WriteName method:

The culprit for that appears to be the lambda expression here: https://github.com/mongodb/mongo-csharp-driver/blob/dc2dbb5ff06fdea80b44a8c2295e542f166ccb16/src/MongoDB.Bson/IO/BsonWriter.cs#L279

_childElementNameValidatorFactory = () => _elementNameValidator.GetValidatorForChildContent(name);

It would certainly be beneficial to move to an alternative design that does not result in closures.



 Comments   
Comment by Boris Dogadov [ 07/Jan/21 ]

Thanks onyxmaster and dnickless for finding and submitting PRs for this issue.
Fixed here with an equivalent approach.

Comment by Githook User [ 07/Jan/21 ]

Author:

{'name': 'Boris', 'email': 'boris.dogadov@mongodb.com', 'username': 'BorisDog'}

Message: CSHARP-2504: BsonWriter: _childElementNameValidatorFactory removal
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/7c3c5caf53366c16df1bf0108e20b674c0b5647f

Comment by Aristarkh Zagorodnikov [ 07/Dec/20 ]

I wonder why would you close an older issue, CSHARP-1942, (from 2017) as a duplicate of a newer one (from 2019) instead of vice versa, even when the newer one contains neither a better description of the issue nor a better solution (the PR in the second issue does not deal with the root of the issue, the delegate could be removed altogether), while also not covering one of the issues (see BsonSerializerRegistry).

Comment by Daniel Hegener [ 20/Feb/19 ]

PR https://github.com/mongodb/mongo-csharp-driver/pull/366

Comment by Daniel Hegener [ 06/Feb/19 ]

Here is another screenshot of the same profiling session showing another area where the current pattern allocates memory:

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