Skip to content

Commit 2de9e38

Browse files
committed
Skip Topological sorting if not required
1 parent 8421141 commit 2de9e38

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/NHibernate/Loader/JoinWalker.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ protected virtual SelectMode GetSelectMode(string path)
204204
return SelectMode.Undefined;
205205
}
206206

207+
/// <summary>
208+
/// Returns null if sorting is not required, otherwise list of indexes in sorted order
209+
/// </summary>
207210
private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
208211
{
212+
if (!fields.Exists(a => a.DependsOn?.Length > 0))
213+
return null;
214+
209215
TopologicalSorter g = new TopologicalSorter(fields.Count);
210216
Dictionary<string, int> indexes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
211217

@@ -799,16 +805,9 @@ protected SqlString MergeOrderings(string ass, string orderBy) {
799805
/// </summary>
800806
protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associations)
801807
{
802-
IList<OuterJoinableAssociation> sortedAssociations = new List<OuterJoinableAssociation>();
803-
804-
var indices = GetTopologicalSortOrder(_dependentAliases);
805-
for (int index = indices.Length - 1; index >= 0; index--)
806-
{
807-
sortedAssociations.Add(associations[indices[index]]);
808-
}
809-
810808
JoinFragment outerjoin = Dialect.CreateOuterJoinFragment();
811809

810+
var sortedAssociations = GetSortedAssociations(associations);
812811
OuterJoinableAssociation last = null;
813812
foreach (OuterJoinableAssociation oj in sortedAssociations)
814813
{
@@ -840,6 +839,21 @@ protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associati
840839
return outerjoin;
841840
}
842841

842+
private IList<OuterJoinableAssociation> GetSortedAssociations(IList<OuterJoinableAssociation> associations)
843+
{
844+
var indexes = GetTopologicalSortOrder(_dependentAliases);
845+
if (indexes == null)
846+
return associations;
847+
848+
var sortedAssociations = new List<OuterJoinableAssociation>(associations.Count);
849+
for (int index = indexes.Length - 1; index >= 0; index--)
850+
{
851+
sortedAssociations.Add(associations[indexes[index]]);
852+
}
853+
854+
return sortedAssociations;
855+
}
856+
843857
/// <summary>
844858
/// Count the number of instances of IJoinable which are actually
845859
/// also instances of ILoadable, or are one-to-many associations

0 commit comments

Comments
 (0)