Skip to content

Commit

Permalink
Also avoid hash collisions when computing the size of data
Browse files Browse the repository at this point in the history
For #323
  • Loading branch information
TysonAndre authored and nikic committed Nov 5, 2022
1 parent 11de4db commit f098fc8
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions apc_persist.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ typedef struct _apc_persist_context_t {
static zend_bool apc_persist_calc_zval(apc_persist_context_t *ctxt, const zval *zv);
static void apc_persist_copy_zval_impl(apc_persist_context_t *ctxt, zval *zv);

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

static inline void apc_persist_copy_zval(apc_persist_context_t *ctxt, zval *zv) {
/* No data apart from the zval itself */
if (Z_TYPE_P(zv) < IS_STRING) {
Expand Down Expand Up @@ -104,16 +109,18 @@ void apc_persist_destroy_context(apc_persist_context_t *ctxt) {

static zend_bool apc_persist_calc_memoize(apc_persist_context_t *ctxt, void *ptr) {
zval tmp;
zend_ulong key;
if (!ctxt->memoization_needed) {
return 0;
}

if (zend_hash_index_exists(&ctxt->already_counted, (uintptr_t) ptr)) {
key = apc_shr3((zend_ulong)(uintptr_t) ptr);
if (zend_hash_index_exists(&ctxt->already_counted, key)) {
return 1;
}

ZVAL_NULL(&tmp);
zend_hash_index_add_new(&ctxt->already_counted, (uintptr_t) ptr, &tmp);
zend_hash_index_add_new(&ctxt->already_counted, key, &tmp);
return 0;
}

Expand Down Expand Up @@ -523,11 +530,6 @@ 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, apc_shr3((zend_ulong)(uintptr_t)ptr));
Expand Down

0 comments on commit f098fc8

Please sign in to comment.