Skip to content

Commit 33ef3d6

Browse files
committed
Use separate typedef for bucket comparison function
Avoid performing the same casting dance inside each sort compare function.
1 parent 22ec3bc commit 33ef3d6

File tree

16 files changed

+104
-203
lines changed

16 files changed

+104
-203
lines changed

UPGRADING.INTERNALS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ PHP 8.0 INTERNALS UPGRADE NOTES
1414
k. The 'I' length modifier
1515
l. Some VM instructions switched to IS_TMP_VAR result instead of IS_VAR
1616
m. All internal functions must have arginfo
17+
n. zend_hash_sort compare function signature change
1718

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

106+
n. The zend_hash_sort and zend_hash_minmax APIs now accept a comparison
107+
function with the following signature:
108+
109+
typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
110+
111+
Previously compare_func_t was used, which accepted void pointers.
112+
105113
========================
106114
2. Build system changes
107115
========================

Zend/zend_hash.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2468,7 +2468,7 @@ ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q)
24682468
q->h = h;
24692469
}
24702470

2471-
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, compare_func_t compar, zend_bool renumber)
2471+
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, bucket_compare_func_t compar, zend_bool renumber)
24722472
{
24732473
Bucket *p;
24742474
uint32_t i, j;
@@ -2493,7 +2493,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort, c
24932493
}
24942494
}
24952495

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

@@ -2642,7 +2642,7 @@ ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t co
26422642
}
26432643

26442644

2645-
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag)
2645+
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag)
26462646
{
26472647
uint32_t idx;
26482648
Bucket *p, *res;

Zend/zend_hash.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,11 @@ ZEND_API void ZEND_FASTCALL zend_hash_merge_ex(HashTable *target, HashTable *so
264264
ZEND_API void zend_hash_bucket_swap(Bucket *p, Bucket *q);
265265
ZEND_API void zend_hash_bucket_renum_swap(Bucket *p, Bucket *q);
266266
ZEND_API void zend_hash_bucket_packed_swap(Bucket *p, Bucket *q);
267+
268+
typedef int (*bucket_compare_func_t)(Bucket *a, Bucket *b);
267269
ZEND_API int zend_hash_compare(HashTable *ht1, HashTable *ht2, compare_func_t compar, zend_bool ordered);
268-
ZEND_API void ZEND_FASTCALL zend_hash_sort_ex(HashTable *ht, sort_func_t sort_func, compare_func_t compare_func, zend_bool renumber);
269-
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, compare_func_t compar, uint32_t flag);
270+
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);
271+
ZEND_API zval* ZEND_FASTCALL zend_hash_minmax(const HashTable *ht, bucket_compare_func_t compar, uint32_t flag);
270272

271273
#define zend_hash_sort(ht, compare_func, renumber) \
272274
zend_hash_sort_ex(ht, zend_sort, compare_func, renumber)

Zend/zend_ini.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,8 @@ ZEND_API int zend_copy_ini_directives(void) /* {{{ */
171171
/* }}} */
172172
#endif
173173

174-
static int ini_key_compare(const void *a, const void *b) /* {{{ */
174+
static int ini_key_compare(Bucket *f, Bucket *s) /* {{{ */
175175
{
176-
const Bucket *f;
177-
const Bucket *s;
178-
179-
f = (const Bucket *) a;
180-
s = (const Bucket *) b;
181-
182176
if (!f->key && !s->key) { /* both numeric */
183177
if (f->h > s->h) {
184178
return -1;

Zend/zend_ts_hash.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, co
264264
end_read(source);
265265
}
266266

267-
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber)
267+
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, int renumber)
268268
{
269269
begin_write(ht);
270270
zend_hash_sort_ex(TS_HASH(ht), sort_func, compare_func, renumber);
@@ -284,7 +284,7 @@ ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_fu
284284
return retval;
285285
}
286286

287-
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag)
287+
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, bucket_compare_func_t compar, int flag)
288288
{
289289
zval *retval;
290290

Zend/zend_ts_hash.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ ZEND_API void zend_ts_hash_copy(TsHashTable *target, TsHashTable *source, copy_c
7373
ZEND_API void zend_ts_hash_copy_to_hash(HashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor);
7474
ZEND_API void zend_ts_hash_merge(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, int overwrite);
7575
ZEND_API void zend_ts_hash_merge_ex(TsHashTable *target, TsHashTable *source, copy_ctor_func_t pCopyConstructor, merge_checker_func_t pMergeSource, void *pParam);
76-
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, compare_func_t compare_func, int renumber);
76+
ZEND_API void zend_ts_hash_sort(TsHashTable *ht, sort_func_t sort_func, bucket_compare_func_t compare_func, int renumber);
7777
ZEND_API int zend_ts_hash_compare(TsHashTable *ht1, TsHashTable *ht2, compare_func_t compar, zend_bool ordered);
78-
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, compare_func_t compar, int flag);
78+
ZEND_API zval *zend_ts_hash_minmax(TsHashTable *ht, bucket_compare_func_t compar, int flag);
7979

8080
ZEND_API int zend_ts_hash_num_elements(TsHashTable *ht);
8181

ext/intl/collator/collator_sort.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -208,19 +208,11 @@ static int collator_icu_compare_function(zval *result, zval *op1, zval *op2)
208208
/* {{{ collator_compare_func
209209
* Taken from PHP7 source (array_data_compare).
210210
*/
211-
static int collator_compare_func( const void* a, const void* b )
211+
static int collator_compare_func(Bucket *f, Bucket *s)
212212
{
213-
Bucket *f;
214-
Bucket *s;
215213
zval result;
216-
zval *first;
217-
zval *second;
218-
219-
f = (Bucket *) a;
220-
s = (Bucket *) b;
221-
222-
first = &f->val;
223-
second = &s->val;
214+
zval *first = &f->val;
215+
zval *second = &s->val;
224216

225217
if( INTL_G(compare_func)( &result, first, second) == FAILURE )
226218
return 0;

ext/opcache/jit/zend_jit_disasm_x86.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ static int zend_jit_disasm(const char *name,
292292
}
293293
}
294294

295-
zend_hash_sort(&labels, (compare_func_t)zend_jit_cmp_labels, 0);
295+
zend_hash_sort(&labels, zend_jit_cmp_labels, 0);
296296

297297
/* label numbering */
298298
n = 0; m = 0;

ext/phar/dirstream.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,11 @@ static int phar_add_empty(HashTable *ht, char *arKey, uint32_t nKeyLength) /* {
152152
/**
153153
* Used for sorting directories alphabetically
154154
*/
155-
static int phar_compare_dir_name(const void *a, const void *b) /* {{{ */
155+
static int phar_compare_dir_name(Bucket *f, Bucket *s) /* {{{ */
156156
{
157-
Bucket *f;
158-
Bucket *s;
159-
int result;
160-
161-
f = (Bucket *) a;
162-
s = (Bucket *) b;
163-
result = zend_binary_strcmp(ZSTR_VAL(f->key), ZSTR_LEN(f->key), ZSTR_VAL(s->key), ZSTR_LEN(s->key));
164-
165-
if (result < 0) {
166-
return -1;
167-
} else if (result > 0) {
168-
return 1;
169-
} else {
170-
return 0;
171-
}
157+
int result = zend_binary_strcmp(
158+
ZSTR_VAL(f->key), ZSTR_LEN(f->key), ZSTR_VAL(s->key), ZSTR_LEN(s->key));
159+
return ZEND_NORMALIZE_BOOL(result);
172160
}
173161
/* }}} */
174162

0 commit comments

Comments
 (0)