Skip to content

Commit 751fc68

Browse files
committed
ext/phar: Optimize temporary string handling in Phar directory streams
1 parent 0fff3cc commit 751fc68

2 files changed

Lines changed: 7 additions & 12 deletions

File tree

UPGRADING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,9 @@ PHP 8.6 UPGRADE NOTES
481481
. Improved performance of indentation generation in json_encode()
482482
when using PHP_JSON_PRETTY_PRINT.
483483

484+
- Phar:
485+
. Reduced temporary allocations when iterating Phar directories.
486+
484487
- Standard:
485488
. Improved performance of array_fill_keys().
486489
. Improved performance of array_map() with multiple arrays passed.

ext/phar/dirstream.c

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static int phar_compare_dir_name(Bucket *f, Bucket *s) /* {{{ */
149149
static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const HashTable *manifest) /* {{{ */
150150
{
151151
HashTable *data;
152-
char *entry;
152+
const char *entry;
153153

154154
ALLOC_HASHTABLE(data);
155155
zend_hash_init(data, 64, NULL, NULL, 0);
@@ -181,9 +181,7 @@ static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const Has
181181
/* the entry has a path separator and is a subdirectory */
182182
keylen = has_slash - ZSTR_VAL(str_key);
183183
}
184-
entry = safe_emalloc(keylen, 1, 1);
185-
memcpy(entry, ZSTR_VAL(str_key), keylen);
186-
entry[keylen] = '\0';
184+
entry = ZSTR_VAL(str_key);
187185
} else {
188186
if (0 != memcmp(ZSTR_VAL(str_key), dir, dirlen)) {
189187
/* entry in directory not found */
@@ -201,16 +199,12 @@ static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const Has
201199
if (has_slash) {
202200
/* is subdirectory */
203201
save -= dirlen + 1;
204-
entry = safe_emalloc(has_slash - save + dirlen, 1, 1);
205-
memcpy(entry, save + dirlen + 1, has_slash - save - dirlen - 1);
206202
keylen = has_slash - save - dirlen - 1;
207-
entry[keylen] = '\0';
203+
entry = save + dirlen + 1;
208204
} else {
209205
/* is file */
210206
save -= dirlen + 1;
211-
entry = safe_emalloc(keylen - dirlen, 1, 1);
212-
memcpy(entry, save + dirlen + 1, keylen - dirlen - 1);
213-
entry[keylen - dirlen - 1] = '\0';
207+
entry = save + dirlen + 1;
214208
keylen = keylen - dirlen - 1;
215209
}
216210
}
@@ -227,8 +221,6 @@ static php_stream *phar_make_dirstream(const char *dir, size_t dirlen, const Has
227221
ZVAL_NULL(&dummy);
228222
zend_hash_str_update(data, entry, keylen, &dummy);
229223
}
230-
231-
efree(entry);
232224
} ZEND_HASH_FOREACH_END();
233225

234226
if (FAILURE != zend_hash_has_more_elements(data)) {

0 commit comments

Comments
 (0)