reduce lock for encoding

To reduce the number of locking for encoding manipulation,
enc_table::list is splited to ::default_list and ::additional_list.
::default_list is pre-allocated and no need locking to access to
the ::default_list. If additional encoding space is needed, use
::additional_list and this list need to use locking.
However, most of case, ::default_list is enough.
This commit is contained in:
Koichi Sasada 2020-10-17 10:13:07 +09:00
parent f1f8f38079
commit de17e2dea1
Notes: git 2020-10-19 14:07:05 +09:00

View File

@ -68,9 +68,11 @@ struct rb_encoding_entry {
}; };
static struct enc_table { static struct enc_table {
struct rb_encoding_entry *list; // default_list + additional_list
struct rb_encoding_entry default_list[DEFAULT_ENCODING_LIST_CAPA];
struct rb_encoding_entry *additional_list;
int additional_list_size;
int count; int count;
int size;
st_table *names; st_table *names;
} global_enc_table; } global_enc_table;
@ -348,24 +350,54 @@ rb_find_encoding(VALUE enc)
} }
static int static int
enc_table_expand(struct enc_table *enc_table, int newsize) enc_table_expand(struct enc_table *enc_table, const int newsize)
{ {
struct rb_encoding_entry *ent; if (newsize <= DEFAULT_ENCODING_LIST_CAPA) {
int count = newsize; // ok
}
else {
int add_size = newsize - DEFAULT_ENCODING_LIST_CAPA;
if (add_size <= enc_table->additional_list_size) {
// ok
}
else {
struct rb_encoding_entry *ent;
add_size = (add_size + 7) / 8 * 8;
if (enc_table->size >= newsize) return newsize; if (enc_table->additional_list == NULL) {
newsize = (newsize + 7) / 8 * 8; ent = enc_table->additional_list = ALLOC_N(struct rb_encoding_entry, add_size);
ent = REALLOC_N(enc_table->list, struct rb_encoding_entry, newsize); }
memset(ent + enc_table->size, 0, sizeof(*ent)*(newsize - enc_table->size)); else {
enc_table->list = ent; ent = REALLOC_N(enc_table->additional_list, struct rb_encoding_entry, add_size);
enc_table->size = newsize; }
return count;
memset(ent + enc_table->additional_list_size, 0, sizeof(*ent)*(add_size - enc_table->additional_list_size));
enc_table->additional_list = ent;
enc_table->additional_list_size = add_size;
}
}
return newsize;
}
static struct rb_encoding_entry *
enc_entry_at(struct enc_table *enc_table, int index)
{
if (LIKELY(index < DEFAULT_ENCODING_LIST_CAPA)) {
return &enc_table->default_list[index];
}
else {
struct rb_encoding_entry *e;
GLOBAL_ENC_TABLE_EVAL(enc_table,
e = &enc_table->additional_list[index - DEFAULT_ENCODING_LIST_CAPA]);
return e;
}
} }
static int static int
enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding) enc_register_at(struct enc_table *enc_table, int index, const char *name, rb_encoding *base_encoding)
{ {
struct rb_encoding_entry *ent = &enc_table->list[index]; struct rb_encoding_entry *ent = enc_entry_at(enc_table, index);
rb_raw_encoding *encoding; rb_raw_encoding *encoding;
if (!valid_encoding_name_p(name)) return -1; if (!valid_encoding_name_p(name)) return -1;
@ -409,19 +441,18 @@ static int enc_registered(struct enc_table *enc_table, const char *name);
static rb_encoding * static rb_encoding *
enc_from_index(struct enc_table *enc_table, int index) enc_from_index(struct enc_table *enc_table, int index)
{ {
// do not need a lock
if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) { if (UNLIKELY(index < 0 || enc_table->count <= (index &= ENC_INDEX_MASK))) {
return 0; return 0;
} }
return enc_table->list[index].enc; return enc_entry_at(enc_table, index)->enc;
} }
rb_encoding * rb_encoding *
rb_enc_from_index(int index) rb_enc_from_index(int index)
{ {
rb_encoding *enc; return enc_from_index(&global_enc_table, index);
GLOBAL_ENC_TABLE_EVAL(enc_table,
enc = enc_from_index(enc_table, index));
return enc;
} }
int int
@ -460,7 +491,7 @@ enc_registered(struct enc_table *enc_table, const char *name)
st_data_t idx = 0; st_data_t idx = 0;
if (!name) return -1; if (!name) return -1;
if (!enc_table->list) return -1; if (!enc_table->names) return -1;
if (st_lookup(enc_table->names, (st_data_t)name, &idx)) { if (st_lookup(enc_table->names, (st_data_t)name, &idx)) {
return (int)idx; return (int)idx;
} }
@ -492,9 +523,9 @@ enc_check_duplication(struct enc_table *enc_table, const char *name)
static rb_encoding* static rb_encoding*
set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base) set_base_encoding(struct enc_table *enc_table, int index, rb_encoding *base)
{ {
rb_encoding *enc = enc_table->list[index].enc; struct rb_encoding_entry *entry = enc_entry_at(enc_table, index);
rb_encoding *enc = entry->enc;
enc_table->list[index].base = base; entry->base = base;
if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc); if (ENC_DUMMY_P(base)) ENC_SET_DUMMY((rb_raw_encoding *)enc);
return enc; return enc;
} }
@ -521,11 +552,7 @@ rb_enc_set_base(const char *name, const char *orig)
int int
rb_enc_set_dummy(int index) rb_enc_set_dummy(int index)
{ {
rb_encoding *enc; rb_encoding *enc = rb_enc_from_index(index);
GLOBAL_ENC_TABLE_EVAL(enc_table,
enc = enc_table->list[index].enc);
ENC_SET_DUMMY((rb_raw_encoding *)enc); ENC_SET_DUMMY((rb_raw_encoding *)enc);
return index; return index;
} }
@ -615,14 +642,10 @@ rb_define_dummy_encoding(const char *name)
{ {
int index; int index;
GLOBAL_ENC_TABLE_ENTER(enc_table); GLOBAL_ENC_TABLE_EVAL(enc_table,
{ index = enc_replicate(enc_table, name, rb_ascii8bit_encoding()));
index = enc_replicate(enc_table, name, rb_ascii8bit_encoding()); rb_encoding *enc = rb_enc_from_index(index);
rb_encoding *enc = enc_table->list[index].enc; ENC_SET_DUMMY((rb_raw_encoding *)enc);
ENC_SET_DUMMY((rb_raw_encoding *)enc);
}
GLOBAL_ENC_TABLE_LEAVE();
return index; return index;
} }
@ -630,17 +653,12 @@ int
rb_encdb_dummy(const char *name) rb_encdb_dummy(const char *name)
{ {
int index; int index;
GLOBAL_ENC_TABLE_EVAL(enc_table,
GLOBAL_ENC_TABLE_ENTER(enc_table); index = enc_replicate_with_index(enc_table, name,
{ rb_ascii8bit_encoding(),
index = enc_replicate_with_index(enc_table, name, enc_registered(enc_table, name)));
rb_ascii8bit_encoding(), rb_encoding *enc = rb_enc_from_index(index);
enc_registered(enc_table, name)); ENC_SET_DUMMY((rb_raw_encoding *)enc);
rb_encoding *enc = enc_table->list[index].enc;
ENC_SET_DUMMY((rb_raw_encoding *)enc);
}
GLOBAL_ENC_TABLE_LEAVE();
return index; return index;
} }
@ -770,9 +788,10 @@ rb_enc_init(struct enc_table *enc_table)
ENC_REGISTER(ASCII); ENC_REGISTER(ASCII);
ENC_REGISTER(UTF_8); ENC_REGISTER(UTF_8);
ENC_REGISTER(US_ASCII); ENC_REGISTER(US_ASCII);
global_enc_ascii = enc_table->list[ENCINDEX_ASCII].enc;
global_enc_utf_8 = enc_table->list[ENCINDEX_UTF_8].enc; global_enc_ascii = enc_table->default_list[ENCINDEX_ASCII].enc;
global_enc_us_ascii = enc_table->list[ENCINDEX_US_ASCII].enc; global_enc_utf_8 = enc_table->default_list[ENCINDEX_UTF_8].enc;
global_enc_us_ascii = enc_table->default_list[ENCINDEX_US_ASCII].enc;
#undef ENC_REGISTER #undef ENC_REGISTER
#define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL) #define ENCDB_REGISTER(name, enc) enc_register_at(enc_table, ENCINDEX_##enc, name, NULL)
ENCDB_REGISTER("UTF-16BE", UTF_16BE); ENCDB_REGISTER("UTF-16BE", UTF_16BE);
@ -828,7 +847,7 @@ load_encoding(const char *name)
else if ((idx = enc_registered(enc_table, name)) < 0) { else if ((idx = enc_registered(enc_table, name)) < 0) {
idx = -1; idx = -1;
} }
else if (enc_autoload_p(enc_table->list[idx].enc)) { else if (enc_autoload_p(enc_from_index(enc_table, idx))) {
idx = -1; idx = -1;
} }
} }
@ -840,13 +859,13 @@ load_encoding(const char *name)
static int static int
enc_autoload_body(struct enc_table *enc_table, rb_encoding *enc) enc_autoload_body(struct enc_table *enc_table, rb_encoding *enc)
{ {
rb_encoding *base = enc_table->list[ENC_TO_ENCINDEX(enc)].base; rb_encoding *base = enc_entry_at(enc_table, ENC_TO_ENCINDEX(enc))->base;
if (base) { if (base) {
int i = 0; int i = 0;
do { do {
if (i >= enc_table->count) return -1; if (i >= enc_table->count) return -1;
} while (enc_table->list[i].enc != base && (++i, 1)); } while (enc_from_index(enc_table, i) != base && (++i, 1));
if (enc_autoload_p(base)) { if (enc_autoload_p(base)) {
if (enc_autoload(base) < 0) return -1; if (enc_autoload(base) < 0) return -1;
} }
@ -2188,7 +2207,7 @@ Init_Encoding(void)
rb_gc_register_mark_object(list); rb_gc_register_mark_object(list);
for (i = 0; i < enc_table->count; ++i) { for (i = 0; i < enc_table->count; ++i) {
rb_ary_push(list, enc_new(enc_table->list[i].enc)); rb_ary_push(list, enc_new(enc_from_index(enc_table, i)));
} }
rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader); rb_marshal_define_compat(rb_cEncoding, Qnil, 0, enc_m_loader);