Skip to content
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 1 commit into from Dec 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 5 additions & 10 deletions classes/Media.php
Expand Up @@ -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) {
Copy link
Contributor

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 ?

Copy link
Author

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 .. )

Copy link
Contributor

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 the foreach 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 a scandir, which touches upon the filesystem and therefore waaaaaay slower than any regular array creation :)

Copy link
Author

@ghost ghost May 6, 2016

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

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see this poc

Copy link
Contributor

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

Copy link
Contributor

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 :)

Copy link
Author

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 -_^

$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);
}
Expand Down Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment here

Tools::deleteFile($dir.DIRECTORY_SEPARATOR.$file);
}
}
}
Expand Down