Skip to content

Commit 670b54f

Browse files
matzGemini
andcommitted
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>
1 parent e6071d5 commit 670b54f

3 files changed

Lines changed: 10 additions & 0 deletions

File tree

include/mruby/variable.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
#ifndef MRUBY_VARIABLE_H
88
#define MRUBY_VARIABLE_H
99

10+
#if defined(__GNUC__) || defined(__clang__)
11+
#define MRB_MEM_PREFETCH(addr) __builtin_prefetch(addr, 0, 1)
12+
#else
13+
#define MRB_MEM_PREFETCH(addr)
14+
#endif
15+
1016
#include "common.h"
1117

1218
/**

src/class.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ bsearch_idx(mrb_sym *keys, int size, mrb_sym target)
105105
/* While more than one element remains, halve the range each iteration */
106106
while (n > 1) {
107107
int half = n >> 1;
108+
MRB_MEM_PREFETCH(p + (half >> 1));
109+
MRB_MEM_PREFETCH(p + half + (half >> 1));
108110
mrb_sym mid_sym = MT_KEY_SYM(p[half]);
109111
/*
110112
* Update pointer p without a branch:

src/variable.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ bsearch_idx(mrb_sym *keys, int size, mrb_sym target) {
7878
/* While more than one element remains, halve the range each iteration */
7979
while (n > 1) {
8080
int half = n >> 1;
81+
MRB_MEM_PREFETCH(p + (half >> 1));
82+
MRB_MEM_PREFETCH(p + half + (half >> 1));
8183
mrb_sym mid_sym = p[half];
8284
/*
8385
* Update pointer p without a branch:

0 commit comments

Comments
 (0)