- 
    Type:Improvement 
- 
    Resolution: Fixed
- 
    Priority:Major - P3 
- 
    Affects Version/s: None
- 
    Component/s: None
- 
        Query Integration
- 
        Fully Compatible
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
- 
        None
I would like to refactor two things for the bounded sort optimization logic:
- While working on SERVER-90833, I noticed that we use dynamic_cast repeatedly inside a for loop to do bounded sort optimization here. If a DocumentSource is a $project / $addFields / $replaceRoot, it will go through checking if it's a sort, then $match and then $unpack and then $project. AFAIK, dynamic_cast requires rtti and inheritance-hierarchy traversal. It may affect short-running query's performances which may spend more optimization time than running time. It would be better to introduce a new getType() method to DocumentSource and we can do switch (source->getType()). Same comment on other places to use dynamic_cast for DocumentSource concrete types. Especially this place. A POC for this can be found here. We should do perf profiling for this change.
- The bounded sort optimization logic is a really big chunk of PipelineD::buildInnerQueryExecutorGeneric() and it spans a lot of lines and it makes PipelineD::buildInnerQueryExecutorGeneric() harder to understand. It would be cleaner to define a separate class or function for the bounded sort optimization to separate concerns.