Skip to content

Commit 08de135

Browse files
committed
zend_weakrefs: Include zend_weakmap_free_obj() optimization in zend_weakrefs_hash_clean()
1 parent 4884fec commit 08de135

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

Zend/zend_weakrefs.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ static void zend_weakref_unregister(zend_object *object, void *payload, bool wea
138138
if (weakref_free) {
139139
zend_weakref_unref_single(ptr, tag, object);
140140
} else {
141-
/* The optimization of skipping unref is only used in the destructor of WeakMap */
142-
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
141+
/* The optimization of skipping unref is used for zend_weakrefs_hash_clean_ex() */
142+
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP || ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_BARE_HT);
143143
}
144144
return;
145145
}
@@ -163,8 +163,8 @@ static void zend_weakref_unregister(zend_object *object, void *payload, bool wea
163163
zend_weakref_unref_single(
164164
ZEND_WEAKREF_GET_PTR(payload), ZEND_WEAKREF_GET_TAG(payload), object);
165165
} else {
166-
/* The optimization of skipping unref is only used in the destructor of WeakMap */
167-
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP);
166+
/* The optimization of skipping unref is used for zend_weakrefs_hash_clean_ex() */
167+
ZEND_ASSERT(ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_MAP || ZEND_WEAKREF_GET_TAG(payload) == ZEND_WEAKREF_TAG_BARE_HT);
168168
}
169169
}
170170

@@ -187,13 +187,20 @@ ZEND_API zend_result zend_weakrefs_hash_del(HashTable *ht, zend_object *key) {
187187
return FAILURE;
188188
}
189189

190-
ZEND_API void zend_weakrefs_hash_clean(HashTable *ht) {
190+
static void zend_weakrefs_hash_clean_ex(HashTable *ht, int type) {
191191
zend_ulong obj_key;
192-
ZEND_HASH_FOREACH_NUM_KEY(ht, obj_key) {
193-
/* Call zend_weakref_unregister to avoid an unneccessary
194-
* zend_hash_index_find lookup in zend_weakrefs_hash_del. */
195-
zend_weakref_unregister(zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(ht, ZEND_WEAKREF_TAG_BARE_HT), 1);
192+
ZEND_HASH_MAP_FOREACH_NUM_KEY(ht, obj_key) {
193+
/* Optimization: Don't call zend_weakref_unref_single to free individual entries from ht when unregistering (which would do a hash table lookup, call zend_hash_index_del, and skip over any bucket collisions).
194+
* Let freeing the corresponding values for WeakMap entries be done in zend_hash_clean, freeing objects sequentially.
195+
* The performance difference is notable for larger WeakMaps with worse cache locality. */
196+
zend_weakref_unregister(
197+
zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(ht, type), 0);
196198
} ZEND_HASH_FOREACH_END();
199+
zend_hash_clean(ht);
200+
}
201+
202+
ZEND_API void zend_weakrefs_hash_clean(HashTable *ht) {
203+
zend_weakrefs_hash_clean_ex(ht, ZEND_WEAKREF_TAG_BARE_HT);
197204
}
198205

199206
void zend_weakrefs_init(void) {
@@ -342,14 +349,7 @@ static zend_object *zend_weakmap_create_object(zend_class_entry *ce)
342349
static void zend_weakmap_free_obj(zend_object *object)
343350
{
344351
zend_weakmap *wm = zend_weakmap_from(object);
345-
zend_ulong obj_key;
346-
ZEND_HASH_MAP_FOREACH_NUM_KEY(&wm->ht, obj_key) {
347-
/* Optimization: Don't call zend_weakref_unref_single to free individual entries from wm->ht when unregistering (which would do a hash table lookup, call zend_hash_index_del, and skip over any bucket collisions).
348-
* Let freeing the corresponding values for WeakMap entries be done in zend_hash_destroy, freeing objects sequentially.
349-
* The performance difference is notable for larger WeakMaps with worse cache locality. */
350-
zend_weakref_unregister(
351-
zend_weakref_key_to_object(obj_key), ZEND_WEAKREF_ENCODE(&wm->ht, ZEND_WEAKREF_TAG_MAP), 0);
352-
} ZEND_HASH_FOREACH_END();
352+
zend_weakrefs_hash_clean_ex(&wm->ht, ZEND_WEAKREF_TAG_MAP);
353353
zend_hash_destroy(&wm->ht);
354354
zend_object_std_dtor(&wm->std);
355355
}

0 commit comments

Comments
 (0)