4 ms·
With copy-on-write: $a = ['a']; $b = $a; $a and $b both point to the exact same bit of memory. Thus passing an array between functions is zero-cost.
by withinboredom 5y ago
With copy-on-write:
$a = ['a'];
$b = $a;
$a and $b both point to the exact same bit of memory. Thus passing an array between functions is zero-cost.
When you do:
$b[] = 'b'; // append 'b'
a new array is allocated, the old data copied to it, and 'b' appended to it (basically, but IIRC, it is a bit more optimized than that).
- deleted 5y ago[deleted]
- kaba0 5y agoSo basically what FP languages do?
- withinboredom 5y agoSurprising php has a lot of FP aspects.
- pas 5y agoIt .. really doesn't. (Sure, probably a tiny bit more than Python.) It's not static types FP like Haskell/Scala (or even TS), as it just recently started gaining some type safety, and while it had map/reduce call_user_func, it wasn't that big in PHP world. It simply has/had too much mutability for it, and most PHP practice focused on trying to implement OOP ideas in it in a nice way.
- withinboredom 5y agoFWIW, I don't consider type safety a major issue with FP vs. OOP vs. procedural. I think type safety is just that, a safety net. I've ported things between C#, Scala, and PHP for fun, and regularly find PHP to be more concise due to it's lack of type safety than it being a hinderance. Sure, it sucks that the IDE can't help me out more (lack of generics cough) but static analysis tools really help there, like Psalm and PHPStan. As a side note, Psalm's security scans have pointed out security issues with the original code it was ported from, so that's something... You can do some pretty fancy things with arrays that you usually only see in FP type languages: $arr1 + $arr2 (non-numeric keys) // scala: arr1 ++ arr2 array_merge($arr1, $arr2) // scala: (arr1 ++ arr2) distinct array_unique(array_merge($arr1, $arr2)) // scala: arr1 union arr2 array_intersect($arr1, $arr2) // scala: arr1 intersect arr2 array_combine($arr1, $arr2) // scala: arr1 zip arr2 array_filter(array_map($mapper(...), $arr)) // scala: arr.flatmap(mapper) in all of these cases, the original array(s) remains untouched. In most OOP languages, the array would probably be mutable (or require some juggling between mutable and immutable types, like C#). That's why its surprising. It's quite verbose in PHP, but the foundations for a FP approach is all there. What's even more surprising is when performance is comparable to a language like C# on some random benchmarks I've run[1] (opcache + JIT turned on in PHP, C# in "release" optimized compilation). I'd consider it a pretty good contender for serious things. [1]: https://github.com/withinboredom/distributed-hashmap/blob/23baf54213ab3dd473724999b88585bba815f74e/README.md#performance https://github.com/withinboredom/distributed-hashmap/blob/23...
- eyelidlessness 5y agoDepends on the language. Many FP languages use persistent data structures, sharing some memory with derived/updated values.
- TazeTSchnitzel 5y agoWell, what PHP does similarly avoids the risk of accidentally mutating shared values, but it's not the same: • the value is locally mutable • no duplication happens when you want to modify it and there is only a single reference