Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 99c7541

Browse files
committed
Fix performance regression in tuplesort specializations
6974924 added 3 new qsort specialization functions aimed to improve the performance of sorting many of the common pass-by-value data types when they're the leading or only sort key. Unfortunately, that has caused a performance regression when sorting datasets where many of the values being compared were equal. What was happening here was that we were falling back to the standard sort comparison function to handle tiebreaks. When the two given Datums compared equally we would incur both the overhead of an indirect function call to the standard comparer to perform the tiebreak and also the standard comparer function would go and compare the leading key needlessly all over again. Here improve the situation in the 3 new comparison functions. We now return 0 directly when the two Datums compare equally and we're performing a 1-key sort. Here we don't do anything to help the multi-key sort case where the leading key uses one of the sort specializations functions. On testing this case, even when the leading key's values are all equal, there appeared to be no performance regression. Let's leave it up to future work to optimize that case so that the tiebreak function no longer re-compares the leading key over again. Another possible fix for this would have been to add 3 additional sort specialization functions to handle single-key sorts for these pass-by-value types. The reason we didn't do that here is that we may deem some other sort specialization to be more useful than single-key sorts. It may be impractical to have sort specialization functions for every single combination of what may be useful and it was already decided that further analysis into which ones are the most useful would be delayed until the v16 cycle. Let's not let this regression force our hand into trying to make that decision for v15. Author: David Rowley Reviewed-by: John Naylor Discussion: https://postgr.es/m/CA+hUKGJRbzaAOUtBUcjF5hLtaSHnJUqXmtiaLEoi53zeWSizeA@mail.gmail.com
1 parent 92e7a53 commit 99c7541

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/backend/utils/sort/tuplesort.c

+28-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,11 @@ struct Tuplesortstate
436436

437437
/*
438438
* This variable is shared by the single-key MinimalTuple case and the
439-
* Datum case (which both use qsort_ssup()). Otherwise it's NULL.
439+
* Datum case (which both use qsort_ssup()). Otherwise, it's NULL. The
440+
* presence of a value in this field is also checked by various sort
441+
* specialization functions as an optimization when comparing the leading
442+
* key in a tiebreak situation to determine if there are any subsequent
443+
* keys to sort on.
440444
*/
441445
SortSupport onlyKey;
442446

@@ -701,6 +705,13 @@ qsort_tuple_unsigned_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
701705
if (compare != 0)
702706
return compare;
703707

708+
/*
709+
* No need to waste effort calling the tiebreak function when there are
710+
* no other keys to sort on.
711+
*/
712+
if (state->onlyKey != NULL)
713+
return 0;
714+
704715
return state->comparetup(a, b, state);
705716
}
706717

@@ -713,9 +724,17 @@ qsort_tuple_signed_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
713724
compare = ApplySignedSortComparator(a->datum1, a->isnull1,
714725
b->datum1, b->isnull1,
715726
&state->sortKeys[0]);
727+
716728
if (compare != 0)
717729
return compare;
718730

731+
/*
732+
* No need to waste effort calling the tiebreak function when there are
733+
* no other keys to sort on.
734+
*/
735+
if (state->onlyKey != NULL)
736+
return 0;
737+
719738
return state->comparetup(a, b, state);
720739
}
721740

@@ -728,9 +747,17 @@ qsort_tuple_int32_compare(SortTuple *a, SortTuple *b, Tuplesortstate *state)
728747
compare = ApplyInt32SortComparator(a->datum1, a->isnull1,
729748
b->datum1, b->isnull1,
730749
&state->sortKeys[0]);
750+
731751
if (compare != 0)
732752
return compare;
733753

754+
/*
755+
* No need to waste effort calling the tiebreak function when there are
756+
* no other keys to sort on.
757+
*/
758+
if (state->onlyKey != NULL)
759+
return 0;
760+
734761
return state->comparetup(a, b, state);
735762
}
736763

0 commit comments

Comments
 (0)