#107818Harden `MathUtility::canBeInterpretedAsInteger()` for `PHP8.5`
Description
Working on the support for upcoming PHP 8.5.0 unit and functional tests
has been evaluated to detect issues for new native deprecations, errors
and warnings and a bunch of them has been already tackled.
`MathUtility::canBeInterpretedAsInteger()` was the last candidate with
two issues for PHP 8.5:
The float 9.223372036854776E+18 is not representable as an int,
cast occurred
and
The float NAN is not representable as an int, cast occurred
The second one has been revealed after added test for float NAN value
with #107800 and executing it with PHP8.5 and requires now additional
care to tackle the NAN issue.
Some developer raised performance concerns regarding #107800 and the
used `preg_match()` approach and performance benchmark attempts has
been done afterwards with a phpbenchmark approach and a simple plain
PHP script using `microtime()`, both revealing a slightly performance
decrease in the range of a fraction of a millisecond (0.0x ms) with
10000000 (ten billion) method calls and is considerable okay gaining
warnings resolved and ending in a more robust implementation.
For the now additional popped up warning more effort has been taken
place to find a even better solution with and without feared method
`preg_match()` and executed against both benchmarking approached
and unit/functional tests of TYPO3.
Statistic overview (phpbenchmark) [1]: ====================================== +-----------------------------------------------------------+-----+-----------+--------+ | subject | set | mode | rstdev | +-----------------------------------------------------------+-----+-----------+--------+ | benchOriginal | 0 | 759.290ms | ±1.97% | | benchOriginal | 1 | 12.662s | ±1.09% | | benchPregMatchGerritChange91188 | 0 | 828.307ms | ±0.78% | | benchPregMatchGerritChange91188 | 1 | 13.333s | ±1.07% | | benchPregMatchChange91188AndNanProtection | 0 | 820.434ms | ±0.51% | | benchPregMatchChange91188AndNanProtection | 1 | 13.483s | ±0.51% | | benchVariantTacklingAllWarningsAndTryingToKeepPerformance | 0 | 742.543ms | ±0.57% | | benchVariantTacklingAllWarningsAndTryingToKeepPerformance | 1 | 12.580s | ±0.67% | +-----------------------------------------------------------+-----+-----------+--------+
Each method variant has been executed `10000000` times per set providing
range from `0` to `10000000 – 1` as value having `set 1` using the value
directly and `set 1` as string with a appended `a` character.
Each method has been executed five times against each set and averaged.
To be honest, 10 billion method calls are quite unrealistic within one
request (frontend/backend) and and the difference for the non-integer
string value has a difference of ~700ms being already in the ~12 seconds
range with the original implementation compared to the `preg_match()`
approach.
However, trying to avoid extensive discussions in multiple places the
effort to test, verify and measure (benchmark) different approaches
has been done directly and documented along right away.
Simply adding a `nan` protection condition to the `preg_match()` code
increased the performance again still having a diff of `~700ms`, and
after testing out a lot of variants not passing the tests and having
even worse stats.
Ending up refactoring the whole method implementation using `gettype()`
and `match()` to execute optimized checks based on the type removing
the `preg_match()` call again still satisfied all unit and functional
tests restoring the performance to the original implementation with
a slightly but ignorable improvement.
Note here, this depends on additional factors like opcache used
or not, the used pcre version and so on along with used the PHP
version (8.2/8.3/8.4/8.5) and like always with +/- ranges usual
for benchmarking results.
[1] https://github.com/sbuerk/mathutility-preg-match-bench
2 https://github.com/sbuerk/mathutility-preg-match-bench/plain-benchmark.php