Skip to content

Commit

Permalink
Optimize unserializing arrays with apc.serializer=default
Browse files Browse the repository at this point in the history
The pointer data apcu sees is usually aligned to 16 bytes on 64-bit
systems using emalloc. It is guaranteed to be aligned to a power of
at least ZEND_MM_ALIGNMENT_LOG2.
Preserve the original bits just in case, similar to opcache's zend_rotr3.

Benchmarking this for repeatedly unserializing large arrays with small strings
and apc.serializer=default, this showed a moderate performance improvement.
(note that apcu_fetch returns a different array on every call due to the
possibility of cache evictions)

For an array with 65536 small string keys and values:
Repeated apcu_fetch went from 3.5 seconds -> 3.0 seconds
For an array with 4096 small string keys and values:
Repeated apcu_fetch went from 1.2 seconds -> 0.9 seconds

For #323
  • Loading branch information
TysonAndre authored and nikic committed Oct 24, 2022
1 parent 532dd62 commit 98b2616
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions apc_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -521,17 +521,22 @@ static zend_bool apc_unpersist_serialized(
return 0;
}

/* Used to reduce hash collisions when using pointers in hash tables. (#175) */
static zend_ulong apc_shr3(zend_ulong index) {
return (index >> 3) | (index << (SIZEOF_ZEND_LONG * 8 - 3));
}

static inline void *apc_unpersist_get_already_copied(apc_unpersist_context_t *ctxt, void *ptr) {
if (ctxt->memoization_needed) {
return zend_hash_index_find_ptr(&ctxt->already_copied, (uintptr_t) ptr);
return zend_hash_index_find_ptr(&ctxt->already_copied, apc_shr3((zend_ulong)(uintptr_t)ptr));
}
return NULL;
}

static inline void apc_unpersist_add_already_copied(
apc_unpersist_context_t *ctxt, const void *old_ptr, void *new_ptr) {
if (ctxt->memoization_needed) {
zend_hash_index_add_new_ptr(&ctxt->already_copied, (uintptr_t) old_ptr, new_ptr);
zend_hash_index_add_new_ptr(&ctxt->already_copied, apc_shr3((zend_ulong)(uintptr_t)old_ptr), new_ptr);
}
}

Expand Down

0 comments on commit 98b2616

Please sign in to comment.