Minor optimization in tuplesort_skiptuples_mk for in memory non unique
tuple store which just advances current position by N instead of getting
tuple N times.
Author: Max Yang
@@ -1728,7 +1728,7 @@ tuplesort_getdatum_mk(Tuplesortstate_mk *state, bool forward,
bool
tuplesort_skiptuples_mk(Tuplesortstate_mk *state, int64 ntuples, bool forward)
{
- MemoryContext oldcontext = MemoryContextSwitchTo(state->sortcontext);
+ MemoryContext oldcontext;
bool fOK;
/*
@@ -1739,14 +1739,23 @@ tuplesort_skiptuples_mk(Tuplesortstate_mk *state, int64 ntuples, bool forward)
Assert(ntuples >= 0);
/*
- * GPDB_84_MERGE_FIXME: This simplistic implementation just calls
- * gettuple, even if the sort happened in memory.
- */
-
- /*
- * We could probably optimize these cases better, but for now it's
- * not worth the trouble.
+ * Optimize skip strategy only for in memory non unique store. But for unique store, we
+ * use normal one by one logic because we need to check the emptiness of entries.
*/
+ if (state->status == TSS_SORTEDINMEM && !state->mkctxt.unique)
+ {
+ TuplesortPos_mk *pos = &state->pos;
+ if (pos->current + ntuples <= state->entry_count)
+ {
+ pos->current += ntuples;
+ return true;
+ }
+ pos->eof_reached = true;
+ return false;
+ }
+ else
+ {
+ oldcontext = MemoryContextSwitchTo(state->sortcontext);
fOK = true;
while (ntuples-- > 0)
{
@@ -1765,8 +1774,8 @@ tuplesort_skiptuples_mk(Tuplesortstate_mk *state, int64 ntuples, bool forward)
}
MemoryContextSwitchTo(oldcontext);
-
- return true;
+ return fOK;
+ }
}
