2020-12-30 21:27 GMT, Olle Härstedt <olleharst...@gmail.com>:
> 2020-12-30 20:37 GMT, Larry Garfield <la...@garfieldtech.com>:
>> On Wed, Dec 30, 2020, at 2:16 PM, Olle Härstedt wrote:
>>
>>> > Ok. You have a benchmark for this? I can make one otherwise, for the
>>> > query
>>> > example.
>>> >
>>> > It worries me a little that immutability is pushed into the ecosystem
>>> > as a
>>> > silver bullet. Main reason functional languages are using it is
>>> > because
>>> > ownership is a newer concept, so it hasn't been adapted as much.
>>>
>>> Tiny benchmark here:
>>> https://gist.github.com/olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a
>>>
>>> Running this on my ARM machine with PHP 7.2 gives ~1.2s for the
>>> immutable loop, ~0.35s for the mutable one, meaning immutability is
>>> ~3x as expensive performance wise. And this is for a SMALL object - I
>>> suspect the performance hit will grow the bigger the class gets (more
>>> properties to clone). Correct me if I'm wrong. :/
>>
>>
>> That's not a valid benchmark; it's comparing cloning and method
>> invocation
>> against just property sets.  The method calls are chewing up most of the
>> time there.
>>
>> Here's a more fair comparison on my laptop:
>> https://gist.github.com/Crell/848568124e25c8c83fc4da5455063bab
>>
>> Which is only ~20% slower.  And that's when dealing with very small
>> numbers,
>> so in most cases you're unlikely to notice a difference unless you really
>> are iterating over something a million times.
>>
>> I also tossed some big string properties into the class, and while the
>> total
>> time went up a bit the ratio between the two stayed about the same.
>>
>> --Larry Garfield
>
> Oh yeah. Huh. Didn't realize method calling was expensive. :) Another
> good reason to not use setters, lol. On my machine it's ~70% slower
> with "with" now (running multiple runs, using bash "time").
>
> The performance might matter when frameworks start to adapt patterns
> like this. It's already in PSR, with the HTTP message interface etc.
> Now I'm curious how OCaml and JS performs with a similar benchmark.
> Since they both have tracing GC, and PHP uses ref counting...

Benchmarks for JS and OCaml:
https://gist.github.com/olleharstedt/eaaf1dd40be541f84aa0f3954a0ea54a

In JS, it's 5x more expensive to use spread operator vs mutate the
object fields directly (or maybe node is just not optimized well on
the ARM CPU?). In OCaml, it's a ~10% difference between destructive
update (mutable fields) and cloning. No idea what to take from that.
:)

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to