Task #54091

Epic #55070: Workpackages

Epic #55065: WP: Overall System Performance (Backend and Frontend)

Story #55078: Optimize PHP code performance in TYPO3 methods

Use === operator instead of strlen() to check for zero length strings

Added by Markus Klein almost 6 years ago. Updated about 1 year ago.

Status:
Closed
Priority:
Should have
Assignee:
-
Category:
Performance
Target version:
-
Start date:
2013-11-29
Due date:
% Done:

100%

TYPO3 Version:
6.2
PHP Version:
Tags:
Complexity:
medium
Sprint Focus:

Description

Use the === operator instead of the strlen() function to check for strings of length zero.

This can give a performance improvement of factor 3.

Short note: Although some source claim that strlen() has O(1), for some reason the comparison against an empty string is still faster.

Nevertheless, besides the possible performance improvement, this change also improves readability.


Related issues

Related to TYPO3 Core - Bug #65239: Missing (string) cast or isset() check in HTMLparser Closed 2015-02-23
Related to TYPO3 Core - Bug #67034: Content Slide: slide.collect issues since 7.1.0 Closed 2015-05-19
Related to TYPO3 Core - Bug #84434: TypoScript ifBlank does not work with strlen like expected Rejected 2018-03-17
Duplicated by TYPO3 Core - Task #64199: Cleanup usage of strlen() Closed 2015-01-09
Precedes TYPO3 Core - Bug #67040: Backend broken on PostgreSQL - missing (string) cast in dbal/DatabaseConnection Closed 2015-05-19

Associated revisions

Revision de27a20a (diff)
Added by Markus Klein over 5 years ago

[CLEANUP] Replace strlen() with === for zero length check

It is faster to compare a string with === '' to find out if it's empty
than to run strlen() on it.

The replacement rules are applied as follows: * if (strlen($str))
=> if ((string)$str !== '') * if (!is_string($str) || strlen($str) === 0)
=> if (!is_string($str) || $str === '') * If it can be seen easily that $str is a string,
the typecast is omitted.

Resolves: #54091
Releases: 6.2
Change-Id: I59c5cbccea4f98b8f282377e6aa67d970859a457
Reviewed-on: https://review.typo3.org/27091
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind

Revision 4722b6cf (diff)
Added by Markus Klein over 4 years ago

[CLEANUP] Replace strlen() with === for zero length check

It is faster to compare a string with === '' to find out if it's empty
than to run strlen() on it.

The replacement rules are applied as follows: * if (strlen($str))
=> if ((string)$str !== '') * if (!is_string($str) || strlen($str) === 0)
=> if (!is_string($str) || $str === '') * If it can be seen easily that $str is a string,
the typecast is omitted.

Change-Id: I888d70e42f925bd57ad23b873b72d6a38acb39ef
Resolves: #54091
Releases: master
Reviewed-on: http://review.typo3.org/27794
Reviewed-by: Mathias Schreiber <>
Tested-by: Mathias Schreiber <>
Reviewed-by: Stefan Neufeind <>
Tested-by: Stefan Neufeind <>

Revision a27dfad8 (diff)
Added by Morton Jonuschat over 4 years ago

[BUGFIX] Add missing (string) cast in dbal/DatabaseConnection

This is a regression fix for #54091.
It has been forgotten to cast the lastQuery to a string before
checking if it's empty

Resolves: #67040
Related: #54091
Releases: master
Change-Id: I6991cad799e686066f8205749baee4aed20a9127
Reviewed-on: http://review.typo3.org/39625
Reviewed-by: Andreas Fernandez <>
Tested-by: Andreas Fernandez <>
Reviewed-by: Stephan GroƟberndt <>
Reviewed-by: Wouter Wolters <>
Reviewed-by: Markus Klein <>
Tested-by: Markus Klein <>

Revision bde48583 (diff)
Added by Benni Mack over 4 years ago

[BUGFIX] CONTENT cObject slide does not loop

The CONTENT cObject is looping over the SQL query
when sliding the rootline and/or collecting but never
repeats due to missing braces.

This happened during a cleanup merge in commit
4722b6cf5c97ab415d3d35687ed77750b97e3258.

The patch re-adds the braces.

Resolves: #67034
Related: #54091
Releases: master
Change-Id: Ifc912ea8d5d2861d6888ddc5ac7bd755e3ff6bb4
Reviewed-on: http://review.typo3.org/40244
Reviewed-by: Markus Klein <>
Tested-by: Markus Klein <>
Reviewed-by: Andreas Fernandez <>
Tested-by: Andreas Fernandez <>

Revision ac72c832 (diff)
Added by Stefan Froemken over 1 year ago

[BUGFIX] Test ifBlank with strlen again

Since TYPO3 7 ifBlank works with trim instead of strlen.
Like documented ifBlank has to work with strlen.
So 2 spaces should not be interpreted as a blank value.

Resolves: #84434
Related: #54091
Releases: master, 8.7
Change-Id: I6a130852225b23f30af40e8a9311d2288dfb31b6
Reviewed-on: https://review.typo3.org/56305
Reviewed-by: Christian Kuhn <>
Tested-by: Christian Kuhn <>
Reviewed-by: Mathias Brodala <>
Tested-by: Mathias Brodala <>
Tested-by: TYPO3com <>
Reviewed-by: Andreas Wolf <>
Tested-by: Andreas Wolf <>

Revision c12fe3c1 (diff)
Added by Stefan Froemken over 1 year ago

[BUGFIX] Test ifBlank with strlen again

Since TYPO3 7 ifBlank works with trim instead of strlen.
Like documented ifBlank has to work with strlen.
So 2 spaces should not be interpreted as a blank value.

Resolves: #84434
Related: #54091
Releases: master, 8.7
Change-Id: I6a130852225b23f30af40e8a9311d2288dfb31b6
Reviewed-on: https://review.typo3.org/56320
Tested-by: TYPO3com <>
Reviewed-by: Stefan Neufeind <>
Tested-by: Stefan Neufeind <>

History

#1 Updated by Stefan Neufeind almost 6 years ago

In general I like the idea.

Beware though that strlen() also returns 0 for FALSE, will cast non-strings to a string first (thus correctly gives string-length of integers etc.) and such.

#2 Updated by Jo Hasenau almost 6 years ago

Beware though that strlen() also returns 0 for FALSE, will cast non-strings to a string first (thus correctly gives string-length of integers etc.) and such.

So

(string)$foo === ''

should do the job and strlen() can stay for those cases that expect a length > 0

#3 Updated by Ernesto Baschny over 5 years ago

  • Target version set to 6.2.0
  • Parent task changed from #52949 to #55078

#4 Updated by Markus Klein over 5 years ago

I tested strlen() a bit.

$c = FALSE;
echo strlen($c); // 0
echo (string)$c === '' ? 'empty' : 'not empty'; // empty
echo $c === '' ? 'empty' : 'not empty'; // not empty
$c = TRUE;
echo strlen($c); // 1
echo (string)$c === '' ? 'empty' : 'not empty'; // not empty
echo $c === '' ? 'empty' : 'not empty'; // not empty

So it suffices to do the cast in cases were we are not sure about the type.

#5 Updated by Gerrit Code Review over 5 years ago

  • Status changed from New to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#6 Updated by Gerrit Code Review over 5 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#7 Updated by Gerrit Code Review over 5 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#8 Updated by Gerrit Code Review over 5 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#9 Updated by Gerrit Code Review over 5 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#10 Updated by Gerrit Code Review over 5 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#11 Updated by Stefan Neufeind over 5 years ago

  • Subject changed from Use === operator instead of strlen() to check for zero length stirngs to Use === operator instead of strlen() to check for zero length strings

#12 Updated by Gerrit Code Review over 5 years ago

Patch set 7 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#13 Updated by Gerrit Code Review over 5 years ago

Patch set 8 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#14 Updated by Gerrit Code Review over 5 years ago

Patch set 9 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27091

#15 Updated by Markus Klein over 5 years ago

  • Status changed from Under Review to Resolved
  • % Done changed from 0 to 100

#16 Updated by Gerrit Code Review over 5 years ago

  • Status changed from Resolved to Under Review

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27794

#17 Updated by Gerrit Code Review over 5 years ago

Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/28017

#18 Updated by Michiel Roos over 5 years ago

To make travis pass all the failing tests:

One issue is in \TYPO3\CMS\Core\Utility\GeneralUtility::xml2arrayProcess()
Around line: 2273:
if ((string)$val['attributes']['index'] !== '') {
$tagName = $val['attributes']['index'];
}

Two more string casts are needed in: typo3/sysext/extbase/Classes/Scheduler/FieldProvider.php around line 244

Two more string casts are needed in typo3/sysext/extensionmanager/Classes/Utility/EmConfUtility.php around line 95:
- if (strlen($emConf['PHP_version'])) {
+ if ((string)$emConf['PHP_version'] !== '') {
$emConf['constraints']['depends']['php'] = $emConf['PHP_version'];
}
- if (strlen($emConf['TYPO3_version'])) {
+ if ((string)$emConf['TYPO3_version'] !== '') {
$emConf['constraints']['depends']['typo3'] = $emConf['TYPO3_version'];
}

#19 Updated by Gerrit Code Review over 5 years ago

Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27794

#20 Updated by Gerrit Code Review over 5 years ago

Patch set 3 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/27794

#21 Updated by Markus Klein over 5 years ago

  • Target version deleted (6.2.0)

#22 Updated by Gerrit Code Review about 5 years ago

Patch set 4 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#23 Updated by Gerrit Code Review almost 5 years ago

Patch set 5 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#24 Updated by Gerrit Code Review almost 5 years ago

Patch set 6 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#25 Updated by Gerrit Code Review over 4 years ago

Patch set 7 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#26 Updated by Gerrit Code Review over 4 years ago

Patch set 8 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#27 Updated by Gerrit Code Review over 4 years ago

Patch set 9 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at http://review.typo3.org/27794

#28 Updated by Markus Klein over 4 years ago

  • Status changed from Under Review to Resolved

#29 Updated by Mathias Brodala over 1 year ago

  • Related to Bug #84434: TypoScript ifBlank does not work with strlen like expected added

#30 Updated by Benni Mack about 1 year ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF