- Issue created by @lucassc
- Assigned to PrabuEla
- 🇮🇳India PrabuEla chennai
The type cast changed but need bit deep testing for this.
- Issue was unassigned.
- Status changed to Needs review
almost 2 years ago 9:27am 30 January 2023 - Status changed to Needs work
almost 2 years ago 2:24pm 30 January 2023 - 🇺🇸United States smustgrave
Failures in #3
Also new functions will need to be typehinted.
- 🇬🇧United Kingdom catch
+++ b/core/lib/Drupal/Core/Form/FormState.php @@ -1110,6 +1110,17 @@ class FormState implements FormStateInterface { + /** + * Type casting to sting. + * + * @return string $val. + */ + public function toStringCasting($section = []) { + foreach ($section as $val) { + return (string) $val; + } + } + /**
In the other issue where there's a 1-1 replacement for intval() or strval() to (int) or (string) that seems like a good micro-optmization. But it seems very unlikely to me that replacing a PHP function with a helper method is going to be an improvement at all, it's also a lot more code / less readable.
- 🇮🇳India TanujJain-TJ
Fixed failed commands on #3
fixed phpcs errors
Attached interdiff patch - First commit to issue fork.
- Merge request !6022Resolves #3337257: Replace the use of intval() and strval() as callbacks with a foreach and typecast or such → (Open) created by dimitriskr
- Status changed to Needs review
12 months ago 3:49pm 4 January 2024 - 🇬🇷Greece dimitriskr
Took another approach from what the patches had, tests are green
- Status changed to Needs work
12 months ago 8:48pm 4 January 2024 - 🇺🇸United States smustgrave
If a different approach was taken can the issue summary be updated to reflect new approach.
- 🇷🇺Russia Chi
The patch contains lots of irrelevant changes. Also I think the code with intval and strval callbacks is much more readable.
- Status changed to Needs review
12 months ago 12:44pm 5 January 2024 - 🇬🇷Greece dimitriskr
I mean I took a different approach than the patches, I believe the code in MR reflects the proposed resolution in IS
- 🇺🇸United States smustgrave
The patch contains lots of irrelevant changes.
@chi could you flag those in the MR.
- 🇷🇺Russia Chi
@smustgrave, I think the parent issue is outdated. There was a big difference indeed in PHP 5 but in PHP 8 it works equally well.
Here is the script I used for testing.
const LIMIT = 10_000_000; // -- (string) $start = \microtime(true); for($i = 0; $i < \LIMIT; $i++) { $a = (string) $i; } $end = \microtime(true); echo ' (string): ', \number_format(1_000 * ($end - $start), 2), ' ms', \PHP_EOL; // -- strval $start = \microtime(true); for($i = 0; $i < \LIMIT; $i++) { $b = \strval($i); } $end = \microtime(true); echo ' strval: ', \number_format(1_000 * ($end - $start), 2), ' ms', \PHP_EOL; // -- callback $to_string = static fn ($value): string => (string) $value; $start = \microtime(true); for($i = 0; $i < \LIMIT; $i++) { $c = $to_string($i); } $end = \microtime(true); echo ' Callback: ', \number_format(1_000 * ($end - $start), 2), ' ms', \PHP_EOL;
Result (PHP 8.2):
(string): 190.62 ms strval: 192.26 ms Callback: 340.39 ms
The approach with custom caster works a way slower than
strval
. But even if it would be faster, notice the absolute values. 179 milliseconds for 10 million iterations. It's not a micro-optimization nor even a nano-optimization. It's a pico-optimization. ) - 🇬🇧United Kingdom longwave UK
Unless this was in a super tight critical loop, to me we should prefer readability over micro-optimisations, and this should be closed won't fix as calling a custom method in order to cast instead of using
array_map('intval', ...)
is much harder to understand. - 🇬🇷Greece dimitriskr
The script in #16 is good for the parent issue. Here it's about arrays and typecasting their values so it might have some differencein performance?
Also I ran the script too and got different results
(string): 697.98 ms strval: 715.63 ms Callback: 2,925.63 ms
- 🇺🇸United States smustgrave
So should this be postponed till the parent issue can be updated to determine if this issue should move forward?
- Status changed to Postponed: needs info
11 months ago 6:18pm 23 January 2024