ruby-changes:61983
From: =E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3 <ko1@a...>
Date: Mon, 29 Jun 2020 11:06:26 +0900 (JST)
Subject: [ruby-changes:61983] 8d182b04ed (master): builtin_lookup: do not goto into a branch
https://git.ruby-lang.org/ruby.git/commit/?id=8d182b04ed From 8d182b04ed04378877e743535658be41a05aef74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8D=9C=E9=83=A8=E6=98=8C=E5=B9=B3?= <shyouhei@r...> Date: Thu, 11 Jun 2020 13:23:56 +0900 Subject: builtin_lookup: do not goto into a branch I'm not necessarily against every goto in general, but jumping into a branch is definitely a bad idea. Better refactor. diff --git a/builtin.c b/builtin.c index 6de7722..7c3653d 100644 --- a/builtin.c +++ b/builtin.c @@ -12,27 +12,24 @@ https://github.com/ruby/ruby/blob/trunk/builtin.c#L12 #include "builtin_binary.inc" +static const unsigned char * +bin4feature(const struct builtin_binary *bb, const char *feature, size_t *psize) +{ + *psize = bb->bin_size; + return strcmp(bb->feature, feature) ? NULL : bb->bin; +} + static const unsigned char* builtin_lookup(const char *feature, size_t *psize) { static int index = 0; - int i = index++; + const unsigned char *bin = bin4feature(&builtin_binary[index++], feature, psize); // usually, `builtin_binary` order is loading order at miniruby. - if (LIKELY(strcmp(builtin_binary[i].feature, feature) == 0)) { - found: - *psize = builtin_binary[i].bin_size; - return builtin_binary[i].bin; + for (const struct builtin_binary *bb = &builtin_binary[0]; bb->feature &&! bin; bb++) { + bin = bin4feature(bb++, feature, psize); } - else { - if (0) fprintf(stderr, "builtin_lookup: cached index miss (index:%d)\n", i); - for (i=0; i<BUILTIN_BINARY_SIZE; i++) { - if (strcmp(builtin_binary[i].feature, feature) == 0) { - goto found; - } - } - } - rb_bug("builtin_lookup: can not find %s\n", feature); + return bin; } void @@ -41,6 +38,9 @@ rb_load_with_builtin_functions(const char *feature_name, const struct rb_builtin https://github.com/ruby/ruby/blob/trunk/builtin.c#L38 // search binary size_t size; const unsigned char *bin = builtin_lookup(feature_name, &size); + if (! bin) { + rb_bug("builtin_lookup: can not find %s\n", feature_name); + } // load binary rb_vm_t *vm = GET_VM(); -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/