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 over 1 year ago. Updated 7 months ago.

Status:Resolved Start date:2013-11-29
Priority:Should have Due date:
Assigned To:- % Done:

100%

Category:Performance Spent time: -
Target version:-
TYPO3 Version:6.2 Complexity:medium
PHP Version: 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 Core - Bug #67034: Content Slide: slide.collect issues since 7.1.0 Resolved 2015-05-19
related to Core - Bug #65239: Missing (string) cast or isset() check in HTMLparser Resolved 2015-02-23
duplicated by Core - Task #64199: Cleanup usage of strlen() Resolved 2015-01-09
precedes Core - Bug #67040: Backend broken on PostgreSQL - missing (string) cast in d... Resolved 2015-05-19

Associated revisions

Revision de27a20a
Added by Markus Klein over 1 year 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
Added by Markus Klein 7 months 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
Added by Morton Jonuschat 3 months 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
Added by Benjamin Mack about 1 month 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 <>

History

#1 Updated by Stefan Neufeind over 1 year 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 over 1 year 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 1 year ago

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

#4 Updated by Markus Klein over 1 year ago

I tested strlen() a bit.

1$c = FALSE;
2echo strlen($c); // 0
3echo (string)$c === '' ? 'empty' : 'not empty'; // empty
4echo $c === '' ? 'empty' : 'not empty'; // not empty
1$c = TRUE;
2echo strlen($c); // 1
3echo (string)$c === '' ? 'empty' : 'not empty'; // not empty
4echo $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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 1 year ago

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

#16 Updated by Gerrit Code Review over 1 year 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 1 year 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 1 year 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 1 year 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 1 year 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 about 1 year ago

  • Target version deleted (6.2.0)

#22 Updated by Gerrit Code Review 11 months 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 7 months 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 7 months 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 7 months 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 7 months 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 7 months 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 7 months ago

  • Status changed from Under Review to Resolved

Also available in: Atom PDF