New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance opt on scandir() usage #5511
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -624,12 +624,9 @@ public static function ieCssSplitter($compiled_css, $cache_path, $refresh = fals | |
$protocol_link = Tools::getCurrentUrlProtocolPrefix(); | ||
//return cached css | ||
if (!$refresh) { | ||
$cached_files = scandir($cache_path); | ||
foreach ($cached_files as $file) { | ||
if ($file != '.' && $file != '..') { | ||
$css_url = str_replace(_PS_ROOT_DIR_, '', $protocol_link.Tools::getMediaServer('').$cache_path.DIRECTORY_SEPARATOR.$file); | ||
$splitted_css[$css_url] = 'all'; | ||
} | ||
foreach (array_diff(scandir($cache_path), array('..', '.')) as $file) { | ||
$css_url = str_replace(_PS_ROOT_DIR_, '', $protocol_link.Tools::getMediaServer('').$cache_path.DIRECTORY_SEPARATOR.$file); | ||
$splitted_css[$css_url] = 'all'; | ||
} | ||
return array('lteIE9' => $splitted_css); | ||
} | ||
|
@@ -775,10 +772,8 @@ public static function clearCache() | |
{ | ||
foreach (array(_PS_THEME_DIR_.'cache') as $dir) { | ||
if (file_exists($dir)) { | ||
foreach (scandir($dir) as $file) { | ||
if ($file[0] != '.' && $file != 'index.php') { | ||
Tools::deleteFile($dir.DIRECTORY_SEPARATOR.$file, array('index.php')); | ||
} | ||
foreach (array_diff(scandir($dir), array('..', '.', 'index.php')) as $file) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment here |
||
Tools::deleteFile($dir.DIRECTORY_SEPARATOR.$file); | ||
} | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not computing the array_diff outside of the foreach ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure it would be faster? I can't find any doc about this (not having assigned the array to any variables .. )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue with computing the
array_diff
within theforeach
is that you make the code computing the same value in every loop. By taking the array_diff computation out of the loop and into a variable, you gain some valuable microseconds, I'm sure -- moreover because you computation performs ascandir
, which touches upon the filesystem and therefore waaaaaay slower than any regular array creation :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My dubt was about to consider or not the (...) part of the loop to be
"inside" it
.. in other words I was not sure that this part of code is evaluated more
than one time ..
I'll do some tests to prove or negate this fact asap
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see this poc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oups you're right, I mixed up with for / while cases. Seems it's also bad to review code on friday :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always nice to have a reminder, thanks @ZiZuu-store :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
eheh I'm here for this @xBorderie -_^