variable.h: add prefetch to bsearch_idx

This commit introduces memory prefetching to the `bsearch_idx` functions
in `src/class.c` and `src/variable.c` to improve performance.

A new macro `MRB_MEM_PREFETCH` is defined in `include/mruby/variable.h`
which uses `__builtin_prefetch` if available.

Co-authored-by: Gemini <gemini@google.com>
This commit is contained in:
Yukihiro "Matz" Matsumoto
2025-07-07 11:46:30 +09:00
parent e6071d5331
commit 670b54f859
3 changed files with 10 additions and 0 deletions
+6
View File
@@ -7,6 +7,12 @@
#ifndef MRUBY_VARIABLE_H
#define MRUBY_VARIABLE_H
#if defined(__GNUC__) || defined(__clang__)
#define MRB_MEM_PREFETCH(addr) __builtin_prefetch(addr, 0, 1)
#else
#define MRB_MEM_PREFETCH(addr)
#endif
#include "common.h"
/**
+2
View File
@@ -105,6 +105,8 @@ bsearch_idx(mrb_sym *keys, int size, mrb_sym target)
/* While more than one element remains, halve the range each iteration */
while (n > 1) {
int half = n >> 1;
MRB_MEM_PREFETCH(p + (half >> 1));
MRB_MEM_PREFETCH(p + half + (half >> 1));
mrb_sym mid_sym = MT_KEY_SYM(p[half]);
/*
* Update pointer p without a branch:
+2
View File
@@ -78,6 +78,8 @@ bsearch_idx(mrb_sym *keys, int size, mrb_sym target) {
/* While more than one element remains, halve the range each iteration */
while (n > 1) {
int half = n >> 1;
MRB_MEM_PREFETCH(p + (half >> 1));
MRB_MEM_PREFETCH(p + half + (half >> 1));
mrb_sym mid_sym = p[half];
/*
* Update pointer p without a branch: