Skip to content

Commit 60e19a0

Browse files
committedNov 12, 2023
Just check if iteration level is non-zero
The level in ivar is no longer needed to check if iterating, only used for increment/decrement.
1 parent 9ab64b1 commit 60e19a0

File tree

1 file changed

+16
-23
lines changed

1 file changed

+16
-23
lines changed
 

‎hash.c

+16-23
Original file line numberDiff line numberDiff line change
@@ -1307,17 +1307,10 @@ iter_lev_in_flags_set(VALUE hash, unsigned long lev)
13071307
RBASIC(hash)->flags = ((RBASIC(hash)->flags & ~RHASH_LEV_MASK) | ((VALUE)lev << RHASH_LEV_SHIFT));
13081308
}
13091309

1310-
static unsigned long
1311-
RHASH_ITER_LEV(VALUE hash)
1310+
static inline bool
1311+
hash_iterating_p(VALUE hash)
13121312
{
1313-
unsigned long lev = iter_lev_in_flags(hash);
1314-
1315-
if (lev == RHASH_LEV_MAX) {
1316-
return iter_lev_in_ivar(hash);
1317-
}
1318-
else {
1319-
return lev;
1320-
}
1313+
return iter_lev_in_flags(hash) > 0;
13211314
}
13221315

13231316
static void
@@ -1436,7 +1429,7 @@ void rb_st_compact_table(st_table *tab);
14361429
static void
14371430
compact_after_delete(VALUE hash)
14381431
{
1439-
if (RHASH_ITER_LEV(hash) == 0 && RHASH_ST_TABLE_P(hash)) {
1432+
if (!hash_iterating_p(hash) && RHASH_ST_TABLE_P(hash)) {
14401433
rb_st_compact_table(RHASH_ST_TABLE(hash));
14411434
}
14421435
}
@@ -1691,14 +1684,14 @@ tbl_update(VALUE hash, VALUE key, tbl_update_func func, st_data_t optional_arg)
16911684
return ret;
16921685
}
16931686

1694-
#define UPDATE_CALLBACK(iter_lev, func) ((iter_lev) > 0 ? func##_noinsert : func##_insert)
1687+
#define UPDATE_CALLBACK(iter_p, func) ((iter_p) ? func##_noinsert : func##_insert)
16951688

1696-
#define RHASH_UPDATE_ITER(h, iter_lev, key, func, a) do { \
1697-
tbl_update((h), (key), UPDATE_CALLBACK((iter_lev), func), (st_data_t)(a)); \
1689+
#define RHASH_UPDATE_ITER(h, iter_p, key, func, a) do { \
1690+
tbl_update((h), (key), UPDATE_CALLBACK(iter_p, func), (st_data_t)(a)); \
16981691
} while (0)
16991692

17001693
#define RHASH_UPDATE(hash, key, func, arg) \
1701-
RHASH_UPDATE_ITER(hash, RHASH_ITER_LEV(hash), key, func, arg)
1694+
RHASH_UPDATE_ITER(hash, hash_iterating_p(hash), key, func, arg)
17021695

17031696
static void
17041697
set_proc_default(VALUE hash, VALUE proc)
@@ -1986,7 +1979,7 @@ rb_hash_rehash(VALUE hash)
19861979
VALUE tmp;
19871980
st_table *tbl;
19881981

1989-
if (RHASH_ITER_LEV(hash) > 0) {
1982+
if (hash_iterating_p(hash)) {
19901983
rb_raise(rb_eRuntimeError, "rehash during iteration");
19911984
}
19921985
rb_hash_modify_check(hash);
@@ -2465,7 +2458,7 @@ rb_hash_shift(VALUE hash)
24652458
rb_hash_modify_check(hash);
24662459
if (RHASH_AR_TABLE_P(hash)) {
24672460
var.key = Qundef;
2468-
if (RHASH_ITER_LEV(hash) == 0) {
2461+
if (!hash_iterating_p(hash)) {
24692462
if (ar_shift(hash, &var.key, &var.val)) {
24702463
return rb_assoc_new(var.key, var.val);
24712464
}
@@ -2480,7 +2473,7 @@ rb_hash_shift(VALUE hash)
24802473
}
24812474
if (RHASH_ST_TABLE_P(hash)) {
24822475
var.key = Qundef;
2483-
if (RHASH_ITER_LEV(hash) == 0) {
2476+
if (!hash_iterating_p(hash)) {
24842477
if (st_shift(RHASH_ST_TABLE(hash), &var.key, &var.val)) {
24852478
return rb_assoc_new(var.key, var.val);
24862479
}
@@ -2838,7 +2831,7 @@ rb_hash_clear(VALUE hash)
28382831
{
28392832
rb_hash_modify_check(hash);
28402833

2841-
if (RHASH_ITER_LEV(hash) > 0) {
2834+
if (hash_iterating_p(hash)) {
28422835
rb_hash_foreach(hash, clear_i, 0);
28432836
}
28442837
else if (RHASH_AR_TABLE_P(hash)) {
@@ -2909,15 +2902,15 @@ NOINSERT_UPDATE_CALLBACK(hash_aset_str)
29092902
VALUE
29102903
rb_hash_aset(VALUE hash, VALUE key, VALUE val)
29112904
{
2912-
unsigned long iter_lev = RHASH_ITER_LEV(hash);
2905+
bool iter_p = hash_iterating_p(hash);
29132906

29142907
rb_hash_modify(hash);
29152908

29162909
if (RHASH_TYPE(hash) == &identhash || rb_obj_class(key) != rb_cString) {
2917-
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset, val);
2910+
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset, val);
29182911
}
29192912
else {
2920-
RHASH_UPDATE_ITER(hash, iter_lev, key, hash_aset_str, val);
2913+
RHASH_UPDATE_ITER(hash, iter_p, key, hash_aset_str, val);
29212914
}
29222915
return val;
29232916
}
@@ -2937,7 +2930,7 @@ rb_hash_replace(VALUE hash, VALUE hash2)
29372930
{
29382931
rb_hash_modify_check(hash);
29392932
if (hash == hash2) return hash;
2940-
if (RHASH_ITER_LEV(hash) > 0) {
2933+
if (hash_iterating_p(hash)) {
29412934
rb_raise(rb_eRuntimeError, "can't replace hash during iteration");
29422935
}
29432936
hash2 = to_hash(hash2);

0 commit comments

Comments
 (0)
Failed to load comments.