BetterReflection icon indicating copy to clipboard operation
BetterReflection copied to clipboard

Improve `ReflectionEnum->getCases()` performance

Open staabm opened this issue 1 year ago • 11 comments

the method is showing up in profiles of https://github.com/phpstan/phpstan/issues/10772 I am aware that this PR will not fix the root cause of the perf problem, but its a low hanging fruit on my way :)

grafik

lets remove the array_map magic and the unnecessary closure invocation

staabm avatar Mar 22 '24 14:03 staabm

@staabm yes, the best optimization would be to remove the adapter layer from PHPStan :)

kukulich avatar Mar 22 '24 14:03 kukulich

I'm OK with merging this, but it will eventually be re-factored again to an array_map() later on anyway :|

I'm convinced method tracing is actually inflating this more than it actually is relevant.

I'd rather memoize cases instead, in future.

Ocramius avatar Mar 22 '24 14:03 Ocramius

lets remove the array_map() magic and the unnecessary closure invocation

FWIW, array_map() is less magic than loops in general: it's just that the engine suuuuucks at handling it :-P

Ocramius avatar Mar 22 '24 14:03 Ocramius

FWIW, array_map() is less magic than loops in general

I feel array_map makes me read code backwards. I really like the simplicity of foreach. maybe I suck in reading it as much as the engine ;-)

staabm avatar Mar 22 '24 14:03 staabm

You need to spend a couple months writing FP (Rust, Haskell, clojure, etc.), and then C-style loops start looking like the garbled mess that they are ;-)

Ocramius avatar Mar 22 '24 14:03 Ocramius

I'd rather memoize cases instead, in future.

done

staabm avatar Mar 22 '24 15:03 staabm

please advice on how to make psalm happy or how/where to move this thing ;)

staabm avatar Mar 22 '24 15:03 staabm

I can try helping later tonight, but currently stuck with another issue at work.

Ocramius avatar Mar 22 '24 15:03 Ocramius

My opinion - this kind of micro optimization isn't worth it. It's only showing 8 % improvement in PHPStan because it's working with huge amounts of data. Once the root cause in PHPStan is fixed then the improvement isn't going to be 8 %, but barely measurable.

The root cause was already fixed: https://github.com/phpstan/phpstan-src/pull/2985

ondrejmirtes avatar Mar 22 '24 16:03 ondrejmirtes

Most likely, but spiking memoization as a general outcome is still worth pursuing :D

Ocramius avatar Mar 22 '24 16:03 Ocramius

I am fine with closing it

staabm avatar Mar 22 '24 16:03 staabm