remove redundant check of item pointer

Started by Junwang Zhaoover 3 years ago3 messages
#1Junwang Zhao
zhjwpku@gmail.com
1 attachment(s)

In function ItemPointerEquals, the ItemPointerGetBlockNumber
already checked the ItemPointer if valid, there is no need
to check it again in ItemPointerGetOffset, so use
ItemPointerGetOffsetNumberNoCheck instead.

--
Regards
Junwang Zhao

Attachments:

v1-0001-remove-redundant-check-of-item-pointer.patchapplication/octet-stream; name=v1-0001-remove-redundant-check-of-item-pointer.patchDownload
From 77e8b91cf6b8facee1ee8f5e96df3ef3d68539fd Mon Sep 17 00:00:00 2001
From: Junwang Zhao <zhjwpku@gmail.com>
Date: Wed, 27 Apr 2022 16:08:50 +0800
Subject: [PATCH v1] remove redundant check of item pointer

In function ItemPointerEquals, the ItemPointerGetBlockNumber
already checked the ItemPointer if valid, there is no need
to check it again in ItemPointerGetOffset, so use
ItemPointerGetOffsetNumberNoCheck instead.

Signed-off-by: Junwang Zhao <zhjwpku@gmail.com>
---
 src/backend/storage/page/itemptr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/backend/storage/page/itemptr.c b/src/backend/storage/page/itemptr.c
index 9011337aa8..61ad727b1d 100644
--- a/src/backend/storage/page/itemptr.c
+++ b/src/backend/storage/page/itemptr.c
@@ -37,8 +37,8 @@ ItemPointerEquals(ItemPointer pointer1, ItemPointer pointer2)
 
 	if (ItemPointerGetBlockNumber(pointer1) ==
 		ItemPointerGetBlockNumber(pointer2) &&
-		ItemPointerGetOffsetNumber(pointer1) ==
-		ItemPointerGetOffsetNumber(pointer2))
+		ItemPointerGetOffsetNumberNoCheck(pointer1) ==
+		ItemPointerGetOffsetNumberNoCheck(pointer2))
 		return true;
 	else
 		return false;
-- 
2.33.0

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Junwang Zhao (#1)
Re: remove redundant check of item pointer

Junwang Zhao <zhjwpku@gmail.com> writes:

In function ItemPointerEquals, the ItemPointerGetBlockNumber
already checked the ItemPointer if valid, there is no need
to check it again in ItemPointerGetOffset, so use
ItemPointerGetOffsetNumberNoCheck instead.

I do not think this change is worth making. The point of
ItemPointerGetOffsetNumberNoCheck is not to save some cycles,
it's to be able to fetch the offset field in cases where it might
validly be zero. The assertion will be compiled out anyway in
production builds --- and even in assert-enabled builds, I'd kind
of expect the compiler to optimize away the duplicated tests.

regards, tom lane

#3Junwang Zhao
zhjwpku@gmail.com
In reply to: Tom Lane (#2)
Re: remove redundant check of item pointer

got it, thanks for the explanation.

On Wed, Apr 27, 2022 at 11:34 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:

Junwang Zhao <zhjwpku@gmail.com> writes:

In function ItemPointerEquals, the ItemPointerGetBlockNumber
already checked the ItemPointer if valid, there is no need
to check it again in ItemPointerGetOffset, so use
ItemPointerGetOffsetNumberNoCheck instead.

I do not think this change is worth making. The point of
ItemPointerGetOffsetNumberNoCheck is not to save some cycles,
it's to be able to fetch the offset field in cases where it might
validly be zero. The assertion will be compiled out anyway in
production builds --- and even in assert-enabled builds, I'd kind
of expect the compiler to optimize away the duplicated tests.

regards, tom lane

--
Regards
Junwang Zhao