Skip to content

Commit

Permalink
* hash.c: do not allocate st_table when it is not necessary.
Browse files Browse the repository at this point in the history
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@35195 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
nobu committed Mar 31, 2012
1 parent 5f81a53 commit e8a1874
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Sat Mar 31 14:16:02 2012 Sokolov Yura (funny-falcon) <[email protected]>

* hash.c: do not allocate st_table when it is not necessary.

Sat Mar 31 13:42:39 2012 Shugo Maeda <[email protected]>

* lib/net/ftp.rb (read_timeout=, open_timeout=): supported timeout.
Expand Down
40 changes: 22 additions & 18 deletions hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@ rb_hash_default(int argc, VALUE *argv, VALUE hash)
static VALUE
rb_hash_set_default(VALUE hash, VALUE ifnone)
{
rb_hash_modify(hash);
rb_hash_modify_check(hash);
RHASH_IFNONE(hash) = ifnone;
FL_UNSET(hash, HASH_PROC_DEFAULT);
return ifnone;
Expand Down Expand Up @@ -697,7 +697,7 @@ rb_hash_set_default_proc(VALUE hash, VALUE proc)
{
VALUE b;

rb_hash_modify(hash);
rb_hash_modify_check(hash);
b = rb_check_convert_type(proc, T_DATA, "Proc", "to_proc");
if (NIL_P(b) || !rb_obj_is_proc(b)) {
rb_raise(rb_eTypeError,
Expand Down Expand Up @@ -799,7 +799,7 @@ rb_hash_delete(VALUE hash, VALUE key)
{
VALUE val;

rb_hash_modify(hash);
rb_hash_modify_check(hash);
val = rb_hash_delete_key(hash, key);
if (val != Qundef) return val;
if (rb_block_given_p()) {
Expand Down Expand Up @@ -852,18 +852,20 @@ rb_hash_shift(VALUE hash)
{
struct shift_var var;

rb_hash_modify(hash);
var.key = Qundef;
rb_hash_foreach(hash, RHASH(hash)->iter_lev > 0 ? shift_i_safe : shift_i,
(VALUE)&var);

if (var.key != Qundef) {
if (RHASH(hash)->iter_lev > 0) {
rb_hash_delete_key(hash, var.key);
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl) {
var.key = Qundef;
rb_hash_foreach(hash, RHASH(hash)->iter_lev > 0 ? shift_i_safe : shift_i,
(VALUE)&var);

if (var.key != Qundef) {
if (RHASH(hash)->iter_lev > 0) {
rb_hash_delete_key(hash, var.key);
}
return rb_assoc_new(var.key, var.val);
}
return rb_assoc_new(var.key, var.val);
}
else if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
if (FL_TEST(hash, HASH_PROC_DEFAULT)) {
return rb_funcall(RHASH_IFNONE(hash), id_yield, 2, hash, Qnil);
}
else {
Expand Down Expand Up @@ -899,8 +901,9 @@ VALUE
rb_hash_delete_if(VALUE hash)
{
RETURN_ENUMERATOR(hash, 0, 0);
rb_hash_modify(hash);
rb_hash_foreach(hash, delete_if_i, hash);
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl)
rb_hash_foreach(hash, delete_if_i, hash);
return hash;
}

Expand Down Expand Up @@ -1025,7 +1028,7 @@ rb_hash_select_bang(VALUE hash)
st_index_t n;

RETURN_ENUMERATOR(hash, 0, 0);
rb_hash_modify(hash);
rb_hash_modify_check(hash);
if (!RHASH(hash)->ntbl)
return Qnil;
n = RHASH(hash)->ntbl->num_entries;
Expand All @@ -1050,8 +1053,9 @@ VALUE
rb_hash_keep_if(VALUE hash)
{
RETURN_ENUMERATOR(hash, 0, 0);
rb_hash_modify(hash);
rb_hash_foreach(hash, keep_if_i, hash);
rb_hash_modify_check(hash);
if (RHASH(hash)->ntbl)
rb_hash_foreach(hash, keep_if_i, hash);
return hash;
}

Expand Down

0 comments on commit e8a1874

Please sign in to comment.