|
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).
|