mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
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:
parent
ce38cba528
commit
393e9a5f3e
4 changed files with 22 additions and 21 deletions
1
eval.c
1
eval.c
|
@ -78,6 +78,7 @@ ruby_setup(void)
|
||||||
#endif
|
#endif
|
||||||
Init_BareVM();
|
Init_BareVM();
|
||||||
rb_vm_encoded_insn_data_table_init();
|
rb_vm_encoded_insn_data_table_init();
|
||||||
|
Init_enable_namespace();
|
||||||
Init_vm_objects();
|
Init_vm_objects();
|
||||||
Init_fstring_table();
|
Init_fstring_table();
|
||||||
|
|
||||||
|
|
|
@ -25,6 +25,9 @@ int Init_enc_set_filesystem_encoding(void);
|
||||||
/* newline.c */
|
/* newline.c */
|
||||||
void Init_newline(void);
|
void Init_newline(void);
|
||||||
|
|
||||||
|
/* namespace.c */
|
||||||
|
void Init_enable_namespace(void);
|
||||||
|
|
||||||
/* vm.c */
|
/* vm.c */
|
||||||
void Init_BareVM(void);
|
void Init_BareVM(void);
|
||||||
void Init_vm_objects(void);
|
void Init_vm_objects(void);
|
||||||
|
|
|
@ -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(cc) (cc ? NAMESPACE_METHOD_ENTRY(cc->cme_) : NULL)
|
||||||
#define NAMESPACE_CC_ENTRIES(ccs) (ccs ? NAMESPACE_METHOD_ENTRY(ccs->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_enable_builtin(void);
|
||||||
void rb_namespace_disable_builtin(void);
|
void rb_namespace_disable_builtin(void);
|
||||||
void rb_namespace_push_loading_namespace(const rb_namespace_t *);
|
void rb_namespace_push_loading_namespace(const rb_namespace_t *);
|
||||||
|
|
30
namespace.c
30
namespace.c
|
@ -47,29 +47,10 @@ static bool tmp_dir_has_dirsep;
|
||||||
# define DIRSEP "/"
|
# define DIRSEP "/"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int namespace_availability = 0;
|
bool ruby_namespace_enabled = false; // extern
|
||||||
|
|
||||||
VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
|
VALUE rb_resolve_feature_path(VALUE klass, VALUE fname);
|
||||||
static VALUE rb_namespace_inspect(VALUE obj);
|
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 void namespace_push(rb_thread_t *th, VALUE namespace);
|
||||||
static VALUE namespace_pop(VALUE th_value);
|
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);
|
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
|
void
|
||||||
Init_Namespace(void)
|
Init_Namespace(void)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue