From 6546aed4757be07f4932326e1eb41a5d69141acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lourens=20Naud=C3=A9?= Date: Sat, 13 Apr 2019 17:05:58 +0100 Subject: [PATCH] Explicitly initialise encodings on init to remove branches on encoding lookup [Misc #15806] Closes: https://github.com/ruby/ruby/pull/2128 --- encoding.c | 23 ++++++----------------- inits.c | 1 + 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/encoding.c b/encoding.c index 012d18c690..9655213f73 100644 --- a/encoding.c +++ b/encoding.c @@ -66,8 +66,6 @@ static struct { #define ENC_DUMMY_P(enc) ((enc)->ruby_encoding_index & ENC_DUMMY_FLAG) #define ENC_SET_DUMMY(enc) ((enc)->ruby_encoding_index |= ENC_DUMMY_FLAG) -void rb_enc_init(void); - #define ENCODING_COUNT ENCINDEX_BUILTIN_MAX #define UNSPECIFIED_ENCODING INT_MAX @@ -557,9 +555,6 @@ rb_enc_alias(const char *alias, const char *orig) int idx; enc_check_duplication(alias); - if (!enc_table.list) { - rb_enc_init(); - } if ((idx = rb_enc_find_index(orig)) < 0) { return -1; } @@ -613,9 +608,6 @@ rb_enc_init(void) rb_encoding * rb_enc_from_index(int index) { - if (!enc_table.list) { - rb_enc_init(); - } if (index < 0 || enc_table.count <= (index &= ENC_INDEX_MASK)) { return 0; } @@ -1324,9 +1316,6 @@ enc_m_loader(VALUE klass, VALUE str) rb_encoding * rb_ascii8bit_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } return enc_table.list[ENCINDEX_ASCII].enc; } @@ -1339,9 +1328,6 @@ rb_ascii8bit_encindex(void) rb_encoding * rb_utf8_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } return enc_table.list[ENCINDEX_UTF_8].enc; } @@ -1354,9 +1340,6 @@ rb_utf8_encindex(void) rb_encoding * rb_usascii_encoding(void) { - if (!enc_table.list) { - rb_enc_init(); - } return enc_table.list[ENCINDEX_US_ASCII].enc; } @@ -1933,6 +1916,12 @@ rb_enc_aliases(VALUE klass) * */ +void +Init_encodings(void) +{ + rb_enc_init(); +} + void Init_Encoding(void) { diff --git a/inits.c b/inits.c index ad34223e36..75ee976192 100644 --- a/inits.c +++ b/inits.c @@ -22,6 +22,7 @@ rb_call_inits(void) CALL(vm_postponed_job); CALL(Method); CALL(RandomSeedCore); + CALL(encodings); CALL(sym); CALL(var_tables); CALL(Object);