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.
This commit is contained in:
Jean Boussier 2025-06-18 09:56:22 +01:00
parent ce38cba528
commit 393e9a5f3e
4 changed files with 22 additions and 21 deletions

1
eval.c
View file

@ -78,6 +78,7 @@ ruby_setup(void)
#endif
Init_BareVM();
rb_vm_encoded_insn_data_table_init();
Init_enable_namespace();
Init_vm_objects();
Init_fstring_table();

View file

@ -25,6 +25,9 @@ int Init_enc_set_filesystem_encoding(void);
/* newline.c */
void Init_newline(void);
/* namespace.c */
void Init_enable_namespace(void);
/* vm.c */
void Init_BareVM(void);
void Init_vm_objects(void);

View file

@ -51,7 +51,14 @@ typedef struct rb_namespace_struct rb_namespace_t;
#define NAMESPACE_CC(cc) (cc ? NAMESPACE_METHOD_ENTRY(cc->cme_) : NULL)
#define NAMESPACE_CC_ENTRIES(ccs) (ccs ? NAMESPACE_METHOD_ENTRY(ccs->cme) : NULL)
int rb_namespace_available(void);
RUBY_EXTERN bool ruby_namespace_enabled;
static inline bool
rb_namespace_available(void)
{
return ruby_namespace_enabled;
}
void rb_namespace_enable_builtin(void);
void rb_namespace_disable_builtin(void);
void rb_namespace_push_loading_namespace(const rb_namespace_t *);

View file

@ -47,29 +47,10 @@ static bool tmp_dir_has_dirsep;
# define DIRSEP "/"
#endif
static int namespace_availability = 0;
bool ruby_namespace_enabled = false; // extern
VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
static VALUE rb_namespace_inspect(VALUE obj);
int
rb_namespace_available(void)
{
const char *env;
if (namespace_availability) {
return namespace_availability > 0 ? 1 : 0;
}
env = getenv("RUBY_NAMESPACE");
if (env && strlen(env) > 0) {
if (strcmp(env, "1") == 0) {
namespace_availability = 1;
return 1;
}
}
namespace_availability = -1;
return 0;
}
static void namespace_push(rb_thread_t *th, VALUE namespace);
static VALUE namespace_pop(VALUE th_value);
@ -1031,6 +1012,15 @@ namespace_define_loader_method(const char *name)
rb_define_singleton_method(rb_mNamespaceLoader, name, rb_namespace_user_loading_func, -1);
}
void
Init_enable_namespace(void)
{
const char *env = getenv("RUBY_NAMESPACE");
if (env && strlen(env) == 1 && env[0] == '1') {
ruby_namespace_enabled = true;
}
}
void
Init_Namespace(void)
{