tekrat

Archive for the ‘tech’ Category

Open Web Vancouver 2008

  • Filed under: tech
Monday
Feb 18,2008

It looks like my talk has been accepted at the Open Web Vancouver 2008 conference. I’m really looking forward to this as this will be my first conference that isn’t strictly PHP. There will be a wider range of topics including Ruby on Rails, Python, Javascript, and even some mobile technology.

I’m really impressed with the site organization so far, I’ve got an account that looks like it lets me update my talk summary and tags. The conference is also really reasonably priced at only $150 (+$20 for a t-shirt)! My talk will be the “APC @ Facebook” talk that I’ve given in the past, but of course I’m hoping to update it with some recent changes and hopefully some new features that I’ve yet to complete.

My name has also apparently made it under the title “Some of the big names you can expect to hear”, although my name is the only one where the hyperlink is on “Facebook” rather than my name. I guess I’m riding in on the coat tails so to speak. ;-)

Update:  Turns out somebody does read my blog and the “coat tailing” is no more, thanks!

Tags:

Tricky Tricky Refcounts….

  • Filed under: tech
Thursday
Nov 29,2007

Occasionally a PHP engineer reports this prolem:

Example Code:
————-
$my_arr = array(1,2,3);
foreach ($my_arr as &$val) {
var_dump($val);
}
foreach ($my_arr as $val) {
var_dump($val);
}


Expected Output:
———
int(1)
int(2)
int(3)
int(1)
int(2)
int(3)

Actual Output:
——-
int(1)
int(2)
int(3)
int(1)
int(2)
int(2)

The confusion comes from the expectation that the second loop will print the last element of the array as int(3) rather than int(2).  The initial reaction is usally “this is a PHP bug”, but it really isn’t.  There are two key aspects to this code to watch out for; 1) The scope of foreach variables is not limited to the foreach block. 2) Foreach loops do not unset foreach variables at the start of the block.

With this in mind we can see that at the end of the first loop, $val is a reference to the last element of $my_arr.  Each iteration over the foreach loop can be thought of as an assignment operation, in this case by reference:

$val = &$my_arr[0]
$val = &$my_arr[1]
$val = &$my_arr[2]
// last iteration $val is a reference to $my_arr[2]


As we step through each iteration of the second foreach loop we see the assignments of $val to each element of the $my_arr (assigned by value this time).

$val = $my_arr[0]
$val = $my_arr[1]
$val = $my_arr[2]

But if we you recall $val is really a reference to the last element of $my_arr because it carries over from the first foreach loop, so the actual assignment looks more like:

$my_arr[2] = $my_arr[0]
$my_arr[2] = $my_arr[1]
$my_arr[2] = $my_arr[2]

Thus we end up with $my_array being set as such on each iteration:

// (array(1,2,1))  first element is set to value of last
$my_arr[2] = $my_arr[0]

// (array(1,2,2))  second element is set to value of last
$my_arr[2] = $my_arr[1]

// (array(1,2,2))  last element is set to value of itself

$my_arr[2] = $my_arr[2]

Note that the last assignment is really assigning the last element to itself!

Because PHP5 handles variables with a copy on write algorithm, it’s typically not necessary to do any assignmnents by reference with performance gains in mind (as was the case with a lot of PHP4 code).  The above code can be made to function as the expected case by placing an unset($var) between the foreach loops, or not iterating over references and instead assigning the values of $my_arr explicitly by index or key values.  References should be used by care  and only when necessary.  When code like this is present in global scope or large functions it may affect future code in seemingly unpredictable ways.

Blogged with the Flock Browser

Tags: , , ,

Forum PHP Paris 2007

  • Filed under: tech
Monday
Nov 26,2007

I had an excellent time at the Forum PHP conference, many thanks to the conference organizers and attendees at the Forum PHP Paris conference. This year I had the honor of getting to speak to this group of people and share how Facebook uses APC to improve site stability and performance over the last couple years, you can check out the slides here.

Experimental APC Binary Dump Support

  • Filed under: tech
Monday
Nov 12,2007

I’ve put up the Experimental APC bindump patch here.  This allows users to dump an architecture specific version of the APC cache and load it back up later on the same or identical machines.  I’d love to get feedback from people regarding bugs and use cases!

DC PHP 2007

  • Filed under: tech
Wednesday
Nov 7,2007

Just finished my talk on APC at the DC PHP 2007 conference.  Added a couple new slides and some patches I’ve released for APC, PHP and Apache.  You can download the slides here.  Thanks to all who attended!

PHP MySQL “typed” functionality

  • Filed under: tech
Tuesday
Oct 9,2007

If you’ve ever wanted to have the PHP MySQL functions return back PHP variables cast to something other than strings this patch is for you.  It’s useful for decreasing memory usage if you’re pulling down integer values for example, especially if you’re then storing them in a cache such as APC or MySQL.  Read the patch for details, download page is here.

PHP INI Includes

  • Filed under: tech
Tuesday
Oct 9,2007

I’ve posted a patch that should allow you to use include statements in PHP’s INI files similar to Apache.  This is useful when you have complicated server configurations that can be managed better via linked include files rather than a scan directory.  Let me know if you have any problems, the patch page/download can be found here.

Apache UnsetErrorDocument

  • Filed under: tech
Thursday
Sep 27,2007

Apache-1.3.x doesn’t provide a way for you to “Unset” an ErrorDocument directive to it’s original value.  I’m providing a patch that will add a “UnsetErrorDocument” directive to reset the value.  This could happen when you have multiple configuration files that need to override each other.  You can find more information and a download here.  (Credit for the initial idea goes to Lucas)

Apache Source Defense

  • Filed under: tech
Monday
Sep 24,2007

I’ve released a small patch (read hack) that can help to prevent source code from being displayed by Apache. In theory this will work for any handler, but in specific it was designed with PHP in mind. The patch is intended to be a last line of defense if Apache attempts to display code with it’s default handler say due to a misconfiguration. It essentially filters out files with the defined extensions so that they cannot be handled by Apache’s default handler. This means you’ll have to hardcode your extensions into the patch. Please feel free to use this, but I’m not responsible for it not working as advertised. Lucas has posted a blog post a blog post about how we use something similar at Facebook. I’ve modified it for public release so hit me up with any problems.
Available via the MIT License

Download: ap_source_defense.patch

Delta runs on Linux

  • Filed under: tech
Thursday
Sep 20,2007

On the flight back from the PHP Works conference in Atlanta they had to reboot the screens on the back of everyone’s seats. This caught my attention when I saw the words “RedBoot” followed by the familiar Linux penguin. I would have liked to report that this was normal, but apparently they where having some “technical difficulties”, luckily for us we got an old fashioned demonstration of how to use a seat belt. Damn good thing too, as I really was having trouble figuring out how one end went in the other.