[SERVER-32302] for-each loop over BSONObj is slower than explicit BSONObjIterator in MatchExpressionParser code Created: 13/Dec/17  Updated: 30/Oct/23  Resolved: 04/Jan/18

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 3.6.0
Fix Version/s: 3.6.7, 3.7.1

Type: Improvement Priority: Major - P3
Reporter: Kyle Suarez Assignee: Mathias Stearn
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Duplicate
is duplicated by SERVER-17974 speed up BSONObjIterator and BSONObj:... Closed
Problem/Incident
is caused by SERVER-30761 Optimize parsing code for top-level M... Closed
Backwards Compatibility: Fully Compatible
Backport Requested:
v3.6
Sprint: Platforms 2018-01-01, Platforms 2018-01-15
Participants:
Linked BF Score: 0

 Description   

As a part of SERVER-30761, we refactored parseInExpression() and changed a loop that looked like

BSONObjIterator iter(theArray);
while (iter.more()) {
    auto e = iter.next();
    // ...
}

into

for (auto e : theArray) {
    // ...
}

This has caused a performance regression of roughly 5% when parsing a $in, and is apparent with very large in expressions. The real fix seems like achieving performance parity between the two ways to iterate through a BSONObj, but I'm not sure how much we want to dive down the rabbit hole in terms of seeing the machine code that's generated by the two constructs.



 Comments   
Comment by Githook User [ 23/Jul/18 ]

Author:

{'username': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Make mutablebson cache total field size

This addresses the perf regression in update due to eagerly computing the
field size in BSONElement's constructor.

(cherry picked from commit d30abf7c53a992635d82b226acbb649106f5dfa2)
Branch: v3.6
https://github.com/mongodb/mongo/commit/468ea92542149779247925bc56f6b340246c4329

Comment by Githook User [ 23/Jul/18 ]

Author:

{'username': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Use a table to compute the size of each BSONElement

(cherry picked from commit 3c586a7c946ac7f9485b9ff978fa029ac4b5108e)
Branch: v3.6
https://github.com/mongodb/mongo/commit/503a10907758b19a66c7e3bc06aedc32ecae5dcb

Comment by Githook User [ 23/Jul/18 ]

Author:

{'username': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Compute BSONElement sizes eagerly

(cherry picked from commit 0d38ef54970744aca3565fabbda76bd6e7836f7a)
Branch: v3.6
https://github.com/mongodb/mongo/commit/ca6228daa963e5c0ea5df680206f82dd73eb2fd2

Comment by Githook User [ 23/Jul/18 ]

Author:

{'username': 'RedBeard0531', 'name': 'Mathias Stearn', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Introduce BSONObjStlIterator as a proper stl-style iterator

(cherry picked from commit 1497899f237dd60ad7313913c38c3f07fe168f2b)
Branch: v3.6
https://github.com/mongodb/mongo/commit/e64cf49efa5410aafdbf1a837070563abcf41fb2

Comment by Githook User [ 04/Jan/18 ]

Author:

{'name': 'Mathias Stearn', 'username': 'RedBeard0531', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Make mutablebson cache total field size

This addresses the perf regression in update due to eagerly computing the
field size in BSONElement's constructor.
Branch: master
https://github.com/mongodb/mongo/commit/d30abf7c53a992635d82b226acbb649106f5dfa2

Comment by Githook User [ 04/Jan/18 ]

Author:

{'name': 'Mathias Stearn', 'username': 'RedBeard0531', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Use a table to compute the size of each BSONElement
Branch: master
https://github.com/mongodb/mongo/commit/3c586a7c946ac7f9485b9ff978fa029ac4b5108e

Comment by Githook User [ 04/Jan/18 ]

Author:

{'name': 'Mathias Stearn', 'username': 'RedBeard0531', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Compute BSONElement sizes eagerly
Branch: master
https://github.com/mongodb/mongo/commit/0d38ef54970744aca3565fabbda76bd6e7836f7a

Comment by Githook User [ 04/Jan/18 ]

Author:

{'name': 'Mathias Stearn', 'username': 'RedBeard0531', 'email': 'mathias@10gen.com'}

Message: SERVER-32302 Introduce BSONObjStlIterator as a proper stl-style iterator
Branch: master
https://github.com/mongodb/mongo/commit/1497899f237dd60ad7313913c38c3f07fe168f2b

Comment by Ian Whalen (Inactive) [ 15/Dec/17 ]

Passing to Platforms to investigate the core cause of why the foreach loop is slower.

Generated at Thu Feb 08 04:29:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.