Skip to content

Commit 1d5f359

Browse files
committed
Use spl_filesystem_object.file_name for SPL_FS_DIR as a cache and prevent multiple file name reconstruction.
1 parent 9f826e8 commit 1d5f359

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

ext/spl/spl_directory.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -202,25 +202,25 @@ PHPAPI char* spl_filesystem_object_get_path(spl_filesystem_object *intern, size_
202202

203203
static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *intern) /* {{{ */
204204
{
205-
char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
205+
if (intern->file_name) {
206+
/* already known */
207+
return SUCCESS;
208+
}
206209

207210
switch (intern->type) {
208211
case SPL_FS_INFO:
209212
case SPL_FS_FILE:
210-
if (!intern->file_name) {
211-
zend_throw_error(NULL, "Object not initialized");
212-
return FAILURE;
213-
}
213+
zend_throw_error(NULL, "Object not initialized");
214+
return FAILURE;
214215
break;
215216
case SPL_FS_DIR:
216217
{
217218
size_t name_len;
218219
size_t path_len = 0;
219-
char *path = spl_filesystem_object_get_path(intern, &path_len);
220+
char *path;
221+
char slash = SPL_HAS_FLAG(intern->flags, SPL_FILE_DIR_UNIXPATHS) ? '/' : DEFAULT_SLASH;
220222

221-
if (intern->file_name) {
222-
zend_string_release(intern->file_name);
223-
}
223+
path = spl_filesystem_object_get_path(intern, &path_len);
224224
/* if there is parent path, amend it, otherwise just use the given path as is */
225225
name_len = strlen(intern->u.dir.entry.d_name);
226226
if (path_len == 0) {
@@ -241,6 +241,11 @@ static inline int spl_filesystem_object_get_file_name(spl_filesystem_object *int
241241

242242
static int spl_filesystem_dir_read(spl_filesystem_object *intern) /* {{{ */
243243
{
244+
if (intern->file_name) {
245+
/* invalidate */
246+
zend_string_release(intern->file_name);
247+
intern->file_name = NULL;
248+
}
244249
if (!intern->u.dir.dirp || !php_stream_readdir(intern->u.dir.dirp, &intern->u.dir.entry)) {
245250
intern->u.dir.entry.d_name[0] = '\0';
246251
return 0;

0 commit comments

Comments
 (0)