mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
use kh_put2 in mrb_hash_set for performance improvement
This commit is contained in:
+6
-4
@@ -204,21 +204,23 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
|
||||
{
|
||||
khash_t(ht) *h;
|
||||
khiter_t k;
|
||||
int r;
|
||||
|
||||
mrb_hash_modify(mrb, hash);
|
||||
h = RHASH_TBL(hash);
|
||||
|
||||
if (!h) h = RHASH_TBL(hash) = kh_init(ht, mrb);
|
||||
k = kh_get(ht, mrb, h, key);
|
||||
if (k == kh_end(h)) {
|
||||
k = kh_put2(ht, mrb, h, key, &r);
|
||||
kh_value(h, k).v = val;
|
||||
|
||||
if (r != 0) {
|
||||
/* expand */
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
k = kh_put(ht, mrb, h, KEY(key));
|
||||
kh_key(h, k) = KEY(key);
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
kh_value(h, k).n = kh_size(h)-1;
|
||||
}
|
||||
|
||||
kh_value(h, k).v = val;
|
||||
mrb_write_barrier(mrb, (struct RBasic*)RHASH(hash));
|
||||
return;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user