<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Willem Stuursma</title>
	<atom:link href="http://willem.stuursma.name/feed/" rel="self" type="application/rss+xml" />
	<link>http://willem.stuursma.name</link>
	<description>I want to write software that doesn&#039;t suck.</description>
	<lastBuildDate>Thu, 12 Jan 2012 12:45:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='willem.stuursma.name' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Willem Stuursma</title>
		<link>http://willem.stuursma.name</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://willem.stuursma.name/osd.xml" title="Willem Stuursma" />
	<atom:link rel='hub' href='http://willem.stuursma.name/?pushpress=hub'/>
		<item>
		<title>HipHop for PHP at Hyves</title>
		<link>http://willem.stuursma.name/2011/12/12/hiphop-for-php-at-hyves/</link>
		<comments>http://willem.stuursma.name/2011/12/12/hiphop-for-php-at-hyves/#comments</comments>
		<pubDate>Mon, 12 Dec 2011 17:27:06 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Hiphop for PHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hiphop]]></category>
		<category><![CDATA[HipHop for PHP]]></category>
		<category><![CDATA[hphp]]></category>
		<category><![CDATA[hyves]]></category>
		<category><![CDATA[linkedin]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=386</guid>
		<description><![CDATA[This is a cross-post from the Hyves Product Development blog. HipHop is Facebook’s open source C++ compiler for PHP. HipHop (also known as HPHP) will compile your PHP code to highly optimized C++ code, which you can then compile (with g++) into a big binary that will run your web site (it includes a web [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=386&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><em>This is <a href="http://hyvesblogonproductdevelopment.blogspot.com/2011/10/hiphop-for-php-at-hyves.html">a cross-post from the Hyves Product Development blog</a>.</em></p>
<p><strong>HipHop</strong> is <a class="zem_slink" title="Facebook" href="http://facebook.com" rel="homepage">Facebook</a>’s open source C++ compiler for PHP. HipHop (also known as <a class="zem_slink" title="HipHop for PHP" href="http://wiki.github.com/facebook/hiphop-php/" rel="homepage">HPHP</a>) will compile your PHP code to highly optimized C++ code, which you can then compile (with g++) into a big binary that will run your web site (it includes a web server). If you want to know more about Hiphop and how it was created, you can check out the Facebook Engineering Blog article or the Github project.</p>
<p>After its introduction many bloggers have written background articles regarding Hiphop but few seem to have actually used it in a production environment. At Hyves, we use HipHop to run our web servers and our daemons, which are also written in in PHP. In this blog, I will detail some of our experiences and results.</p>
<h2>Compiler and interpreter</h2>
<p>Hiphop includes both and interpreter and a compiler. This is something that is not frequently mentioned in blog posts on Hiphop. The interpreter (called HPHPi) can be used for developing, thus saving you the hassle and the delays associated with compiling every time you change a single file. In general is has the same behavior as the compiled version of Hiphop. It is a bit slower than using PHP with an opcode cache, but it is not that bad and with a fast laptop it is still workable.</p>
<p>The compiler can be used to create an optimized binary for your code base. The Hyves code base is in the order of 3.5M LOC in PHP. The conversion from PHP to C++ has to be done by a single server, but we use a dedicated cluster of sixteen servers for compiling the C++ source code to an executable binary. This takes about seven minutes including the building (e.g. minimizing style sheets and javascript).</p>
<p>The resulting binary is approximately 500MB size. This cannot be deployed to our web servers in a serial fashion, as copying it would take too long. To deploy this to our web servers, we use a <a href="http://assets.en.oreilly.com/1/event/74/Deploying%20Large%20Payloads%20At%20Scale%20Presentation.pdf">bittorent based p2p deploy system</a>. In case of an emergency, we can roll out a fix in approximately ten minutes to our 300 web servers.</p>
<p><strong>Update: Facebook just released a <a href="http://www.facebook.com/note.php?note_id=10150415177928920">JIT compiling VM for Hiphop</a>.</strong></p>
<h2>Behavioral differences</h2>
<p>The HPHP interpreter, the compiled version and the official PHP binary all have slightly different behavior. HipHop is about 99% compatible with pure PHP. 99% times 3.5M LOC is a lot of problems. If you want to run HipHop, make sure that your unit tests are ran against all three versions automatically or you will drive yourself and your team members insane, especially in a mixed PHP / HPHP environment.</p>
<p>Some <a class="zem_slink" title="PHP" href="http://www.php.net/" rel="homepage">PHP 5.3</a> stuff is still not completely supported in Hiphop, such as namespaces, of which at the moment nearly every feature is broken. Extensions such as PHP’s SOAP extension have been ported but some behavior differences exist between the different runtimes.</p>
<p>Converting the Hyves code base to run on HipHop took a big effort, several of our software engineers worked on this projects for months.</p>
<p>Hiphop has some really cool features. There is a <a href="http://willem.stuursma.name/2011/09/08/parallel-array_map-with-hiphop/">call_user_func_async()</a> function (it does exactly what it says), and you can have two versions of you web site running at the same time, so that you can deploy a new version without downtime. Also, you can catch fatal (E_FATAL) errors with their stack traces with HPHP, a feature sorely lacking from PHP.</p>
<p>There is only sparse online documentation of these specific features so you will have to figure a lot of it our by reading the source code.</p>
<h2>Performance</h2>
<p>Performance of our Hiphop enabled web site has been very good. In particular our API has strongly benefited, with API calls being twice times as fast and requiring only a quarter of the cpu power compared to plain PHP. This has resulted in much faster mobile clients and better performance of the parts of our web site that use internal API calls.</p>
<p><a href="http://willemst.files.wordpress.com/2011/10/responsetimes.png"><img class="size-medium wp-image-388 alignnone" title="responsetimes" src="http://willemst.files.wordpress.com/2011/10/responsetimes.png?w=300&#038;h=138" alt="" width="300" height="138" /></a></p>
<p>In the top graph you can see our web servers 95% and 99% response times. We enabled HipHop for all our web servers in March 2011. The higher response times just before the switch are due to internal network congestions because of some issues with the first versions p2p deploy system we used.</p>
<p><a href="http://willemst.files.wordpress.com/2011/10/cpu.png"><img class="alignnone size-medium wp-image-387" title="cpu" src="http://willemst.files.wordpress.com/2011/10/cpu.png?w=300&#038;h=163" alt="" width="300" height="163" /></a></p>
<p>The bottom graph shows the CPU load of our web servers, which has dropped by a factor of four after the switch to HipHop. This will enable us deprecate a big portion of our web servers in the future, resulting in high savings on hosting and power.</p>
<h2>Open source</h2>
<p>HipHop is an <a href="https://github.com/facebook/hiphop-php">open source project</a>, available on Github. If you file a bug, Facebook’s maintainers can be pretty responsive, well reported bugs can be fixed in a matter of days. Of course you can always fix it yourself on Github and try to get the fix pulled into the project.</p>
<p>Al though Facebook has reported that WordPress and Mediawiki are experimenting with HipHop, not many people actually seem to be using Hiphop in production. For now, the community is pretty small outside of Facebook.</p>
<p>Also Hyves contributed to the HipHop project, we contributed (among other things) bug reports, a number of bugfixes and the ability to use distcc so you can compile your web site on multiple machines.</p>
<h2>Conclusion</h2>
<p>All in all the switch to HipHop has been a big success for Hyves. Al though switching comes with a steep cost in terms of implementation and tooling, for a big web site such as Hyves it is definitely worth the effort. If you do not have at least a few dozen web servers, you should strongly consider buying more hardware in favor of spending a lot of effort on making Hiphop work for you.</p>
<h3>Update</h3>
<p>The original version of this article at the Hyves Product Development Blog made it to the <a href="http://news.ycombinator.com/item?id=3381093">front page of Hacker News</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/386/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/386/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/386/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=386&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/12/12/hiphop-for-php-at-hyves/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>

		<media:content url="http://willemst.files.wordpress.com/2011/10/responsetimes.png?w=300" medium="image">
			<media:title type="html">responsetimes</media:title>
		</media:content>

		<media:content url="http://willemst.files.wordpress.com/2011/10/cpu.png?w=300" medium="image">
			<media:title type="html">cpu</media:title>
		</media:content>
	</item>
		<item>
		<title>How to enable remote web inspector for UIWebView</title>
		<link>http://willem.stuursma.name/2011/12/09/how-to-enable-remove-web-inspector-for-uiwebview/</link>
		<comments>http://willem.stuursma.name/2011/12/09/how-to-enable-remove-web-inspector-for-uiwebview/#comments</comments>
		<pubDate>Fri, 09 Dec 2011 10:12:27 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[iOS development]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[iOS]]></category>
		<category><![CDATA[iPhone]]></category>
		<category><![CDATA[WebKit]]></category>
		<category><![CDATA[WebView]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=391</guid>
		<description><![CDATA[For a little while people have figured out how to enable the remove inspector in iOS applications. With this trick, you can get a Webkit inspector (Opt+Cmd+I) for a UIWebView running in an app on the iPhone simulator. This will give you Weinre like abilities but it works much more reliably. Then, it didn&#8217;t take long before [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=391&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="attachment_401" class="wp-caption alignright" style="width: 310px"><a href="http://willemst.files.wordpress.com/2011/12/schermafbeelding-2011-12-09-om-11-08-05.png"><img class="size-medium wp-image-401 " title="Remote debugging UIWebView" src="http://willemst.files.wordpress.com/2011/12/schermafbeelding-2011-12-09-om-11-08-05.png?w=300&#038;h=217" alt="" width="300" height="217" /></a><p class="wp-caption-text">Remote debugging UIWebView with the iOS simulator</p></div>
<p>For a little while people have <a href="http://atnan.com/blog/2011/11/17/enabling-remote-debugging-via-private-apis-in-mobile-safari/">figured out</a> how to enable the remove inspector in iOS applications. With this trick, you can get a <a class="zem_slink" title="WebKit" href="http://webkit.org/" rel="homepage">Webkit</a> inspector (Opt+Cmd+I) for a UIWebView running in an app on the iPhone simulator. This will give you <a href="http://phonegap.github.com/weinre/">Weinre</a> like abilities but it works much more reliably. Then, it didn&#8217;t take long before someone came up with an <a href="http://www.iwebinspector.com/">app to do all the GDB debugger attaching</a> for you.</p>
<p>However, enabling the Webkit remote inspector is a private API call and if you have it in your application when you submit it to the App Store, your application will be rejected. Also, it only works on the simulator, if it runs on a device you cannot access the remote inspector because of firewall rules.</p>
<p>An easy solution is to include some <a class="zem_slink" title="Preprocessor" href="http://en.wikipedia.org/wiki/Preprocessor" rel="wikipedia">precompiler</a> directives that will only add the private API call when you compiled your app for the iOS simulator:</p>
<pre>#if (TARGET_IPHONE_SIMULATOR)
    [NSClassFromString(@"WebView") _enableRemoteInspector]; // Private API call.
#endif</pre>
<p>You need to import the TargetConditionals.h file too:</p>
<pre>#ifdef __APPLE__
    #include "TargetConditionals.h"
#endif</pre>
<p>You can access the remote inspector by loading your app in the iOS simulator and navigating to http://localhost:9999/.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/391/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/391/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/391/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=391&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/12/09/how-to-enable-remove-web-inspector-for-uiwebview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>

		<media:content url="http://willemst.files.wordpress.com/2011/12/schermafbeelding-2011-12-09-om-11-08-05.png?w=300" medium="image">
			<media:title type="html">Remote debugging UIWebView</media:title>
		</media:content>
	</item>
		<item>
		<title>I just love this little Google maps detail</title>
		<link>http://willem.stuursma.name/2011/10/26/i-just-love-this-little-google-maps-detail/</link>
		<comments>http://willem.stuursma.name/2011/10/26/i-just-love-this-little-google-maps-detail/#comments</comments>
		<pubDate>Wed, 26 Oct 2011 19:21:20 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Maps]]></category>
		<category><![CDATA[usability]]></category>
		<category><![CDATA[google maps]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=373</guid>
		<description><![CDATA[I just love this little Google maps detail: the preview of the street / satellite view is not just a static image, but shows exactly what you will get when you click it. If you move the map around, the preview of the other view also moves. Unfortunately, this feature is only available in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=373&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I just love this little Google maps detail: the preview of the street / satellite view is not just a static image, but shows exactly what you will get when you click it. If you move the map around, the preview of the other view also moves.</p>
<p><a href="http://willemst.files.wordpress.com/2011/10/schermafbeelding-2011-10-14-om-09-08-39.png"><img class="aligncenter size-full wp-image-392" title="Detail of the upper right corner of Google maps" src="http://willemst.files.wordpress.com/2011/10/schermafbeelding-2011-10-14-om-09-08-39.png?w=600" alt=""   /></a></p>
<p>Unfortunately, this feature is only available in the <a class="zem_slink" title="Google Chrome" href="http://www.google.com/chrome" rel="homepage">Google Chrome browser</a>.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/373/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/373/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/373/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=373&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/10/26/i-just-love-this-little-google-maps-detail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>

		<media:content url="http://willemst.files.wordpress.com/2011/10/schermafbeelding-2011-10-14-om-09-08-39.png" medium="image">
			<media:title type="html">Detail of the upper right corner of Google maps</media:title>
		</media:content>
	</item>
		<item>
		<title>Character sets at Hyves</title>
		<link>http://willem.stuursma.name/2011/10/09/character-sets-at-hyves/</link>
		<comments>http://willem.stuursma.name/2011/10/09/character-sets-at-hyves/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 08:33:46 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Work]]></category>
		<category><![CDATA[Character encoding]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[Windows-1252]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=379</guid>
		<description><![CDATA[At my work I gave a short tech lunch presentation on character sets at Hyves. At Hyves we unfortunately still use Windows-1252 because of legacy reasons, which causes many issues. Included are a short reminder of what character sets are, how to use them, collation, best practices and common pitfalls. Finally, there are some tips [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=379&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>At my work I gave a short tech lunch presentation on character sets at <a class="zem_slink" title="Hyves" href="http://www.hyves.nl" rel="homepage">Hyves</a>. At Hyves we unfortunately still use <a class="zem_slink" title="Windows-1252" href="http://en.wikipedia.org/wiki/Windows-1252" rel="wikipedia">Windows-1252</a> because of legacy reasons, which causes many issues.</p>
<p>Included are a short reminder of what character sets are, how to use them, collation, best practices and common pitfalls. Finally, there are some tips for debugging issues with character sets.</p>
<iframe src="https://docs.google.com/present/embed?id=dc947tvk_37fn2jp7gv" frameborder="0" width="410" height="342"  marginheight="0" marginwidth="0"></iframe>
<p>If you have any questions after viewing this presentation, please ask.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/379/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/379/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/379/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=379&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/10/09/character-sets-at-hyves/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>How to enable XHP on Hiphop for PHP</title>
		<link>http://willem.stuursma.name/2011/10/09/how-to-enable-xhp-on-hiphop-for-php/</link>
		<comments>http://willem.stuursma.name/2011/10/09/how-to-enable-xhp-on-hiphop-for-php/#comments</comments>
		<pubDate>Sun, 09 Oct 2011 08:26:38 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Hiphop for PHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hiphop]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[xhp]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=357</guid>
		<description><![CDATA[A quick primer on how to enable XHP on Hiphop for PHP: Set the Eval.EnableXHP to true to that XHP will work on the interpreter if you need that. This will enable the XHP processor which will convert xml syntax to the __xhp classes. Get the XHP PHP extension from https://github.com/facebook/xhp Copy the files in the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=357&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>A quick primer on how to enable <strong>XHP</strong> on <strong>Hiphop for PHP</strong>:</p>
<ol>
<li>Set the Eval.EnableXHP to true to that XHP will work on the interpreter if you need that. This will enable the XHP processor which will convert xml syntax to the __xhp classes.</li>
<li>Get the XHP PHP extension from https://github.com/facebook/xhp</li>
<li>Copy the files in the php-lib/ folder to your project</li>
<li>Include them in the source files where you want to use XHP.</li>
</ol>
<div>You can now use the XML syntax in PHP.</div>
<div>The authors of XHP added array dereferencing as a bonus to XHP, so you can now also use that.</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/357/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/357/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/357/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=357&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/10/09/how-to-enable-xhp-on-hiphop-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>Parallel array_map() with HipHop</title>
		<link>http://willem.stuursma.name/2011/09/08/parallel-array_map-with-hiphop/</link>
		<comments>http://willem.stuursma.name/2011/09/08/parallel-array_map-with-hiphop/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 19:55:16 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Hiphop for PHP]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[hiphop]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[Parallel computing]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Threads]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=346</guid>
		<description><![CDATA[Many computational operations, such as iterating over some big list can be sped up if they are parallelized. Parallelization offers the benefit that multiple items in the iterator can be processed at the same time. This is accomplished by using multiple threads, running the processing on multiple CPU cores at the same time. One condition has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=346&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Many computational operations, such as iterating over some big list can be sped up if they are parallelized. Parallelization offers the benefit that multiple items in the iterator can be processed at the same time. This is accomplished by using multiple threads, running the processing on multiple CPU cores at the same time.</p>
<p>One condition has to be met before it is possible to use multiple threads to process an iterator: each processing operation on an item in the operator must be independent on the other items. The typical PHP function to use in such cases is <a href="http://php.net/array_map">array_map()</a>, which allows you to run a function over each item in an array. array_map() prohibits you to access other items in the iterator while processing an item, so the order of execution is not important.</p>
<p>In the past it has been very hard to effectively use parallelization in PHP. However, with <a href="http://willem.stuursma.name/2011/06/12/some-experiences-using-facebooks-hphp-hiphop/">Facebook&#8217;s Hiphop</a> Facebook engineers have added the option to create threads in PHP using the <a href="https://github.com/facebook/hiphop-php/blob/6fb21c0dcae39301aa2dec45a19291a769aa9ef5/doc/threading">call_user_func_async()</a> function.</p>
<p>Some caveats apply:</p>
<ul>
<li>You can only create two extra threads, next to the main thread that is already running. Creating more threads doesn&#8217;t have any effects.</li>
<li>Creating additional threads is expensive, so you have to carefully consider if it is worthwhile to use them.</li>
<li>Unlike all other functions in PHP that accept functions as arguments, the call_user_func_async() does not accept anonymous functions as arguments. You need to pass in a string or array callback.</li>
<li>The call_user_func_async() function is <a href="https://github.com/facebook/hiphop-php/blob/6c59bb632eb862d7e2657d4d339a84718b52fae4/doc">currently deprecated</a> in HipHop and will issue a warning when called.</li>
</ul>
<p>call_user_func_async() and array_map() can be combined into a parallel version of array_map(). This can be done by splitting the iterator in equal parts and then running the iterator over each part, each part in a separate thread. Finally, you need to put all the pieces back together (synchronize) by calling end_user_func_async() for each thread.</p>
<p>You can find <a href="https://github.com/willemstuursma/hiphop-util/blob/master/functions/function.array_map_parallel.php">my implementation</a> at GitHub.</p>
<p>In practice the use of this function is limited: the maximum number of threads is three and there is quite a high cost for creating threads. However for complex processing or processing that calls external services (such as databases) that block it might be worth the extra effort.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/346/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/346/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/346/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=346&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/09/08/parallel-array_map-with-hiphop/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>Some experiences using Facebook&#8217;s HPHP / Hiphop</title>
		<link>http://willem.stuursma.name/2011/06/12/some-experiences-using-facebooks-hphp-hiphop/</link>
		<comments>http://willem.stuursma.name/2011/06/12/some-experiences-using-facebooks-hphp-hiphop/#comments</comments>
		<pubDate>Sun, 12 Jun 2011 15:42:31 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Hiphop for PHP]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[hiphop]]></category>
		<category><![CDATA[hphp]]></category>
		<category><![CDATA[hyves]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=335</guid>
		<description><![CDATA[Hiphop is Facebook&#8217;s open source C++ compiler for PHP. Hiphop (also known as HPHP) will compile your PHP code to C++ code, which you can then compile (with g++) into a big binary that will run your web site. The binary includes a web server, which will then behave (almost) exactly the same as your [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=335&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Hiphop</strong> is Facebook&#8217;s open source C++ compiler for PHP. Hiphop (also known as <strong>HPHP</strong>) will compile your PHP code to C++ code, which you can then compile (with g++) into a big binary that will run your web site. The binary includes a web server, which will then behave (almost) exactly the same as your Apache + mod_php or Nginx with fastcgi PHP. If you want to know more about Hiphop and how it was created, you can check out the <a href="https://www.facebook.com/note.php?note_id=280583813919">Facebook Engineering Blog article</a> or the <a href="https://github.com/facebook/hiphop-php/">Github project</a>.</p>
<p>After its introduction many bloggers <a href="http://sebastian-bergmann.de/archives/880-My-Take-on-Facebooks-HipHop-for-PHP.html">have</a> <a href="http://www.insidefacebook.com/2010/02/02/facebook-open-sources-hiphop-php-compiler-software/">written</a> background articles regarding Hiphop but few seem to have actually used it in a production environment. I have and here are some of my thoughts on working with Hiphop in such an environment.</p>
<h3>Speed</h3>
<p>Hiphop includes both and interpreter and a compiled version. This is something that is not frequently mentioned in blog posts about Hiphop. The interpreter (called <em>HPHPi</em>) can be used for developing, thus saving you the hassle and the delays associated with compiling every time you change a single file. (Compiling a simple 10 line script takes about one minute. This is probably because the web server has to be compiled too and this component is a few orders of magnitude more complex than my PHP script). In general is has the same behavior as the compiled version of Hiphop. It is a bit slower than using PHP with an opcode cache, but it is not that bad and with a fast laptop it is still workable.</p>
<p>The compiled version is really fast, at least a few times faster than using PHP with fastcgi. Also, Hiphop has become already <a href="https://www.facebook.com/note.php?note_id=10150121348198920">three times as fast</a> since it has been released in Februari 2010. So the speed is really, really good.</p>
<h3>Behavioral differences</h3>
<p>The HPHP interpreter, the compiled version and the official PHP interpreter will all give you ever so slightly different behavior. So make sure that your unit tests are ran against all three versions automatically or you will drive yourself and your team members insane, especially in a mixed PHP / HPHP environment.</p>
<p>Some PHP 5.3 stuff is still not completely supported in Hiphop, such as namespaces, of which at the moment nearly every feature is broken. Support for anonymous functions have only recently been added. Extensions such as PHP&#8217;s SOAP extension have been ported but some behavior varies between the variants. Small stuff mostly. For example, if you get a SoapFault exception somewhere, in PHP the exception will contain the message from the SOAP response, but in HPHP the exception will contain the entire SOAP response.</p>
<p>If you want to write scripts that are PHP and HPHP compatible, you will need to write some if statements here and there. Definitely test all your code against the compiled, interpreted and official PHP binary.</p>
<h3>Features</h3>
<p>Hiphop has some really cool features. There is a call_user_func_async() function (it does exactly what it says), and you can have two versions of you web site running at the same time, so that you can deploy a new version without downtime. Also, you can catch fatal (E_FATAL) errors with their stack traces with HPHP, a feature sorely lacking from PHP. There is only sparse online documentation of these specific features, but you can set up <a href="https://github.com/facebook/hiphop-php/wiki/HipHop-Documentation">your own documentation server</a>.</p>
<p>Among these new features, HPHP introduces some new errors that can occur in your scripts, mostly when you do strange stuff which you are not supposed to some datatypes., but PHP still allows. For example, in PHP this will not throw any errors: $var = null; $var['foo'] = &#8216;bar&#8217;;. In HPHP, this will throw an E_NOTICE. I will leave it as an exercise to the reader to decide which of the two behaviors actually is preferred. In general, HPHP is more strict and less forgiving, so that will probably cause latent bugs in your project to rise to the surface.</p>
<p>You gain some, you loose some: some PHP features such as fastcgi_finish_request() are not available under Hiphop. If you want any of the non included PHP extensions, you will have to port them to C++ yourself. Since they are already in C, it is not hard but you still need to do it.</p>
<h3>Open source</h3>
<p>One of the great things about Hiphop is that is is an open source project. The code for Hiphop is shared on Github. If you file a bug, Facebook&#8217;s maintainers can be pretty responsive. I filed a one (behavior difference from PHP for anonymous functions) and it was fixed four days later. Of course you can always fix it yourself on Github and try to get the fix pulled into the project. Not many people are actually using Hiphop in production, so the community is pretty small outside of Facebook.</p>
<h3>Conclusions</h3>
<p>Facebook&#8217;s Hiphop is really, really fast and offers useful features. However, you can imaging that with the behavioral differences and the changes you will need to your build and deploy process it may or may not be worth it for your web site.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/335/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/335/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/335/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=335&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/06/12/some-experiences-using-facebooks-hphp-hiphop/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>Handling errors when dynamically creating class methods in PHP</title>
		<link>http://willem.stuursma.name/2011/02/25/handling-errors-when-dynamically-creating-class-methods-in-php/</link>
		<comments>http://willem.stuursma.name/2011/02/25/handling-errors-when-dynamically-creating-class-methods-in-php/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 18:59:54 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[error handling]]></category>
		<category><![CDATA[exception handling]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[overloading]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=327</guid>
		<description><![CDATA[In PHP, it is possible to dynamically create class methods using by implementing the magic __call() method on a class. This function will then be called when accessing a method that is not visible in the current scope, e.g. a private method outside the object&#8217;s scope or a non-existent method. PHP will pass the called [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=327&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In PHP, it is possible to dynamically create class methods using by implementing the magic <a href="http://nl.php.net/__call">__call()</a> method on a class. This function will then be called when accessing a method that is not visible in the current scope, e.g. a private method outside the object&#8217;s scope or a non-existent method. PHP will pass the called method&#8217;s name as the first argument and an array of arguments as the second argument.</p>
<p>Many programmers assume that when the __call() method returns false, this will indicate to the PHP interpreter that the requested method is not (dynamically) available and that the normal error handling will then take over and trigger an E_FATAL error.</p>
<p>This is not the case in PHP! The method call will complete successfully, returning false. <strong>So if you make a typo or whatever there will not be an error, but it will silently succeed.</strong> This is a bit unintuitive, because in other cases returning false will trigger the default behavior in PHP. For example, if you set a custom error handler and have it return false, the normal error handler will take over. But with overloading methods this works a bit different.</p>
<p>The best way to deal with this is by raising an error or throwing an exception from the __call() method whenever you are unsure what to do with the dynamic method call. The <a href="http://nl3.php.net/spl">Standard PHP library</a> provides the excellent <a href="http://php.net/manual/en/class.badmethodcallexception.php">BadMethodCallException</a>, which you can throw when there is a &#8211; you guessed it &#8211; bad method call.</p>
<p>An example with dynamic getters and setters:</p>
<pre>class a {
    private $foo = "bar";
    public function __call($method, $args) {
        $property = substr($method,3);
        if (substr($method, 0, 3) == 'get' &amp;&amp; isset($this-&gt;$property)) {
            return $this-&gt;$property;
        }

        if (substr($method, 0, 3) == 'set' &amp;&amp; isset($this-&gt;$property)) {
            $this-&gt;$property = $args[0];
            return;
        }
        throw new BadMethodCallException();
    }
}</pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/327/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/327/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/327/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=327&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2011/02/25/handling-errors-when-dynamically-creating-class-methods-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>Disk space disappearing in OS X with Filevault</title>
		<link>http://willem.stuursma.name/2010/12/30/disk-space-disappearing-in-os-x-with-filevault/</link>
		<comments>http://willem.stuursma.name/2010/12/30/disk-space-disappearing-in-os-x-with-filevault/#comments</comments>
		<pubDate>Thu, 30 Dec 2010 09:41:56 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Disk Management]]></category>
		<category><![CDATA[disk space]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[filevault]]></category>
		<category><![CDATA[Mac OS X]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=321</guid>
		<description><![CDATA[Quick post: if you use Mac OS X&#8217;s Filevault and you feel that disk space is disappearing when you use OS X, and deleting files does not give you any space back, just log out and log back in. The missing disk space will then reappear. Some background information: Filevault creates an encrypted disk image [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=321&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong>Quick post: if you use Mac OS X&#8217;s Filevault and you feel that disk space is disappearing when you use OS X, and deleting files does not give you any space back, just log out and log back in. The missing disk space will then reappear.</strong></p>
<p>Some background information: Filevault creates an encrypted disk image that contains your user profile. When files are deleted from your profile, the encrypted disk image does not get resized. Resizing only happens when you log out, you will see the &#8220;Filevault is recovering disk space&#8221; message. This only takes a few seconds and will resize the encrypted disk image to the actual needed size. If you log back in, you will see that the free disk space is now there.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/321/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/321/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/321/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=321&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2010/12/30/disk-space-disappearing-in-os-x-with-filevault/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
		<item>
		<title>A detailed look into array_map() and foreach</title>
		<link>http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/</link>
		<comments>http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/#comments</comments>
		<pubDate>Mon, 22 Nov 2010 22:24:07 +0000</pubDate>
		<dc:creator>stuursma</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[array_map]]></category>
		<category><![CDATA[foreach]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[iterators]]></category>
		<category><![CDATA[linkedin]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[php performance]]></category>

		<guid isPermaLink="false">http://willem.stuursma.name/?p=313</guid>
		<description><![CDATA[One of the features introduced with PHP 5.3 is the improved support for anonymous functions and closures(sometimes also called lambda functions). Although they were possible with earlier PHP versions, their usage (through the create_function() function) was very cumbersome. In this post, I mainly want to discuss the differences between PHP&#8217;s foreach control structure and the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=313&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the features <a href="http://php.net/releases/5_3_0.php">introduced with PHP 5.3</a> is the improved support for <a href="http://nl2.php.net/manual/en/functions.anonymous.php">anonymous functions and closures</a>(sometimes also called lambda functions). Although they were possible with earlier PHP versions, their usage (through the <a href="http://nl2.php.net/manual/en/function.create-function.php">create_function()</a> function) was very cumbersome.</p>
<p>In this post, I mainly want to discuss the differences between PHP&#8217;s <a href="http://nl2.php.net/manual/en/control-structures.foreach.php">foreach control structure</a> and the <a href="http://nl2.php.net/array_map">array_map()</a> function. Both methods allow you to iterate over an array. As an example I present two methods to calculate the squares of all elements in an array. The traditional foreach approach would be something like</p>
<pre>foreach ($array as $key =&gt; $value) {
    $array[$key] *= $value;
}</pre>
<p>Quite the difference compared with an anonymous function and array_map():</p>
<pre>$array = array_map(function($each) { return $each * $each;}, $array);</pre>
<p>Both approaches have very different characteristics, I will discuss these in this post.</p>
<h2>Performance</h2>
<p>There exists a big performance gap between the two methods. The fastest is the foreach construct. This construct can about 70% faster than an anonymous function, at least when there is not much to do within the loop (see comments below). In most scenarios, the time it takes to iterate over the array is negligible compared to the time spend in the loop or anonymous function. So this performance difference does not really matter.</p>
<p>An interesting difference is peak memory usage. With big arrays, the peak memory usage of the array_map() function may be 2.5 times as much as the foreach loop. This is probably because this function creates an in memory copy of the array. If you have really big arrays, this can become a problem. One way to deal with this is to pass the array (second) argument to array_map() by reference (using the &amp; operator). Although this is deprecated, it can reduce memory usage to a &#8220;mere&#8221; twice as much as a foreach loop.</p>
<h2>Default anonymous functions</h2>
<p>Anonymous functions cannot be used as default values for class properties or function arguments. This is probably because the anonymous function is internally represented by the <a href="http://nl2.php.net/manual/en/reserved.classes.php#reserved.classes.closure">Closure class</a>.</p>
<p>If you want to have a default function argument for a function, you will have to use null and check for that in  the function. Default functions as arguments can be very useful for functions that accept functions as arguments. The following examples will unfortunately gives you a compile error (unexpected T_FUNCTION):</p>
<pre>public function reduce(array $array, $function = function($each) { return !$each;}) {
    return array_filter($array, $function);
}</pre>
<p>You will have to move the default value into the function:</p>
<pre>public function reduce(array $array, Closure $function = null) {
    if ($function === null) {
        $function = function($each) { return !$each; };
    }
    return array_filter($array, $function);
}</pre>
<p>It is possible to use typehinting to signal that you want a function as an argument for a function. You can do this by using the Closure class. However, the PHP documentation clearly specifies that no code should rely on this class. In future PHP versions, the class might be renamed or replaced altogether.</p>
<h2>Scoping</h2>
<p>An advantage of using anonymous functions to iterate over arrays is in scoping. PHP has very little support for scoping as compared to, for example, C. The foreach example above will leave $key and $value available after the loop has been completed. Although this can be very useful, it can also be confusing and lead to bugs. For example, $value could overwrite a variable with the same name set earlier. These kind of bugs might be very hard to spot.</p>
<p>Within the anonymous function all used variables are properly scoped and cannot influence outside variables unless specifically specified with the use statement. After the iteration has been completed, no variables are left lingering around.</p>
<h2><code>break</code> and <code>continue</code></h2>
<p>The foreach loop allows you to use break and continue statements to break out of the loop or out of the current iteration of the loop. This can be very beneficial for performance reasons. Imagine that you would like to check if all elements of an array are non-empty. With foreach, you can just break out of the loop when you encounter the first empty element. With array_map() this is not possible.</p>
<p>The Prototype Javascript library solves this by introducing two special Exceptions, $break and $continue. When either of these exceptions is thrown, the iteration over the array is stopped and prototype makes sure that the correct behavior the occurs. As PHP does not have these magical exceptions implemented, you would need to create them yourself.</p>
<h2>Miscellaneous</h2>
<p>Foreach allows you access the other elements in the array too, for example to compare the current value with the previous one (if you use the $key =&gt; $value syntax). This is not possible with the array_map() and array_walk() functions.</p>
<p>The syntax for foreach has a few caveats. You cannot write to the $value variable, unless you write a foreach statment by reference or reference the array and its key:</p>
<pre>foreach ($array as &amp;$value) $value *= $value;
foreach ($array as $key =&gt; $value) $array[$key] *= $value;</pre>
<p>Either will work.</p>
<p>Finally, if you use Facebook&#8217;s <a href="https://github.com/facebook/hiphop-php/wiki/">Hiphop compiler for PHP</a> to speed up your web site, you have a problem as the anonymous function syntax is not yet supported by Hiphip. In that case you will have to fall back to your old friend create_function(). This leaves you without the pretty syntax and support for the use argument.</p>
<h2>Conclusion</h2>
<p>Ultimately, both foreach and the array_map() + anonymous functions have their pros and cons. Anonymous functions are especially suited for operations that change all values of an array. They also offer strickt scoping which can prevent bugs.</p>
<p>There is also a strong matter of personal preference involved. With anonymous functions you can write your code in a totally new style. If you like this and can deal with the lack of continue and break, anonymous functions may be the way to go. For example, it is not very hard to implement <a href="http://www.prototypejs.org/api/enumerable">Prototype&#8217;s Enumerable</a> in PHP with the use of array_map() and anonymous functions.</p>
<p>If you are more a procedural type of guy (or girl), stick with the foreach control structure.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/willemst.wordpress.com/313/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/willemst.wordpress.com/313/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/willemst.wordpress.com/313/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=willem.stuursma.name&amp;blog=6028144&amp;post=313&amp;subd=willemst&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://willem.stuursma.name/2010/11/22/a-detailed-look-into-array_map-and-foreach/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/eeeed33e97bb956677957d0e24700c36?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">stuursma</media:title>
		</media:content>
	</item>
	</channel>
</rss>
