Skip to content

Commit 2b4582e

Browse files
committed
array_map: Avoid allocation by using Z_EXTRA storage of parameters
1 parent c349660 commit 2b4582e

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
@@ -6772,11 +6772,11 @@ PHP_FUNCTION(array_all)
67726772
PHP_FUNCTION(array_map)
67736773
{
67746774
zval *arrays = NULL;
6775-
int n_arrays = 0;
6775+
uint32_t n_arrays = 0;
67766776
zval result;
67776777
zend_fcall_info fci;
67786778
zend_fcall_info_cache fci_cache;
6779-
int i;
6779+
uint32_t i;
67806780
uint32_t k, maxlen = 0;
67816781

67826782
ZEND_PARSE_PARAMETERS_START(2, -1)
@@ -6861,10 +6861,10 @@ PHP_FUNCTION(array_map)
68616861
}
68626862
}
68636863

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

68676866
if (!ZEND_FCI_INITIALIZED(fci)) {
6867+
uint32_t *array_pos = ecalloc(n_arrays, sizeof(HashPosition));
68686868
zval zv;
68696869

68706870
/* We iterate through all the arrays at once. */
@@ -6908,9 +6908,16 @@ PHP_FUNCTION(array_map)
69086908

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

6916+
/* Remember next starting point in the array, initialize those as zeros. */
6917+
for (i = 0; i < n_arrays; i++) {
6918+
Z_EXTRA(params[i]) = 0;
6919+
}
6920+
69146921
fci.retval = &result;
69156922
fci.param_count = n_arrays;
69166923
fci.params = params;
@@ -6920,15 +6927,15 @@ PHP_FUNCTION(array_map)
69206927
for (i = 0; i < n_arrays; i++) {
69216928
/* If this array still has elements, add the current one to the
69226929
* parameter list, otherwise use null value. */
6923-
uint32_t pos = array_pos[i];
6930+
uint32_t pos = Z_EXTRA(params[i]);
69246931
if (HT_IS_PACKED(Z_ARRVAL(arrays[i]))) {
69256932
while (1) {
69266933
if (pos >= Z_ARRVAL(arrays[i])->nNumUsed) {
69276934
ZVAL_NULL(&params[i]);
69286935
break;
69296936
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arPacked[pos]) != IS_UNDEF) {
69306937
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arPacked[pos]);
6931-
array_pos[i] = pos + 1;
6938+
Z_EXTRA(params[i]) = pos + 1;
69326939
break;
69336940
}
69346941
pos++;
@@ -6940,7 +6947,7 @@ PHP_FUNCTION(array_map)
69406947
break;
69416948
} else if (Z_TYPE(Z_ARRVAL(arrays[i])->arData[pos].val) != IS_UNDEF) {
69426949
ZVAL_COPY_VALUE(&params[i], &Z_ARRVAL(arrays[i])->arData[pos].val);
6943-
array_pos[i] = pos + 1;
6950+
Z_EXTRA(params[i]) = pos + 1;
69446951
break;
69456952
}
69466953
pos++;
@@ -6953,7 +6960,6 @@ PHP_FUNCTION(array_map)
69536960
ZEND_IGNORE_VALUE(ret);
69546961

69556962
if (Z_TYPE(result) == IS_UNDEF) {
6956-
efree(array_pos);
69576963
efree(params);
69586964
RETURN_THROWS();
69596965
}
@@ -6963,7 +6969,6 @@ PHP_FUNCTION(array_map)
69636969

69646970
efree(params);
69656971
}
6966-
efree(array_pos);
69676972
}
69686973
}
69696974
/* }}} */

0 commit comments

Comments
 (0)