v23-0001-tsm_system_time-Avoid-repeated-calling-of-INSTR_.patch

application/octet-stream

Filename: v23-0001-tsm_system_time-Avoid-repeated-calling-of-INSTR_.patch
Type: application/octet-stream
Part: 0
Message: Re: Reduce timing overhead of EXPLAIN ANALYZE using rdtsc?

Patch

Same data as JSON: GET /api/v1/attachments/:id/patch the parsed metadata as JSON — format, series position, per-file stats; never the diff bytes. API reference →
Format: format-patch
Series: patch v23-0001
Subject: tsm_system_time: Avoid repeated calling of INSTR_TIME_GET_MILLISEC
File+
contrib/tsm_system_time/tsm_system_time.c 7 6
From 471667369f2b30f60c4b9d1b950da5c1949e1bd7 Mon Sep 17 00:00:00 2001
From: Lukas Fittl <lukas@fittl.com>
Date: Tue, 7 Apr 2026 23:32:39 -0700
Subject: [PATCH v23] tsm_system_time: Avoid repeated calling of
 INSTR_TIME_GET_MILLISEC

The end time check in system_time_nextsampleblock unnecessarily converted
the internal ticks counter to a time value every time a block is returned,
which got slightly more expensive with 0022622c93d9. Instead, set a
target end time when we initialize, and compare against that whilst
staying in instr_time, through use of INSTR_TIME_GT.

Per report from buildfarm member drongo.

Author: Lukas Fittl <lukas@fittl.com>
Reviewed-by:
Discussion:
---
 contrib/tsm_system_time/tsm_system_time.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/contrib/tsm_system_time/tsm_system_time.c b/contrib/tsm_system_time/tsm_system_time.c
index 4814c31bc6f..4413d1c48da 100644
--- a/contrib/tsm_system_time/tsm_system_time.c
+++ b/contrib/tsm_system_time/tsm_system_time.c
@@ -47,7 +47,7 @@ typedef struct
 {
 	uint32		seed;			/* random seed */
 	double		millis;			/* time limit for sampling */
-	instr_time	start_time;		/* scan start time */
+	instr_time	end_time;		/* sampling end time */
 	OffsetNumber lt;			/* last tuple returned from current block */
 	BlockNumber doneblocks;		/* number of already-scanned blocks */
 	BlockNumber lb;				/* last block visited */
@@ -205,7 +205,7 @@ system_time_beginsamplescan(SampleScanState *node,
 	sampler->millis = millis;
 	sampler->lt = InvalidOffsetNumber;
 	sampler->doneblocks = 0;
-	/* start_time, lb will be initialized during first NextSampleBlock call */
+	/* end_time, lb will be initialized during first NextSampleBlock call */
 	/* we intentionally do not change nblocks/firstblock/step here */
 }
 
@@ -248,9 +248,11 @@ system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
 			sampler->step = random_relative_prime(sampler->nblocks, &randstate);
 		}
 
-		/* Reinitialize lb and start_time */
+		/* Reinitialize lb and compute end_time */
 		sampler->lb = sampler->firstblock;
-		INSTR_TIME_SET_CURRENT(sampler->start_time);
+		INSTR_TIME_SET_CURRENT(sampler->end_time);
+		INSTR_TIME_ADD_NANOSEC(sampler->end_time,
+							   sampler->millis * NS_PER_MS);
 	}
 
 	/* If we've read all blocks in relation, we're done */
@@ -259,8 +261,7 @@ system_time_nextsampleblock(SampleScanState *node, BlockNumber nblocks)
 
 	/* If we've used up all the allotted time, we're done */
 	INSTR_TIME_SET_CURRENT(cur_time);
-	INSTR_TIME_SUBTRACT(cur_time, sampler->start_time);
-	if (INSTR_TIME_GET_MILLISEC(cur_time) >= sampler->millis)
+	if (!INSTR_TIME_GT(sampler->end_time, cur_time))
 		return InvalidBlockNumber;
 
 	/*
-- 
2.47.1