Skip to content

Commit 5acb4cc

Browse files
TysonAndrenikic
authored andcommitted
Reduce shared memory usage storing the empty array
Reduce shared memory usage in php 7.3+ for `apc.serializer=default`. Store the pointer to the immutable zend_empty_array rather than a brand new array. (APCu is already storing a pointer to static variables elsewhere, e.g. uninitialized_bucket)
1 parent 27782a8 commit 5acb4cc

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

apc_persist.c

+11-2
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ static zend_bool apc_persist_calc_memoize(apc_persist_context_t *ctxt, void *ptr
127127
static zend_bool apc_persist_calc_ht(apc_persist_context_t *ctxt, const HashTable *ht) {
128128
uint32_t idx;
129129

130-
ADD_SIZE(sizeof(HashTable));
131-
if (ht->nNumUsed == 0) {
130+
/* In php 7.3+, this points to the immutable zend_empty_array outside of shared memory. */
131+
if (ht->nNumOfElements == 0) {
132+
#if PHP_VERSION_ID < 70300
133+
ADD_SIZE(sizeof(HashTable));
134+
#endif
132135
return 1;
133136
}
137+
ADD_SIZE(sizeof(HashTable));
134138

135139
/* TODO Too sparse hashtables could be compacted here */
136140
#if PHP_VERSION_ID >= 80200
@@ -314,6 +318,11 @@ static zend_reference *apc_persist_copy_ref(
314318
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] = {HT_INVALID_IDX, HT_INVALID_IDX};
315319

316320
static zend_array *apc_persist_copy_ht(apc_persist_context_t *ctxt, const HashTable *orig_ht) {
321+
#if PHP_VERSION_ID >= 70300
322+
if (orig_ht->nNumOfElements == 0) {
323+
return (HashTable *)&zend_empty_array;
324+
}
325+
#endif
317326
HashTable *ht = COPY(orig_ht, sizeof(HashTable));
318327
uint32_t idx;
319328
apc_persist_add_already_allocated(ctxt, orig_ht, ht);

0 commit comments

Comments
 (0)