diff --git a/include/mruby/variable.h b/include/mruby/variable.h index 0a7b7b42e..2af62f048 100644 --- a/include/mruby/variable.h +++ b/include/mruby/variable.h @@ -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" /** diff --git a/src/class.c b/src/class.c index c2bffeaf9..a23b87447 100644 --- a/src/class.c +++ b/src/class.c @@ -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: diff --git a/src/variable.c b/src/variable.c index 4f4d9e123..75f5254b3 100644 --- a/src/variable.c +++ b/src/variable.c @@ -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: