mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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:
@@ -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"
|
||||
|
||||
/**
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user