[SERVER-72661] Optimize SBE stage builder type checking for $add Created: 10/Jan/23  Updated: 29/Oct/23  Resolved: 12/Jan/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.3.0-rc0

Type: Improvement Priority: Major - P3
Reporter: Ivan Fefer Assignee: Ivan Fefer
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
Backwards Compatibility: Fully Compatible
Sprint: QE 2023-01-23
Participants:
Linked BF Score: 35

 Description   

Currently SBE stage builder for $add generates code with a lot of redundant isDate checks because we are allowed to have Date in the list of arguments, but only one and there is no obvious way to check that.

It looks like we can do it in two passes with only one isDate and isNumber check per argument.

First pass:

Let's create a local variable that will store the index of the first argument that is a Date or -1 if there is no dates.

Let <dateIndex> =
if <isDate(argument 0)> then 0
else if <isDate(argument 1) then 1
else ...
else if <isDate(argument n-1)> then n - 1
else
-1. 

Second pass:
Each argument, except for the date found in previous pass must be a Number.

if
<0 != dateIndex and argument 0 is not a Number> or 
<1 != dateIndex and argument 1 is not a Number> or 
...
<n-1 != dateIndex and argument n-1 is not a Number> 
then error 

After that we can just add all the arguments without extra type checking.

Performance improvement should be visible in sbe_expression_bm (I hope).



 Comments   
Comment by Githook User [ 11/Jan/23 ]

Author:

{'name': 'Ivan Fefer', 'email': 'ivan.fefer@mongodb.com', 'username': 'Fefer-Ivan'}

Message: SERVER-72661 Optimize $add type checking in SBE
Branch: master
https://github.com/mongodb/mongo/commit/47da4bd45b898a350f0b7142451f1a6cfef2f496

Generated at Thu Feb 08 06:22:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.