From 3ebefc433b0db0e621d6e80e1654039e5a80405b Mon Sep 17 00:00:00 2001 From: RingsC Date: Tue, 15 Oct 2024 20:15:42 +0800 Subject: [PATCH] feat(rpaid):to use build-in-prefect in rapid table scan (#268) Co-authored-by: shannon data ai --- storage/rapid_engine/imcs/chunk.h | 2 +- storage/rapid_engine/imcs/data_table.cpp | 8 ++++++++ storage/rapid_engine/include/rapid_const.h | 9 +++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/storage/rapid_engine/imcs/chunk.h b/storage/rapid_engine/imcs/chunk.h index 720720c69..f38279c58 100644 --- a/storage/rapid_engine/imcs/chunk.h +++ b/storage/rapid_engine/imcs/chunk.h @@ -147,7 +147,7 @@ class Chunk : public MemoryObject { // the data write to where in this chunk. inline uchar *where() { assert(m_data < m_end); - return m_data; + return m_data.load(); } // return the real time chunk size. diff --git a/storage/rapid_engine/imcs/data_table.cpp b/storage/rapid_engine/imcs/data_table.cpp index cc4df3b1a..bee13ad68 100644 --- a/storage/rapid_engine/imcs/data_table.cpp +++ b/storage/rapid_engine/imcs/data_table.cpp @@ -118,6 +118,9 @@ int DataTable::next(uchar *buf) { auto offset_in_chunk = m_rowid % SHANNON_ROWS_IN_CHUNK; ut_a(m_context->m_trx_id_cu); auto trx_id_ptr = m_context->m_trx_id_cu->chunk(current_chunk)->seek(offset_in_chunk); + // more info for __builtin_prefetch: https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html + if ((trx_id_ptr + CACHE_LINE_SIZE) < m_context->m_trx_id_cu->chunk(current_chunk)->where()) + __builtin_prefetch(trx_id_ptr + CACHE_LINE_SIZE, SHANNON_PREFETCH_FOR_READ, SHANNON_PREFETCH_L3_LOCALITY); Transaction::ID trx_id = mach_read_from_6(trx_id_ptr); for (auto idx = 0u; idx < m_field_cus.size(); idx++) { @@ -130,6 +133,11 @@ int DataTable::next(uchar *buf) { return HA_ERR_GENERIC; }); + // prefetch data for cpu to reduce the data cache miss. + if (cu->chunk(current_chunk)->seek(offset_in_chunk) + CACHE_LINE_SIZE < cu->chunk(current_chunk)->where()) + __builtin_prefetch(cu->chunk(current_chunk)->seek(offset_in_chunk) + CACHE_LINE_SIZE, SHANNON_PREFETCH_FOR_READ, + SHANNON_PREFETCH_L3_LOCALITY); + // visibility check. if it's not visibile and does not have old version, go to next. if (!m_context->m_trx->is_visible(trx_id, m_context->m_table_name) && !cu->chunk(current_chunk)->get_header()->m_smu) { diff --git a/storage/rapid_engine/include/rapid_const.h b/storage/rapid_engine/include/rapid_const.h index 5bac4e101..9c3561f1b 100644 --- a/storage/rapid_engine/include/rapid_const.h +++ b/storage/rapid_engine/include/rapid_const.h @@ -40,6 +40,15 @@ #define SHANNON_ALIGNAS alignas(CACHE_LINE_SIZE) +// THE FOLLOWING FOR PREFETCH CPU INSTRUCTION. +#define SHANNON_PREFETCH_FOR_READ 0 +#define SHANNON_PREFETCH_FOR_WRITE 1 + +#define SHANNON_PREFETCH__NONE_LOCALITY 0 +#define SHANNON_PREFETCH_L3_LOCALITY 1 +#define SHANNON_PREFETCH_L2_LOCALITY 2 +#define SAHNNON_PREFETCH_L1_LOCALITY 3 + namespace ShannonBase { using row_id_t = size_t; /** Handler name for rapid */