Project

General

Profile

Actions

Task #54091

closed

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 10 years ago. Updated over 5 years ago.

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

100%

Estimated time:
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 5 (0 open5 closed)

Related to TYPO3 Core - Bug #65239: Missing (string) cast or isset() check in HTMLparserClosedMarkus Klein2015-02-23

Actions
Related to TYPO3 Core - Bug #67034: Content Slide: slide.collect issues since 7.1.0ClosedBenni Mack2015-05-19

Actions
Related to TYPO3 Core - Bug #84434: TypoScript ifBlank does not work with strlen like expectedRejectedStefan Froemken2018-03-17

Actions
Has duplicate TYPO3 Core - Task #64199: Cleanup usage of strlen()Closed2015-01-09

Actions
Precedes TYPO3 Core - Bug #67040: Backend broken on PostgreSQL - missing (string) cast in dbal/DatabaseConnectionClosed2015-05-19

Actions
Actions #1

Updated by Stefan Neufeind over 10 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.

Actions #2

Updated by Jo Hasenau over 10 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

Actions #3

Updated by Ernesto Baschny about 10 years ago

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

Updated by Markus Klein about 10 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.

Actions #5

Updated by Gerrit Code Review about 10 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

Actions #6

Updated by Gerrit Code Review about 10 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

Actions #7

Updated by Gerrit Code Review about 10 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

Actions #8

Updated by Gerrit Code Review about 10 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

Actions #9

Updated by Gerrit Code Review about 10 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

Actions #10

Updated by Gerrit Code Review about 10 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

Actions #11

Updated by Stefan Neufeind about 10 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
Actions #12

Updated by Gerrit Code Review about 10 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

Actions #13

Updated by Gerrit Code Review about 10 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

Actions #14

Updated by Gerrit Code Review about 10 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

Actions #15

Updated by Markus Klein about 10 years ago

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

Updated by Gerrit Code Review about 10 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

Actions #17

Updated by Gerrit Code Review about 10 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

Actions #18

Updated by Michiel Roos about 10 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'];
}

Actions #19

Updated by Gerrit Code Review about 10 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

Actions #20

Updated by Gerrit Code Review about 10 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

Actions #21

Updated by Markus Klein almost 10 years ago

  • Target version deleted (6.2.0)
Actions #22

Updated by Gerrit Code Review over 9 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

Actions #23

Updated by Gerrit Code Review about 9 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

Actions #24

Updated by Gerrit Code Review about 9 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

Actions #25

Updated by Gerrit Code Review about 9 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

Actions #26

Updated by Gerrit Code Review about 9 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

Actions #27

Updated by Gerrit Code Review about 9 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

Actions #28

Updated by Markus Klein about 9 years ago

  • Status changed from Under Review to Resolved
Actions #29

Updated by Mathias Brodala about 6 years ago

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

Updated by Benni Mack over 5 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF