Skip to content

Commit ff32c24

Browse files
committed
Add notes, be explicit about chosen sizes.
(no change) Fix formatting
1 parent 3325dd7 commit ff32c24

11 files changed

+22
-19
lines changed

Zend/zend_API.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3271,7 +3271,7 @@ ZEND_API zend_bool zend_make_callable(zval *callable, zend_string **callable_nam
32713271
if (zend_is_callable_ex(callable, NULL, 0, callable_name, &fcc, NULL)) {
32723272
if (Z_TYPE_P(callable) == IS_STRING && fcc.calling_scope) {
32733273
zval_ptr_dtor_str(callable);
3274-
array_init(callable);
3274+
array_init_size(callable, 2);
32753275
add_next_index_str(callable, zend_string_copy(fcc.calling_scope->name));
32763276
add_next_index_str(callable, zend_string_copy(fcc.function_handler->common.function_name));
32773277
}

Zend/zend_ast.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ static int zend_ast_add_unpacked_element(zval *result, zval *expr) {
451451
HashTable *ht = Z_ARRVAL_P(expr);
452452
zval *val;
453453
zend_string *key;
454-
454+
455455
ZEND_HASH_FOREACH_STR_KEY_VAL(ht, key, val) {
456456
if (key) {
457457
zend_throw_error(NULL, "Cannot unpack array with string keys");
@@ -661,13 +661,15 @@ ZEND_API int ZEND_FASTCALL zend_ast_evaluate(zval *result, zend_ast *ast, zend_c
661661
{
662662
uint32_t i;
663663
zend_ast_list *list = zend_ast_get_list(ast);
664+
uint32_t n_children = list->children;
664665

665-
if (!list->children) {
666+
if (!n_children) {
666667
ZVAL_EMPTY_ARRAY(result);
667668
break;
668669
}
669-
array_init(result);
670-
for (i = 0; i < list->children; i++) {
670+
/** Usually, there won't be an AST_UNPACK or duplicate keys. Assume that's the initial capacity. */
671+
array_init_size(result, n_children);
672+
for (i = 0; i < n_children; i++) {
671673
zend_ast *elem = list->child[i];
672674
if (elem->kind == ZEND_AST_UNPACK) {
673675
if (UNEXPECTED(zend_ast_evaluate(&op1, elem->child[0], scope) != SUCCESS)) {

Zend/zend_builtin_functions.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ ZEND_FUNCTION(gc_status)
280280

281281
zend_gc_get_status(&status);
282282

283-
array_init_size(return_value, 3);
283+
array_init_size(return_value, 4);
284284

285285
add_assoc_long_ex(return_value, "runs", sizeof("runs")-1, (long)status.runs);
286286
add_assoc_long_ex(return_value, "collected", sizeof("collected")-1, (long)status.collected);

Zend/zend_hash.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ static zend_always_inline void _zend_hash_init_int(HashTable *ht, uint32_t nSize
245245
ht->nInternalPointer = 0;
246246
ht->nNextFreeElement = ZEND_LONG_MIN;
247247
ht->pDestructor = pDestructor;
248+
// TODO: Add a way to specify the size of a packed table exactly? Currently, send_hash_check_size rounds up to the nearest power of 2.
248249
ht->nTableSize = zend_hash_check_size(nSize);
249250
}
250251

@@ -272,7 +273,7 @@ ZEND_API HashTable* ZEND_FASTCALL zend_new_pair(zval *val1, zval *val2)
272273
{
273274
Bucket *p;
274275
HashTable *ht = emalloc(sizeof(HashTable));
275-
// XXX will need to adjust all calls like this if HT_MIN_SIZE goes below 2
276+
// XXX: Currently, HT_MIN_SIZE == 2. will need to adjust all calls like this if HT_MIN_SIZE goes below 2
276277
_zend_hash_init_int(ht, HT_MIN_SIZE, ZVAL_PTR_DTOR, 0);
277278
ht->nNumUsed = ht->nNumOfElements = ht->nNextFreeElement = 2;
278279
zend_hash_real_init_packed_ex(ht);
@@ -328,7 +329,7 @@ ZEND_API void ZEND_FASTCALL zend_hash_packed_to_hash(HashTable *ht)
328329
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
329330
Bucket *old_buckets = ht->arData;
330331
uint32_t nSize = ht->nTableSize;
331-
uint32_t nNewSize = nSize >= HT_MIN_SIZE_UNPACKED ? nSize : HT_MIN_SIZE_UNPACKED;
332+
uint32_t nNewSize = nSize >= HT_MIN_SIZE_UNPACKED ? nSize : HT_MIN_SIZE_UNPACKED;
332333

333334
HT_ASSERT_RC1(ht);
334335
HT_FLAGS(ht) &= ~HASH_FLAG_PACKED;
@@ -1170,9 +1171,9 @@ static void ZEND_FASTCALL zend_hash_do_resize(HashTable *ht)
11701171
} else if (ht->nTableSize < HT_MAX_SIZE) { /* Let's double the table size */
11711172
void *new_data, *old_data = HT_GET_DATA_ADDR(ht);
11721173
uint32_t nSize = ht->nTableSize + ht->nTableSize;
1173-
if (nSize < HT_MIN_SIZE_UNPACKED) {
1174-
nSize = HT_MIN_SIZE_UNPACKED;
1175-
}
1174+
if (nSize < HT_MIN_SIZE_UNPACKED) {
1175+
nSize = HT_MIN_SIZE_UNPACKED;
1176+
}
11761177
Bucket *old_buckets = ht->arData;
11771178

11781179
ht->nTableSize = nSize;

ext/standard/basic_functions.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3939,7 +3939,7 @@ PHP_FUNCTION(time_nanosleep)
39393939
if (!nanosleep(&php_req, &php_rem)) {
39403940
RETURN_TRUE;
39413941
} else if (errno == EINTR) {
3942-
array_init(return_value);
3942+
array_init_size(return_value, 2);
39433943
add_assoc_long_ex(return_value, "seconds", sizeof("seconds")-1, php_rem.tv_sec);
39443944
add_assoc_long_ex(return_value, "nanoseconds", sizeof("nanoseconds")-1, php_rem.tv_nsec);
39453945
return;
@@ -4205,7 +4205,7 @@ PHP_FUNCTION(error_get_last)
42054205
}
42064206

42074207
if (PG(last_error_message)) {
4208-
array_init(return_value);
4208+
array_init_size(return_value, 4);
42094209
add_assoc_long_ex(return_value, "type", sizeof("type")-1, PG(last_error_type));
42104210
add_assoc_string_ex(return_value, "message", sizeof("message")-1, PG(last_error_message));
42114211
add_assoc_string_ex(return_value, "file", sizeof("file")-1, PG(last_error_file)?PG(last_error_file):"-");

ext/standard/datetime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ PHP_FUNCTION(strptime)
9090
RETURN_FALSE;
9191
}
9292

93-
array_init(return_value);
93+
array_init_size(return_value, 9);
9494
add_assoc_long(return_value, "tm_sec", parsed_time.tm_sec);
9595
add_assoc_long(return_value, "tm_min", parsed_time.tm_min);
9696
add_assoc_long(return_value, "tm_hour", parsed_time.tm_hour);

ext/standard/dir.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ PHP_FUNCTION(scandir)
584584
RETURN_FALSE;
585585
}
586586

587-
array_init(return_value);
587+
array_init_size(return_value, n);
588588

589589
for (i = 0; i < n; i++) {
590590
add_next_index_str(return_value, namelist[i]);

ext/standard/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1536,7 +1536,7 @@ PHP_NAMED_FUNCTION(php_if_fstat)
15361536
RETURN_FALSE;
15371537
}
15381538

1539-
array_init(return_value);
1539+
array_init_size(return_value, 26);
15401540

15411541
ZVAL_LONG(&stat_dev, stat_ssb.sb.st_dev);
15421542
ZVAL_LONG(&stat_ino, stat_ssb.sb.st_ino);

ext/standard/scanf.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ PHPAPI int php_sscanf_internal( char *string, char *format,
633633
zval tmp;
634634

635635
/* allocate an array for return */
636-
array_init(return_value);
636+
array_init_size(return_value, totalVars);
637637

638638
for (i = 0; i < totalVars; i++) {
639639
ZVAL_NULL(&tmp);

ext/standard/streamsfuncs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ PHP_FUNCTION(stream_socket_pair)
6868
RETURN_FALSE;
6969
}
7070

71-
array_init(return_value);
71+
array_init_size(return_value, 2);
7272

7373
s1 = php_stream_sock_open_from_socket(pair[0], 0);
7474
s2 = php_stream_sock_open_from_socket(pair[1], 0);

ext/tokenizer/tokenizer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static void add_token(zval *return_value, int token_type,
9696
unsigned char *text, size_t leng, int lineno) {
9797
if (token_type >= 256) {
9898
zval keyword;
99-
array_init(&keyword);
99+
array_init_size(&keyword, 3);
100100
add_next_index_long(&keyword, token_type);
101101
if (leng == 1) {
102102
add_next_index_str(&keyword, ZSTR_CHAR(text[0]));

0 commit comments

Comments
 (0)