Project

General

Profile

Bug #8312 » add_st_shift.diff

fix Hash#shift performance by adding st_shift - funny_falcon (Yura Sokolov), 04/24/2013 12:21 AM

View differences:

hash.c
};
static int
shift_i(VALUE key, VALUE value, VALUE arg)
{
struct shift_var *var = (struct shift_var *)arg;
if (var->key != Qundef) return ST_STOP;
var->key = key;
var->val = value;
return ST_DELETE;
}
static int
shift_i_safe(VALUE key, VALUE value, VALUE arg)
{
struct shift_var *var = (struct shift_var *)arg;
......
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl) {
var.key = Qundef;
rb_hash_foreach(hash, RHASH_ITER_LEV(hash) > 0 ? shift_i_safe : shift_i,
(VALUE)&var);
if (var.key != Qundef) {
if (RHASH_ITER_LEV(hash) > 0) {
if (RHASH_ITER_LEV(hash) == 0) {
if (st_shift(RHASH(hash)->ntbl, &var.key, &var.val)) {
return rb_assoc_new(var.key, var.val);
}
}
else {
rb_hash_foreach(hash, shift_i_safe, (VALUE)&var);
if (var.key != Qundef) {
rb_hash_delete_key(hash, var.key);
return rb_assoc_new(var.key, var.val);
}
return rb_assoc_new(var.key, var.val);
}
}
return hash_default_value(hash, Qnil);
include/ruby/st.h
st_table *st_init_strcasetable_with_size(st_index_t);
int st_delete(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */
int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t);
int st_shift(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */
int st_insert(st_table *, st_data_t, st_data_t);
int st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t));
int st_lookup(st_table *, st_data_t, st_data_t *);
st.c
return 0;
}
int
st_shift(register st_table *table, register st_data_t *key, st_data_t *value)
{
st_index_t hash_val;
st_table_entry **prev;
register st_table_entry *ptr;
if (table->num_entries == 0) {
if (value != 0) *value = 0;
return 0;
}
if (table->entries_packed) {
if (value != 0) *value = PVAL(table, 0);
*key = PKEY(table, 0);
remove_packed_entry(table, 0);
return 1;
}
hash_val = do_hash(table->head->key, table);
prev = &table->bins[hash_val % table->num_bins];
for (;(ptr = *prev) != 0; prev = &ptr->next) {
if (ptr == table->head) {
*prev = ptr->next;
if (value != 0) *value = ptr->record;
*key = ptr->key;
remove_entry(table, ptr);
st_free_entry(ptr);
return 1;
}
}
/* if hash is not consistent and need to be rehashed */
if (value != 0) *value = 0;
return 0;
}
void
st_cleanup_safe(st_table *table, st_data_t never)
{
(2-2/3)