From 43fcdbcdaee406b1d0eae71bd4a2b117d3acf416 Mon Sep 17 00:00:00 2001 From: Thomas Munro Date: Fri, 5 Apr 2024 15:06:32 +1300 Subject: [PATCH v10 4/4] Prefetch page header memory when streaming relations. read_stream.c can always see at least one page ahead of the one the caller is accessing. Take the opportunity to prefetch the cache line that holds the next page's header. For some scans, that can generate a decent speedup, though real world results will depend on how much work the CPU does before it gets around to accessing the next page. --- src/backend/storage/aio/read_stream.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/backend/storage/aio/read_stream.c b/src/backend/storage/aio/read_stream.c index 80b8be8cc7e..35ee1fc1043 100644 --- a/src/backend/storage/aio/read_stream.c +++ b/src/backend/storage/aio/read_stream.c @@ -612,7 +612,8 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data) stream->advice_enabled ? READ_BUFFERS_ISSUE_ADVICE : 0))) { - /* Fast return. */ + /* Predict caller will soon access next page's header. */ + pg_prefetch_mem(BufferGetPage(stream->buffers[oldest_buffer_index])); return buffer; } @@ -743,6 +744,10 @@ read_stream_next_buffer(ReadStream *stream, void **per_buffer_data) /* Prepare for the next call. */ read_stream_look_ahead(stream, false); + /* Predict caller will soon access next page's header. */ + if (stream->pinned_buffers > 0) + pg_prefetch_mem(BufferGetPage(stream->buffers[stream->oldest_buffer_index])); + #ifndef READ_STREAM_DISABLE_FAST_PATH /* See if we can take the fast path for all-cached scans next time. */ if (stream->ios_in_progress == 0 && -- 2.39.2