v48-0004-optimization.patch

text/x-patch

Filename: v48-0004-optimization.patch
Type: text/x-patch
Part: 4
Message: Re: [PATCH] Incremental sort (was: PoC: Partial sort)

Patch

Format: format-patch
Series: patch v48-0004
Subject: optimization
File+
src/backend/optimizer/path/pathkeys.c 25 0
From 6499501194b6e96782edd97805571dd390c0badc Mon Sep 17 00:00:00 2001
From: James Coleman <jtc331@gmail.com>
Date: Tue, 31 Mar 2020 08:27:20 -0400
Subject: [PATCH v48 4/7] optimization

---
 src/backend/optimizer/path/pathkeys.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)

diff --git a/src/backend/optimizer/path/pathkeys.c b/src/backend/optimizer/path/pathkeys.c
index 3b84feaf7b..71fb790d35 100644
--- a/src/backend/optimizer/path/pathkeys.c
+++ b/src/backend/optimizer/path/pathkeys.c
@@ -346,6 +346,30 @@ pathkeys_common_contained_in(List *keys1, List *keys2, int *n_common)
 	ListCell   *key1,
 			   *key2;
 
+	/*
+	 * See if we can avoiding looping through both lists. This optimization
+	 * gains us several percent in planning time in a worst-case test.
+	 */
+	if (keys1 == keys2)
+	{
+		*n_common = list_length(keys1);
+		return true;
+	}
+	else if (keys1 == NIL)
+	{
+		*n_common = 0;
+		return true;
+	}
+	else if (keys2 == NIL)
+	{
+		*n_common = 0;
+		return false;
+	}
+
+	/*
+	 * If both lists are non-empty, iterate through both to find out how many
+	 * items are shared.
+	 */
 	forboth(key1, keys1, key2, keys2)
 	{
 		PathKey    *pathkey1 = (PathKey *) lfirst(key1);
@@ -359,6 +383,7 @@ pathkeys_common_contained_in(List *keys1, List *keys2, int *n_common)
 		n++;
 	}
 
+	/* If we ended with a null value, then we've processed the whole list. */
 	*n_common = n;
 	return (key1 == NULL);
 }
-- 
2.17.1