From d584cc33669c036b10b23c67f2ea6a7641abfee7 Mon Sep 17 00:00:00 2001
From: Peter Geoghegan <pg@bowt.ie>
Date: Wed, 22 Jan 2020 20:35:04 -0800
Subject: [PATCH v5 2/2] Inline _bt_compare().

---
 src/backend/access/nbtree/nbtsearch.c | 29 +++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)

diff --git a/src/backend/access/nbtree/nbtsearch.c b/src/backend/access/nbtree/nbtsearch.c
index 98642cbccb..869e779b84 100644
--- a/src/backend/access/nbtree/nbtsearch.c
+++ b/src/backend/access/nbtree/nbtsearch.c
@@ -28,6 +28,8 @@ static void _bt_drop_lock_and_maybe_pin(IndexScanDesc scan, BTScanPos sp);
 static OffsetNumber _bt_binsrch(Relation rel, BTScanInsert key, Buffer buf);
 static int	_bt_binsrch_posting(BTScanInsert key, Page page,
 								OffsetNumber offnum);
+static inline int32 _bt_compare_inl(Relation rel, BTScanInsert key, Page page,
+									OffsetNumber offnum, bool isleaf);
 static bool _bt_readpage(IndexScanDesc scan, ScanDirection dir,
 						 OffsetNumber offnum);
 static void _bt_saveitem(BTScanOpaque so, int itemIndex,
@@ -307,7 +309,8 @@ _bt_moveright(Relation rel,
 			continue;
 		}
 
-		if (P_IGNORE(opaque) || _bt_compare(rel, key, page, P_HIKEY) >= cmpval)
+		if (P_IGNORE(opaque) || _bt_compare_inl(rel, key, page, P_HIKEY,
+												P_ISLEAF(opaque)) >= cmpval)
 		{
 			/* step right one page */
 			buf = _bt_relandgetbuf(rel, buf, opaque->btpo_next, access);
@@ -355,6 +358,7 @@ _bt_binsrch(Relation rel,
 				high;
 	int32		result,
 				cmpval;
+	bool		isleaf;
 
 	page = BufferGetPage(buf);
 	opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -366,6 +370,7 @@ _bt_binsrch(Relation rel,
 
 	low = P_FIRSTDATAKEY(opaque);
 	high = PageGetMaxOffsetNumber(page);
+	isleaf = P_ISLEAF(opaque);
 
 	/*
 	 * If there are no keys on the page, return the first available slot. Note
@@ -399,7 +404,7 @@ _bt_binsrch(Relation rel,
 
 		/* We have low <= mid < high, so mid points at a real slot */
 
-		result = _bt_compare(rel, key, page, mid);
+		result = _bt_compare_inl(rel, key, page, mid, isleaf);
 
 		if (result >= cmpval)
 			low = mid + 1;
@@ -414,7 +419,7 @@ _bt_binsrch(Relation rel,
 	 * On a leaf page, we always return the first key >= scan key (resp. >
 	 * scan key), which could be the last slot + 1.
 	 */
-	if (P_ISLEAF(opaque))
+	if (isleaf)
 		return low;
 
 	/*
@@ -512,7 +517,7 @@ _bt_binsrch_insert(Relation rel, BTInsertState insertstate)
 
 		/* We have low <= mid < high, so mid points at a real slot */
 
-		result = _bt_compare(rel, key, page, mid);
+		result = _bt_compare_inl(rel, key, page, mid, true);
 
 		if (result >= cmpval)
 			low = mid + 1;
@@ -650,6 +655,18 @@ _bt_compare(Relation rel,
 			BTScanInsert key,
 			Page page,
 			OffsetNumber offnum)
+{
+	BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
+
+	return _bt_compare_inl(rel, key, page, offnum, P_ISLEAF(opaque));
+}
+
+static inline int32
+_bt_compare_inl(Relation rel,
+				BTScanInsert key,
+				Page page,
+				OffsetNumber offnum,
+				bool isleaf)
 {
 	TupleDesc	itupdesc = RelationGetDescr(rel);
 	BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
@@ -667,9 +684,9 @@ _bt_compare(Relation rel,
 
 	/*
 	 * Force result ">" if target item is first data item on an internal page
-	 * --- see NOTE above.
+	 * --- see NOTE above _bt_compare().
 	 */
-	if (!P_ISLEAF(opaque) && offnum == P_FIRSTDATAKEY(opaque))
+	if (!isleaf && offnum == P_FIRSTDATAKEY(opaque))
 		return 1;
 
 	itup = (IndexTuple) PageGetItem(page, PageGetItemId(page, offnum));
-- 
2.17.1

