We have observed two performance improvements for set related expression:
- Use unordered sets instead of ordered sets
- Cache constant sets to avoid the set building
These two optimizations have been implemented for $setIsSubset expression, but not for other expressions.
One problem for porting these optimization to other set expression is using unordered sets causes the set element order being non deterministic. These can fail the fuzzer tests also may result in cascading non-deterministic expression values. (e.g. $group on $setIntersection). Note that this is not a problem for $setIsSubset expression since it's of boolean value. See related tickets https://jira.mongodb.org/browse/SERVER-58875 and https://jira.mongodb.org/browse/TIG-3254 for some context.
We have the following potential proposals:
- Sort the set expression values before return to ensure a consistent order
- Let the expression be non-deterministic, fix the fuzzer by either not generating set expressions or by other more complicated mechanisms.