Skip to content

Commit 393e9a5

Browse files
committed
Optimize rb_namespace_available
Rather than to lazily check the env using a trinary value, we can more straightforwardly check for the env during the VM boot. This allow `rb_namespace_available` to just be a pointer dereference.
1 parent ce38cba commit 393e9a5

File tree

4 files changed

+22
-21
lines changed

4 files changed

+22
-21
lines changed

eval.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ ruby_setup(void)
7878
#endif
7979
Init_BareVM();
8080
rb_vm_encoded_insn_data_table_init();
81+
Init_enable_namespace();
8182
Init_vm_objects();
8283
Init_fstring_table();
8384

internal/inits.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ int Init_enc_set_filesystem_encoding(void);
2525
/* newline.c */
2626
void Init_newline(void);
2727

28+
/* namespace.c */
29+
void Init_enable_namespace(void);
30+
2831
/* vm.c */
2932
void Init_BareVM(void);
3033
void Init_vm_objects(void);

internal/namespace.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,14 @@ typedef struct rb_namespace_struct rb_namespace_t;
5151
#define NAMESPACE_CC(cc) (cc ? NAMESPACE_METHOD_ENTRY(cc->cme_) : NULL)
5252
#define NAMESPACE_CC_ENTRIES(ccs) (ccs ? NAMESPACE_METHOD_ENTRY(ccs->cme) : NULL)
5353

54-
int rb_namespace_available(void);
54+
RUBY_EXTERN bool ruby_namespace_enabled;
55+
56+
static inline bool
57+
rb_namespace_available(void)
58+
{
59+
return ruby_namespace_enabled;
60+
}
61+
5562
void rb_namespace_enable_builtin(void);
5663
void rb_namespace_disable_builtin(void);
5764
void rb_namespace_push_loading_namespace(const rb_namespace_t *);

namespace.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,10 @@ static bool tmp_dir_has_dirsep;
4747
# define DIRSEP "/"
4848
#endif
4949

50-
static int namespace_availability = 0;
50+
bool ruby_namespace_enabled = false; // extern
5151

5252
VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
5353
static VALUE rb_namespace_inspect(VALUE obj);
54-
55-
int
56-
rb_namespace_available(void)
57-
{
58-
const char *env;
59-
if (namespace_availability) {
60-
return namespace_availability > 0 ? 1 : 0;
61-
}
62-
env = getenv("RUBY_NAMESPACE");
63-
if (env && strlen(env) > 0) {
64-
if (strcmp(env, "1") == 0) {
65-
namespace_availability = 1;
66-
return 1;
67-
}
68-
}
69-
namespace_availability = -1;
70-
return 0;
71-
}
72-
7354
static void namespace_push(rb_thread_t *th, VALUE namespace);
7455
static VALUE namespace_pop(VALUE th_value);
7556

@@ -1031,6 +1012,15 @@ namespace_define_loader_method(const char *name)
10311012
rb_define_singleton_method(rb_mNamespaceLoader, name, rb_namespace_user_loading_func, -1);
10321013
}
10331014

1015+
void
1016+
Init_enable_namespace(void)
1017+
{
1018+
const char *env = getenv("RUBY_NAMESPACE");
1019+
if (env && strlen(env) == 1 && env[0] == '1') {
1020+
ruby_namespace_enabled = true;
1021+
}
1022+
}
1023+
10341024
void
10351025
Init_Namespace(void)
10361026
{

0 commit comments

Comments
 (0)