- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.7k
Performance optimizations #1 #12171
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 optimizations #1 #12171
Changes from 24 commits
033aa88
              a650515
              a33ad89
              4173a7f
              186f975
              19eb362
              f9cb3fe
              d30d1d7
              0d1f128
              321c813
              d30fe28
              5dfd4e9
              4c1cfdd
              271d02a
              c500e83
              44d234d
              6fb499b
              5ada9b9
              c2fd895
              de1a93e
              2aa353b
              307cd94
              062ef22
              1bc9d8f
              7d287f0
              53d4854
              5f6d634
              2e2df19
              6b3e0fb
              171f66b
              a730ca0
              1ff621a
              ba6ac8a
              c67b374
              7e02da0
              a924e5c
              e0b11c7
              22fc1c4
              4b30c17
              e3f8617
              7d7b3cd
              42a6146
              cb5ee93
              bb10022
              c4157d9
              ec2c21a
              c0d3b44
              3619945
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -160,7 +160,7 @@ public function canUpload($file, $component = 'com_media') | |
| $finfo = finfo_open(FILEINFO_MIME); | ||
| $type = finfo_file($finfo, $file['tmp_name']); | ||
|  | ||
| if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) | ||
| if ($type !== '' && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) | ||
| { | ||
| $app->enqueueMessage(JText::_('JLIB_MEDIA_ERROR_WARNINVALID_MIME'), 'notice'); | ||
|  | ||
|  | @@ -174,7 +174,7 @@ public function canUpload($file, $component = 'com_media') | |
| // We have mime magic. | ||
| $type = mime_content_type($file['tmp_name']); | ||
|  | ||
| if (strlen($type) && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) | ||
| if ($type !== '' && !in_array($type, $allowed_mime) && in_array($type, $illegal_mime)) | ||
| { | ||
| $app->enqueueMessage(JText::_('JLIB_MEDIA_ERROR_WARNINVALID_MIME'), 'notice'); | ||
|  | ||
|  | @@ -206,7 +206,7 @@ public function canUpload($file, $component = 'com_media') | |
| foreach ($html_tags as $tag) | ||
| { | ||
| // A tag is '<tagname ', so we need to add < and a space or '<tagname>' | ||
| if (stristr($xss_check, '<' . $tag . ' ') || stristr($xss_check, '<' . $tag . '>')) | ||
| if (false !== stripos($xss_check, '<' . $tag . ' ') || false !== stripos($xss_check, '<' . $tag . '>')) | ||
|          | ||
| { | ||
| $app->enqueueMessage(JText::_('JLIB_MEDIA_ERROR_WARNIEXSS'), 'notice'); | ||
|  | ||
|  | @@ -271,13 +271,13 @@ public function countFiles($dir) | |
|  | ||
| while (false !== ($entry = $d->read())) | ||
| { | ||
| if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) | ||
| if ($entry[0] !== '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) | ||
| && strpos($entry, '.html') === false && strpos($entry, '.php') === false) | ||
|          | ||
| { | ||
| $total_file++; | ||
| } | ||
|  | ||
| if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) | ||
| if ($entry[0] !== '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) | ||
| { | ||
| $total_dir++; | ||
| } | ||
|  | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -134,7 +134,7 @@ protected function findItem($needles = array()) | |
| $language = isset($needles['language']) ? $needles['language'] : '*'; | ||
|  | ||
| // $this->extension may not be set if coming from a static method, check it | ||
| if (is_null($this->extension)) | ||
| if ($this->extension === null) | ||
| { | ||
| $this->extension = $app->input->getCmd('option'); | ||
| } | ||
|  | @@ -180,7 +180,7 @@ protected function findItem($needles = array()) | |
| * $language != * can override existing entries | ||
| * $language == * cannot override existing entries | ||
| */ | ||
| if (!isset(static::$lookup[$language][$view][$item->query['id']]) || $item->language != '*') | ||
| if ($item->language != '*' || !isset(static::$lookup[$language][$view][$item->query['id']])) | ||
|          | ||
| { | ||
| static::$lookup[$language][$view][$item->query['id']] = $item->id; | ||
| } | ||
|  | ||
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.
I can't find the reference at the moment, but I remember reading something along the lines that storing a function return in a variable and returning that variable in your method is less error prone and faster than directly returning an evaluated function in your method.
Technically they both work, and off the top of my head I don't think we have a standard on which way to write code.
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.
In my opinion, such kind of variables should not be introduced.
They serve no practical purpose. And they do actually introduce another unnecessary step in our minds, when we humans read the code - and also when the machine aka the interpreter/compiler reads and processes it.
Why introduce something whose only purpose is to be returned on the next line, without any further processing?
As for the compiler, I would think modern PHP compilers do this step automatically internally, but they have to do it nevertheless, using cpu and memory, so why not leave it out in the first place?
Just my 2 cents 😄
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.
In terms of code readability I've been leaning toward less creation/assignment of variables that aren't needed than "explicit" structures like this one. There isn't a standard since both are valid syntax, but unless there is a majorly strong argument (like engine performance) for one over the other, I'd lean toward the readability argument and I find it easier to read one line instead of two code lines and the blank line in the middle.
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.
BTW, there are some interesting numbers here on inlining and redundant variables. These numbers depict different implementations in loops, but you can see the difference, which is even bigger in 7.0 (than even the latest 5.6), when using redundant variables.
Of course, it's not like Joomla is looping several thousands of times through these functions to create some content, but why not save a few bytes on memory and also a few cpu cycles? We can only benefit from it. ... + it's easier to read...
Uh oh!
There was an error while loading. Please reload this page.
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.
@photodude I think you were possibly referring to this q&a...
Uh oh!
There was an error while loading. Please reload this page.
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.
That might have been it, but maybe not. I just checked some of my book references to see if it was in one of those books, and it seems the general consensus in the examples leans towards the inlining the return. (but nothing specific was stated in those books)
The argument of code readability is a good one.
It might be one of those specific circumstances things. Like ternary operators are ok in php, unless there is large objects in the variables being passed, then ternary operators in php suck up time and memory due to the copy operation they preform on the variables before evaluating.
In anycase I can't seem to find the reference at the moment, so Code Readability wins me over.
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.
@frankmayer I think part of what I remember was something like this comment on the PHP manual
http://php.net/manual/en/function.pg-connect.php#82117
considering that it's 8 years old I'm not sure if it still applies in more modern php
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.
@photodude I see what you mean. The case of the comment is partially true (at least with PHP7 which I just tested). In PHP7, it will die() even inline. However, if the returned value is not false, it will return a bool, which automatically will be
true.So this is actually a case where caution is advised and those cases should be dealt with, the old way.