Project

General

Profile

Actions

Task #54265

closed

Epic #55070: Workpackages

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

Story #55078: Optimize PHP code performance in TYPO3 methods

Use (int) instead of intval() or (integer)

Added by Jo Hasenau over 10 years ago. Updated over 6 years ago.

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

100%

Estimated time:
TYPO3 Version:
6.2
PHP Version:
5.4
Tags:
Complexity:
Sprint Focus:

Description

Description

When testing for the identity of integers using the intval() function has worse performance than a type cast with (int). (3-5x)

Replace all ~1600 calls to intval() with a type cast.


Related issues 4 (0 open4 closed)

Related to TYPO3 Core - Task #54085: Replace all strcmp() calls with ===Closed2013-11-29

Actions
Related to TYPO3 Core - Bug #55753: pagetype is not respected in pagecacheClosed2014-02-07

Actions
Related to TYPO3 Core - Bug #61699: Workspace Preview not working with lockSSL=3ClosedOliver Hader2014-09-18

Actions
Precedes TYPO3 Core - Bug #55706: Different behavior of levelmedia in TYPO3 6.2Closed2014-02-05

Actions
Actions #1

Updated by Jo Hasenau over 10 years ago

Actually the impact is around factor 2.5 not 3 to 5 but still it should be worth switching from intval() to (int).

There is one execpetion though, which is the "base" parameter of the intval() function.
Don't know yet, if this is actually used within the TYPO3 core, but if it is, then intval() should not be replaced.

Actions #2

Updated by Markus Klein over 10 years ago

Hi! Thanks for fiddling around with this.

Before we invest that much time here (the replacement with a regexp is fast, but the review is very cumbersome), we'd discuss this on the core list first.
I've a gut feeling that not too many devs will support this and therefor it might be hard to get it merged finally.

Cheers Markus

Actions #3

Updated by Markus Klein over 10 years ago

  • Parent task set to #52949
Actions #4

Updated by Steffen Ritter over 10 years ago

We discuessed this shortly on the release team meeting as we are concepting the performance improvements.

We are in - but need to make sure that it's really the same :)

Actions #5

Updated by Mathias Schreiber over 10 years ago

Hi joey,

in general: great idea.
I suggest changing the topic to "replace intval() with (int) where it makes sense" in order to make it sound less ranty

Actions #6

Updated by Steffen Ritter over 10 years ago

  • Subject changed from Replace all intval() with (int) to Replace all intval() with (int) wherever it makes sense
Actions #7

Updated by Jo Hasenau over 10 years ago

So I will go for it but do it the "semiautomatic" way.

It might take a bit more patchsets than #54085 but IMHO it was working quite well to have a limited number of changes per patchset. So reviewing will be easier and discussing can happen on the way through the patchsets. After all there might be less context related things to discuss than with strcmp().

BTW: According to some PHP benchmarking pages I have some more candidates like array_unique or array_key_exists on my list. So if you guys already found some others, feel free to assign the tasks to me.

Actions #8

Updated by Stefan Froemken over 10 years ago

Please correct me, but I may read that we want to use (integer) instead of (int). Same with (boolean) instead of (bool). I think we should use the same style everywhere.

Actions #9

Updated by Jo Hasenau over 10 years ago

Agreed to use the same style, but in that case it should be (int) and (bool) for everything, since these are less "bloaty" while still easily understandable.

Actions #10

Updated by Stefan Neufeind over 10 years ago

Please see current thread on the core-mailinglist about CGL-decisions like these ("integer or int"). There is a notes-document to sum up decisions, their pros/cons etc.

Actions #11

Updated by Jo Hasenau over 10 years ago

So could someone take care of that document and confirm one or the other way.
https://notes.typo3.org/p/CGL-decisions

It's not necessary to decide everything but at least the (bool) (int) vs. (boolean) (integer) thingie should be decided before I go on with this task.

Actions #12

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

Actions #13

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

Actions #14

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

Actions #15

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

Actions #16

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

Actions #17

Updated by Chris topher over 10 years ago

  • Subject changed from Replace all intval() with (int) wherever it makes sense to Use (integer) instead of intval() or (int)
Actions #18

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

Actions #19

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

Actions #20

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

Actions #21

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

Actions #22

Updated by Gerrit Code Review over 10 years ago

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

Actions #23

Updated by Gerrit Code Review over 10 years ago

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

Actions #24

Updated by Gerrit Code Review over 10 years ago

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

Actions #25

Updated by Gerrit Code Review over 10 years ago

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

Actions #26

Updated by Gerrit Code Review over 10 years ago

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

Actions #27

Updated by Gerrit Code Review over 10 years ago

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

Actions #28

Updated by Ernesto Baschny about 10 years ago

  • Parent task changed from #52949 to #55078
Actions #29

Updated by Ingo Schmitt about 10 years ago

Core decision as from today: it should be written (int)$variable

Actions #30

Updated by Gerrit Code Review about 10 years ago

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

Actions #31

Updated by Gerrit Code Review about 10 years ago

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

Actions #32

Updated by Gerrit Code Review about 10 years ago

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

Actions #33

Updated by Bernhard Kraft about 10 years ago

What are the (php internal?) differences between intval() and a (int) typecast?

Is it only that intval() takes a base argument (defaulting to 10) and the difference in speed or are there other specialities?

Could someone point me to a document on that case?

Actions #34

Updated by Jo Hasenau about 10 years ago

It's exactly that: A difference in speed and memory consumption.

The rule of thumb is:

  1. The right operator or typecast is better than
  2. the right native function is better than
  3. your own optimized method for the same purpose

So as long as there is no need for a different base, you should do a typecast to (int) instead of intval().

This might be irrelevant with future PHP versions, since the PHP guys might improve performance as well, but still we can be sure that the function call will never outperform the typecast.

http://www.php.net/manual/de/language.types.type-juggling.php
http://www.php.net/manual/de/language.types.integer.php#language.types.integer.casting

<quote>
To explicitly convert a value to integer, use either the (int) or (integer) casts. However, in most cases the cast is not needed, since a value will be automatically converted if an operator, function or control structure requires an integer argument. A value can also be converted to integer with the intval() function.
</quote>

Actions #35

Updated by Gerrit Code Review about 10 years ago

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

Actions #36

Updated by Gerrit Code Review about 10 years ago

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

Actions #37

Updated by Gerrit Code Review about 10 years ago

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

Actions #38

Updated by Gerrit Code Review about 10 years ago

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

Actions #39

Updated by Jo Hasenau about 10 years ago

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

Updated by Simon Schaufelberger about 10 years ago

can somebody please share the script how to do that automatically or was everything done manually? I want to use that for my own extensions as well!

Actions #41

Updated by Jo Hasenau about 10 years ago

This was done manually, since I wanted to take care of surrounding issues as well, which would have been impossible with an automated search and replace.
The goal was to get rid of superfluous method calls and typecasts additionally. So the only helper was PHPstorm to find the occurrences and provide a local history of the changes.

Actions #42

Updated by Chris topher about 10 years ago

  • Subject changed from Use (integer) instead of intval() or (int) to Use (int) instead of intval() or (integer)
Actions #43

Updated by Riccardo De Contardi over 6 years ago

  • Status changed from Resolved to Closed
Actions

Also available in: Atom PDF