Skip to content

Commit 79adb01

Browse files
committed
array_map: Avoid allocation by using Z_EXTRA storage of parameters
1 parent 60a0f10 commit 79adb01

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

ext/standard/array.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6773,11 +6773,11 @@ PHP_FUNCTION(array_all)
67736773
PHP_FUNCTION(array_map)
67746774
{
67756775
zval *arrays = NULL;
6776-
int n_arrays = 0;
6776+
uint32_t n_arrays = 0;
67776777
zval result;
67786778
zend_fcall_info fci;
67796779
zend_fcall_info_cache fci_cache;
6780-
int i;
6780+
uint32_t i;
67816781
uint32_t k, maxlen = 0;
67826782

67836783
ZEND_PARSE_PARAMETERS_START(2, -1)
@@ -6862,10 +6862,10 @@ PHP_FUNCTION(array_map)
68626862
}
68636863
}
68646864

6865-
uint32_t *array_pos = ecalloc(n_arrays, sizeof(HashPosition));
68666865
array_init_size(return_value, maxlen);
68676866

68686867
if (!ZEND_FCI_INITIALIZED(fci)) {
6868+
uint32_t *array_pos = ecalloc(n_arrays, sizeof(HashPosition));
68696869
zval zv;
68706870

68716871
/* We iterate through all the arrays at once. */
@@ -6909,9 +6909,16 @@ PHP_FUNCTION(array_map)
69096909

69106910
zend_hash_next_index_insert_new(Z_ARRVAL_P(return_value), &result);
69116911
}
6912+
6913+
efree(array_pos);
69126914
} else {
69136915
zval *params = (zval *)safe_emalloc(n_arrays, sizeof(zval), 0);
69146916

6917+
/* Remember next starting point in the array, initialize those as zeros. */
6918+
for (i = 0; i < n_arrays; i++) {
6919+
Z_EXTRA(params[i]) = 0;
6920+
}
6921+
69156922
fci.retval = &result;
69166923
fci.param_count = n_arrays;
69176924
fci.params = params;
@@ -6921,15 +6928,15 @@ PHP_FUNCTION(array_map)
69216928
for (i = 0; i < n_arrays; i++) {
69226929
/* If this array still has elements, add the current one to the
69236930
* parameter list, otherwise use null value. */
6924-
uint32_t pos = array_pos[i];
6931+
uint32_t pos = Z_EXTRA(params[i]);
69256932
if (HT_IS_PACKED(Z_ARRVAL(arrays[i]))) {
69266933
while (1) {
69276934
if (pos >= Z_ARRVAL(arrays[i])->nNumUsed) {
69286935
ZVAL_NULL(&params[i]);
69296936
break;
69306937
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arPacked[pos]) != IS_UNDEF) {
69316938
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arPacked[pos]);
6932-
array_pos[i] = pos + 1;
6939+
Z_EXTRA(params[i]) = pos + 1;
69336940
break;
69346941
}
69356942
pos++;
@@ -6941,7 +6948,7 @@ PHP_FUNCTION(array_map)
69416948
break;
69426949
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arData[pos].val) != IS_UNDEF) {
69436950
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arData[pos].val);
6944-
array_pos[i] = pos + 1;
6951+
Z_EXTRA(params[i]) = pos + 1;
69456952
break;
69466953
}
69476954
pos++;
@@ -6954,7 +6961,6 @@ PHP_FUNCTION(array_map)
69546961
ZEND_IGNORE_VALUE(ret);
69556962

69566963
if (Z_TYPE(result) == IS_UNDEF) {
6957-
efree(array_pos);
69586964
efree(params);
69596965
RETURN_THROWS();
69606966
}
@@ -6964,7 +6970,6 @@ PHP_FUNCTION(array_map)
69646970

69656971
efree(params);
69666972
}
6967-
efree(array_pos);
69686973
}
69696974
}
69706975
/* }}} */

0 commit comments

Comments
 (0)