I am experiencing a significant CPU spike under high concurrency when executing a simple AsQueryable projection using the MongoDB C# Driver.

After profiling the application and debugging the driver source code, I found that the bottleneck occurs in the BsonMemberMap.GetPropertySetter() method. Specifically, a LambdaExpression is being compiled every single time the query is executed, triggering excessive JIT activity.

BsonClassMap.RegisterClassMap<DynamicApplication>(cm => { cm.AutoMap(); }); BsonClassMap.RegisterClassMap<GetDbCodesRespDto>(cm => { cm.AutoMap(); }); var client = new MongoClient(connectionString); var db = client.GetDatabase("WebApplication"); var collection = db.GetCollection<DynamicApplication>("DynamicApplication"); var d = await collection.AsQueryable() .Where(p => p.Id == "003566cd-3a51-4a24-9842-c32b01db0600") .Select(p => new GetDbCodesRespDto { AppId = p.InternalId, DbCode = p.DbCode }) .ToListAsync();

The Issue: Even though the DTO structure and the projection logic remain constant, the driver seems to be re-compiling the IL for the property setters on every request.

MongoDB .NET Driver Version: 3.7.1

JIT Status

enter image description here

Z.Chen's user avatar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.