Last week I started reading Extending and Embedding PHP. I really like extending scripting languages for a number of reasons, most of all because it’s a great way to learn about the more esoteric features of a language. So far I’ve discovered two neat things about PHP that made me smile, so I thought I’d share them here.
Copy on Write
In an effort to save CPU cycles, PHP uses an optimization strategy called “Copy on Write”. Basically what this means is that variable data isn’t copied until it needs to be. Using an example similar to the one found in the book:
$a = 50; $b = $a; $b += 5;
At the first line, the variable $a is created and memory is allocated for it. When we set $b to equal $a however, you might think that the data in $a is copied to $b. PHP doesn’t actually copy the data until the 3rd line when $b is incremented by 5. The value in this is that data is not copied unnecessarily. If, for some reason, we took out the 3rd line and $b was never modified after the initial assignment, the data would actually never have to be copied.
Return Value Used?
The second, admittedly more surprising thing I learned was that PHP provides internal functions a parameter called return_value_used whose value can be inspected to determine whether the return value being prepared by the function is actually being saved or not. Imagine you have a function that does some heavy crunching to compute a return value, and a caller (for some reason beyond us) does this:
<?php ... myextension_compute_something(); ... ?>
In other words, they call the function but don’t store the return value. Well, being a thoughtful internal function author, you can check to see that return_value_used evaluates to false and just skip doing any work, saving CPU cycles and time, like so:
PHP_FUNCTION(myextension_compute_something) { if (return_value_used) { // some fancy but expensive algorithm // ... RETURN_DOUBLE(some_value); } else { RETURN_NULL(); } }