Skip to content

Commit

Permalink
Use separate typedef for bucket comparison function
Browse files Browse the repository at this point in the history
Avoid performing the same casting dance inside each sort compare
function.
  • Loading branch information
nikic committed Mar 4, 2020
1 parent 22ec3bc commit 33ef3d6
Show file tree
Hide file tree
Showing 16 changed files with 104 additions and 203 deletions.
8 changes: 8 additions & 0 deletions UPGRADING.INTERNALS
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ PHP 8.0 INTERNALS UPGRADE NOTES
k. The 'I' length modifier
l. Some VM instructions switched to IS_TMP_VAR result instead of IS_VAR
m. All internal functions must have arginfo
n. zend_hash_sort compare function signature change

2. Build system changes
a. Abstract
Expand Down Expand Up @@ -102,6 +103,13 @@ PHP 8.0 INTERNALS UPGRADE NOTES
m. All internal functions and methods are now required to specify arginfo
information, otherwise warnings will be thrown on startup.

n. The zend_hash_sort and zend_hash_minmax APIs now accept a comparison
function with the following signature:

typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);

Previously compare_func_t was used, which accepted void pointers.

========================
2. Build system changes
========================
Expand Down
6 changes: 3 additions & 3 deletions Zend/zend_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -2468,7 +2468,7 @@ ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q)
q->h = h;
}

ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, compare_func_t compar, zend_bool renumber)
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, zend_bool renumber)
{
Bucket *p;
uint32_t i, j;
Expand All @@ -2493,7 +2493,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, c
}
}

sort((void *)ht->arData, i, sizeof(Bucket), compar,
sort((void *)ht->arData, i, sizeof(Bucket), (compare_func_t) compar,
(swap_func_t)(renumber? zend_hash_bucket_renum_swap :
((HT_FLAGS(ht) & HASH_FLAG_PACKED) ? zend_hash_bucket_packed_swap : zend_hash_bucket_swap)));

Expand Down Expand Up @@ -2642,7 +2642,7 @@ ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t co
}


ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag)
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag)
{
uint32_t idx;
Bucket *p, *res;
Expand Down
6 changes: 4 additions & 2 deletions Zend/zend_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,11 @@ ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *so
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q);
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);

typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber);
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, zend_bool renumber);
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag);

#define zend_hash_sort(ht, compare_func, renumber) \
zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)
Expand Down
8 changes: 1 addition & 7 deletions Zend/zend_ini.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,8 @@ ZEND_API int zend_copy_ini_directives(void) /* {{{ */
/* }}} */
#endif

static int ini_key_compare(const void *a, const void *b) /* {{{ */
static int ini_key_compare(Bucket *f, Bucket *s) /* {{{ */
{
const Bucket *f;
const Bucket *s;

f = (const Bucket *) a;
s = (const Bucket *) b;

if (!f->key && !s->key) { /* both numeric */
if (f->h > s->h) {
return -1;
Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_ts_hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, co
end_read(source);
}

ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber)
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, int renumber)
{
begin_write(ht);
zend_hash_sort_ex(TS_HASH(ht), sort_func, compare_func, renumber);
Expand All @@ -284,7 +284,7 @@ ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_fu
return retval;
}

ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag)
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, bucket_compare_func_t compar, int flag)
{
zval *retval;

Expand Down
4 changes: 2 additions & 2 deletions Zend/zend_ts_hash.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_c
ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite);
ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber);
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, int renumber);
ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag);
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, bucket_compare_func_t compar, int flag);

ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);

Expand Down
14 changes: 3 additions & 11 deletions ext/intl/collator/collator_sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,11 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
/* {{{ collator_compare_func
* Taken from PHP7 source (array_data_compare).
*/
static int collator_compare_func( const void* a, const void* b )
static int collator_compare_func(Bucket *f, Bucket *s)
{
Bucket *f;
Bucket *s;
zval result;
zval *first;
zval *second;

f = (Bucket *) a;
s = (Bucket *) b;

first = &f->val;
second = &s->val;
zval *first = &f->val;
zval *second = &s->val;

if( INTL_G(compare_func)( &result, first, second) == FAILURE )
return 0;
Expand Down
2 changes: 1 addition & 1 deletion ext/opcache/jit/zend_jit_disasm_x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static int zend_jit_disasm(const char *name,
}
}

zend_hash_sort(&labels, (compare_func_t)zend_jit_cmp_labels, 0);
zend_hash_sort(&labels, zend_jit_cmp_labels, 0);

/* label numbering */
n = 0; m = 0;
Expand Down
20 changes: 4 additions & 16 deletions ext/phar/dirstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,11 @@ static int phar_add_empty(HashTable *ht, char *arKey, uint32_t nKeyLength) /* {
/**
* Used for sorting directories alphabetically
*/
static int phar_compare_dir_name(const void *a, const void *b) /* {{{ */
static int phar_compare_dir_name(Bucket *f, Bucket *s) /* {{{ */
{
Bucket *f;
Bucket *s;
int result;

f = (Bucket *) a;
s = (Bucket *) b;
result = zend_binary_strcmp(ZSTR_VAL(f->key), ZSTR_LEN(f->key), ZSTR_VAL(s->key), ZSTR_LEN(s->key));

if (result < 0) {
return -1;
} else if (result > 0) {
return 1;
} else {
return 0;
}
int result = zend_binary_strcmp(
ZSTR_VAL(f->key), ZSTR_LEN(f->key), ZSTR_VAL(s->key), ZSTR_LEN(s->key));
return ZEND_NORMALIZE_BOOL(result);
}
/* }}} */

Expand Down
Loading

0 comments on commit 33ef3d6

Please sign in to comment.