Skip to content

Commit f19d502

Browse files
committed
Carefully control inlining
1 parent 48d7b43 commit f19d502

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

ext/standard/array.c

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -132,32 +132,40 @@ PHP_MSHUTDOWN_FUNCTION(array) /* {{{ */
132132
}
133133
/* }}} */
134134

135+
static zend_never_inline ZEND_COLD int stable_sort_fallback(Bucket *a, Bucket *b) {
136+
if (Z_EXTRA(a->val) > Z_EXTRA(b->val)) {
137+
return 1;
138+
} else if (Z_EXTRA(a->val) < Z_EXTRA(b->val)) {
139+
return -1;
140+
} else {
141+
return 0;
142+
}
143+
}
144+
135145
#define RETURN_STABLE_SORT(a, b, result) do { \
136146
int _result = (result); \
137147
if (EXPECTED(_result)) { \
138148
return _result; \
139149
} \
140-
if (Z_EXTRA((a)->val) > Z_EXTRA((b)->val)) { \
141-
return 1; \
142-
} else if (Z_EXTRA((a)->val) < Z_EXTRA((b)->val)) { \
143-
return -1; \
144-
} else { \
145-
return 0; \
146-
} \
150+
return stable_sort_fallback((a), (b)); \
147151
} while (0)
148152

153+
/* Generate inlined unstable and stable variants, and non-inlined reversed variants. */
149154
#define DEFINE_SORT_VARIANTS(name) \
150-
static int php_array_reverse_##name##_unstable(Bucket *a, Bucket *b) { \
151-
return php_array_##name##_unstable(a, b) * -1; \
155+
static zend_never_inline int php_array_##name##_unstable(Bucket *a, Bucket *b) { \
156+
return php_array_##name##_unstable_i(a, b); \
152157
} \
153-
static int php_array_##name(Bucket *a, Bucket *b) { \
154-
RETURN_STABLE_SORT(a, b, php_array_##name##_unstable(a, b)); \
158+
static zend_never_inline int php_array_##name(Bucket *a, Bucket *b) { \
159+
RETURN_STABLE_SORT(a, b, php_array_##name##_unstable_i(a, b)); \
160+
} \
161+
static zend_never_inline int php_array_reverse_##name##_unstable(Bucket *a, Bucket *b) { \
162+
return php_array_##name##_unstable(a, b) * -1; \
155163
} \
156-
static int php_array_reverse_##name(Bucket *a, Bucket *b) { \
164+
static zend_never_inline int php_array_reverse_##name(Bucket *a, Bucket *b) { \
157165
RETURN_STABLE_SORT(a, b, php_array_reverse_##name##_unstable(a, b)); \
158166
} \
159167

160-
static int php_array_key_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
168+
static zend_always_inline int php_array_key_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
161169
{
162170
zend_uchar t;
163171
zend_long l1, l2;
@@ -196,7 +204,7 @@ static int php_array_key_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
196204
}
197205
/* }}} */
198206

199-
static int php_array_key_compare_numeric_unstable(Bucket *f, Bucket *s) /* {{{ */
207+
static zend_always_inline int php_array_key_compare_numeric_unstable_i(Bucket *f, Bucket *s) /* {{{ */
200208
{
201209
if (f->key == NULL && s->key == NULL) {
202210
return (zend_long)f->h > (zend_long)s->h ? 1 : -1;
@@ -217,7 +225,7 @@ static int php_array_key_compare_numeric_unstable(Bucket *f, Bucket *s) /* {{{ *
217225
}
218226
/* }}} */
219227

220-
static int php_array_key_compare_string_case_unstable(Bucket *f, Bucket *s) /* {{{ */
228+
static zend_always_inline int php_array_key_compare_string_case_unstable_i(Bucket *f, Bucket *s) /* {{{ */
221229
{
222230
const char *s1, *s2;
223231
size_t l1, l2;
@@ -242,7 +250,7 @@ static int php_array_key_compare_string_case_unstable(Bucket *f, Bucket *s) /* {
242250
}
243251
/* }}} */
244252

245-
static int php_array_key_compare_string_unstable(Bucket *f, Bucket *s) /* {{{ */
253+
static zend_always_inline int php_array_key_compare_string_unstable_i(Bucket *f, Bucket *s) /* {{{ */
246254
{
247255
const char *s1, *s2;
248256
size_t l1, l2;
@@ -316,7 +324,7 @@ static int php_array_reverse_key_compare_string_natural(Bucket *a, Bucket *b) /*
316324
}
317325
/* }}} */
318326

319-
static int php_array_key_compare_string_locale_unstable(Bucket *f, Bucket *s) /* {{{ */
327+
static zend_always_inline int php_array_key_compare_string_locale_unstable_i(Bucket *f, Bucket *s) /* {{{ */
320328
{
321329
const char *s1, *s2;
322330
char buf1[MAX_LENGTH_OF_LONG + 1];
@@ -336,7 +344,7 @@ static int php_array_key_compare_string_locale_unstable(Bucket *f, Bucket *s) /*
336344
}
337345
/* }}} */
338346

339-
static inline int php_array_data_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
347+
static zend_always_inline int php_array_data_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
340348
{
341349
zval *first = &f->val;
342350
zval *second = &s->val;
@@ -351,7 +359,7 @@ static inline int php_array_data_compare_unstable(Bucket *f, Bucket *s) /* {{{ *
351359
}
352360
/* }}} */
353361

354-
static int php_array_data_compare_numeric_unstable(Bucket *f, Bucket *s) /* {{{ */
362+
static zend_always_inline int php_array_data_compare_numeric_unstable_i(Bucket *f, Bucket *s) /* {{{ */
355363
{
356364
zval *first = &f->val;
357365
zval *second = &s->val;
@@ -367,7 +375,7 @@ static int php_array_data_compare_numeric_unstable(Bucket *f, Bucket *s) /* {{{
367375
}
368376
/* }}} */
369377

370-
static int php_array_data_compare_string_case_unstable(Bucket *f, Bucket *s) /* {{{ */
378+
static zend_always_inline int php_array_data_compare_string_case_unstable_i(Bucket *f, Bucket *s) /* {{{ */
371379
{
372380
zval *first = &f->val;
373381
zval *second = &s->val;
@@ -383,7 +391,7 @@ static int php_array_data_compare_string_case_unstable(Bucket *f, Bucket *s) /*
383391
}
384392
/* }}} */
385393

386-
static int php_array_data_compare_string_unstable(Bucket *f, Bucket *s) /* {{{ */
394+
static zend_always_inline int php_array_data_compare_string_unstable_i(Bucket *f, Bucket *s) /* {{{ */
387395
{
388396
zval *first = &f->val;
389397
zval *second = &s->val;
@@ -413,19 +421,19 @@ static int php_array_natural_general_compare(Bucket *f, Bucket *s, int fold_case
413421
}
414422
/* }}} */
415423

416-
static int php_array_natural_compare_unstable(Bucket *a, Bucket *b) /* {{{ */
424+
static zend_always_inline int php_array_natural_compare_unstable_i(Bucket *a, Bucket *b) /* {{{ */
417425
{
418426
return php_array_natural_general_compare(a, b, 0);
419427
}
420428
/* }}} */
421429

422-
static int php_array_natural_case_compare_unstable(Bucket *a, Bucket *b) /* {{{ */
430+
static zend_always_inline int php_array_natural_case_compare_unstable_i(Bucket *a, Bucket *b) /* {{{ */
423431
{
424432
return php_array_natural_general_compare(a, b, 1);
425433
}
426434
/* }}} */
427435

428-
static int php_array_data_compare_string_locale_unstable(Bucket *f, Bucket *s) /* {{{ */
436+
static int php_array_data_compare_string_locale_unstable_i(Bucket *f, Bucket *s) /* {{{ */
429437
{
430438
zval *first = &f->val;
431439
zval *second = &s->val;
@@ -911,7 +919,7 @@ PHP_FUNCTION(rsort)
911919
}
912920
/* }}} */
913921

914-
static int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
922+
static inline int php_array_user_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
915923
{
916924
zval args[2];
917925
zval retval;
@@ -1021,7 +1029,7 @@ PHP_FUNCTION(uasort)
10211029
}
10221030
/* }}} */
10231031

1024-
static int php_array_user_key_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
1032+
static inline int php_array_user_key_compare_unstable(Bucket *f, Bucket *s) /* {{{ */
10251033
{
10261034
zval args[2];
10271035
zval retval;

0 commit comments

Comments
 (0)