<?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/"
	>

<channel>
	<title>Mike Gerwitz &#187; Development</title>
	<atom:link href="http://mikegerwitz.com/cat/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://mikegerwitz.com</link>
	<description>Free Software Hacker</description>
	<lastBuildDate>Mon, 07 May 2012 14:14:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Value of Vim Swap Files</title>
		<link>http://mikegerwitz.com/value-of-vim-swap-files/</link>
		<comments>http://mikegerwitz.com/value-of-vim-swap-files/#comments</comments>
		<pubDate>Sun, 22 May 2011 16:04:51 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=526</guid>
		<description><![CDATA[My nightly development routine is fairly consistent. I hack some code, commit it, hack some. Before bed, I take a look at the commits and see if everything is in a stable state that actually makes sense to push. If so, I push it. Otherwise, I save it for another day. I then suspend my [...]]]></description>
			<content:encoded><![CDATA[<p>My nightly development routine is fairly consistent. I hack some code, commit it, hack some. Before bed, I take a look at the commits and see if everything is in a stable state that actually makes sense to push. If so, I push it. Otherwise, I save it for another day. I then suspend my PC and head off to bed. If I find myself in bed, having forgotten that last step, I&#8217;ll hit a button on my N900 which runs a script to put my PC to sleep with me. I&#8217;ll then lay awake for at least a half hour pondering the same problem I was working to solve before bed, mulling over the source code in my head.</p>
<p>Last night was no different. I was working on finishing up some final features before the release of version 0.1.0 of ease.js. It wasn&#8217;t a horribly productive day (code-wise), but it was still time that I had to squeeze in amongst other errands and the rest of my life. As usual, I committed my changes, found that I wasn&#8217;t in a state I wanted to push, suspended my PC and went off to bed. When I awoke in the morning, I found that I wasn&#8217;t able to do anything without filesystem errors. None of my drives were readable or writable.</p>
<p>Alright. It&#8217;s happened before. I&#8217;ll just reboot, run fsck and hope nothing important was corrupted. I wasn&#8217;t able to reboot properly (due to filesystem errors), so I just did a hard reset. To my relief, the PC booted back up just fine. I was able to reconnect to the network, restore my screen session on my server for <acronym title="Internet Relay Chat">IRC</acronym> and re-open vim to begin hacking. All seemed well.</p>
<p>Normally, when I open vim after shutting down without giving it a chance to close, I&#8217;m spammed with messages regarding its swap files. While vim is running, it writes your changes to a swap file, one per open file, just in case something happens that causes vim to shut down without being able to save your changes. Next time you open that file, vim will notice that there&#8217;s already a swap file an will prompt the user, asking what should be done. Should vim recover from the swap file, delete it, or ignore it and edit the file anyway? Normally, I delete them all and start fresh, because everything is normally committed in Git.</p>
<p>But I noticed something odd. My shell, when in a Git project dir, will add additional information to the prompt (see git-ps1). Rather than saying &#8220;master&#8221; as it should of, it said &#8220;HEAD&#8221;. Sensing a problem, I didn&#8217;t delete the swap files. Instead, I typed &#8216;git status&#8217; and was rather discomforted to find that every file was listed as modified and no previous commits were recognized.</p>
<p>Okay. That&#8217;s happened before too. Somehow, the HEAD was lost, likely due to corruption. Since most commands fail with an invalid HEAD, I just cloned my repository from GitHub and put the SHA-1 hash of its head into .git/HEAD. Worked like a charm. Unfortunately, <tt>`git reflog`</tt> was not displaying anything useful, so I couldn&#8217;t just check out one of my more recent commits. I then ran <tt>`git fsck --lost-found`</tt> to hopefully recover the commits I had made since I last pushed. It complained with a fatal error, stating that a commit was corrupt.</p>
<p>Suffice it to say that many commits were corrupt. After a while of trying every trick in the book for recovering lost commits (which I&#8217;m not going to go over here), I decided that it was a lost cause. Not having run <tt>`git gc`</tt> since the start of the project, there were files everywhere. There was no way I was going to manually sort through these to figure out my commit, and <tt>`git fsck`</tt> was useless because, every time I recovered or removed corrupt commits, another one was a problem. It was clear that this repository was a lost cause.</p>
<p>No developer likes to lose hours of work. As I said, I have to squeeze the time in when I can. Lost time is truly lost. I only have so much time in my day and, if I couldn&#8217;t recover that work, I may not be able to make the v0.1.0 release that I had hope to this weekend. Or at least get as close as possible to it. So, naturally, that was very disappointing.</p>
<p>Recovering the exact commits would have been ideal. The timestamps, original commit messages, etc are nice to have. With that lost, I at least wanted to try to recover the code. If I could do that, I could split it into a number of commits and hopefully get back to where I was before. So, I looked back to vim. Because I was unable to shut down the PC properly, none of the swap files were cleaned up. Therefore, I moved my old, corrupt repository to a temporary location, moved the new clone into the same dir as the original, and then chose the &#8220;recover&#8221; option for each of the files in my session and wrote them all to disk. That recovered most of my old files, which was very exciting.</p>
<p>Before I had shut down, I attempted to use <tt>:mksess</tt> to create a session to restore all my open files after reboot. However, since the disk was not writable, I was unable to do so. Therefore, I was stuck with an old session, which did not reload many of the files I had open. Some of those files didn&#8217;t even exist in the newly cloned repository. Fortunately, simply opening those non-existent files in vim would cause the swap file to be recognized. Vim would prompt, I would recover from the swap file, write it to disk and have my old file back.</p>
<p>Though I lost the commits, I now had all of my lost code back where it belongs. After recommitting everything, I didn&#8217;t have as many commits as I had previously, nor did I have the original messages, timestamps, etc, but at least I didn&#8217;t lose any actual work. Just a little bit of time, for which I was able to share an interesting experience. That has not been the only time that vim&#8217;s swap files have saved me from strangling myself. While it&#8217;s not good to rely on them for a backup solution, they are able to help in situations where you&#8217;re left with no other choice.</p>
<p>Had the entire disk been corrupted, I still would have been okay. My repository is on a separate physical disk than my home directory. Using the <tt>directory</tt> option in vim, you can set a different location for your swap files (it&#8217;s in the same directory as the file, by default). I set mine to ~/.vim-tmp by default. Had they been in the same directory, it&#8217;s likely that they may have been accidently lost while I was screwing around with Git trying to recover everything. If the entire disk did fail, I would have still had my work in the swap files, so I would have lost only the commits, not the code.</p>
<p>Moral of the story &#8211; store your swap files wisely. They&#8217;re an annoying pest when you don&#8217;t need them, but highly valuable in the off chance that they are needed.</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/value-of-vim-swap-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE toString() Not Enumerable: Workaround</title>
		<link>http://mikegerwitz.com/ie-tostring-not-enumerable-workaround/</link>
		<comments>http://mikegerwitz.com/ie-tostring-not-enumerable-workaround/#comments</comments>
		<pubDate>Sun, 06 Mar 2011 16:21:50 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=520</guid>
		<description><![CDATA[While working on a project of mine, I noticed that overriding toString() on an object would not cause it to show up when looping through the object&#8217;s properties with for in Internet Explorer 6 (I have not tested what other versions are affected). My suspicious were verified on StackOverflow: http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293 To clarify, this is the [...]]]></description>
			<content:encoded><![CDATA[<p>While working on a project of mine, I noticed that overriding toString() on an object would not cause it to show up when looping through the object&#8217;s properties with <tt>for</tt> in Internet Explorer 6 (I have not tested what other versions are affected). My suspicious were verified on StackOverflow:</p>
<p><a href="http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293">http://stackoverflow.com/questions/3679233/are-there-still-ecmascript-3-implementation-differences-in-major-browsers/3679293#3679293</a></p>
<p>To clarify, this is the problem:</p>

<div class="wp_codebox"><table><tr id="p5203"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p520code3"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    obj.<span style="color: #660066;">toString</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> prop <span style="color: #000066; font-weight: bold;">in</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// &quot;prop&quot; will never show 'toString' in IE</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>I needed some way to work around this. The solution needed to be fairly elegant &#8211; that is, I don&#8217;t want to have to pass in an argument stating &#8220;yes, I set the toString property&#8221;. My workaround, though not pretty, was to test to see if toString was set:</p>

<div class="wp_codebox"><table><tr id="p5204"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p520code4"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">function</span> hasToString<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> obj.<span style="color: #660066;">toString</span> <span style="color: #339933;">===</span> Object.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">toString</span> <span style="color: #009900;">&#41;</span>
            <span style="color: #339933;">?</span> <span style="color: #003366; font-weight: bold;">false</span>
            <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
        <span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If the <tt>toString</tt> property of the object is equal to that of its prototype, then it&#8217;s safe to say that the property was not overwritten. However, there is a problem. We&#8217;re using <tt>Object.prototype</tt> for this test. We are not checking the parent. This is because <acronym title="Internet Explorer">IE</acronym> does not have a <tt>__proto__</tt> property like other browsers such as FF or Chromium. So we cannot test against the actual prototype.</p>
<p>This means that <em>the above function will give you an incorrect result if the <tt>toString</tt> property was previously overridden</em>.</p>
<p>Fortunately, in my use case, that will never be an issue. If it is an issue for you, then make sure you replace <tt>Object</tt> with whatever <tt>obj</tt> is an instance of.</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/ie-tostring-not-enumerable-workaround/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Node.js modules and instanceof operator</title>
		<link>http://mikegerwitz.com/node-js-modules-and-instanceof-operator/</link>
		<comments>http://mikegerwitz.com/node-js-modules-and-instanceof-operator/#comments</comments>
		<pubDate>Wed, 10 Nov 2010 03:19:19 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Node.js]]></category>
		<category><![CDATA[node]]></category>

		<guid isPermaLink="false">http://mikegerwitz.com/?p=426</guid>
		<description><![CDATA[There are many moments in a developer&#8217;s life where things simply don&#8217;t make sense. You test even the most obscure solutions to try to figure out the problem, when the actual solution ends up making perfect sense (though it may be subtle). The case of modules in Node.js with use of the instanceof operator was [...]]]></description>
			<content:encoded><![CDATA[<p>There are many moments in a developer&#8217;s life where things simply don&#8217;t make sense. You test even the most obscure solutions to try to figure out the problem, when the actual solution ends up making perfect sense (though it may be subtle). The case of modules in <a href="http://nodejs.org">Node.js</a> with use of the <tt>instanceof</tt> operator was one of those instances.</p>
<p>Modules are brought into scope with the <tt>require</tt> function. You then have access to the exports of that module. In the example below, we&#8217;re interested in the <tt>Foo</tt> class of the <tt>foobar</tt> module.</p>

<div class="wp_codebox"><table><tr id="p42610"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p426code10"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Load the &quot;Foo&quot; class from the &quot;foobar&quot; module</span>
<span style="color: #003366; font-weight: bold;">var</span> Foo <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    obj <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// we would expect the following to be true, and we are correct</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span></pre></td></tr></table></div>

<p>Obviously, <tt>obj</tt> is an instance of <tt>Foo</tt>. Pretty straightforward. If you use the module elsewhere in your code, you would expect that <tt>obj</tt> is still an instance of <tt>Foo</tt>:</p>

<div class="wp_codebox"><table><tr id="p42611"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code" id="p426code11"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> foo<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> foo<span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></td></tr></table></div>

<p>Huh? That above function, <tt>foo</tt>, is returning <tt>false</tt>! That did not make sense to me. As it turns out, the problem was with how I was including the module. Early on in the project, I included the module as above &#8211; by prepending <tt>./</tt> to the path. However, a more convenient method is to simply add it to the include path:</p>

<div class="wp_codebox"><table><tr id="p42612"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p426code12"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// add $CWD to the include path</span>
require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You will notice that, in <tt>foo()</tt>, we were using a different path for the module. They may <em>resolve</em> to the same path ultimately, but Node seems to keep track of them differently.</p>

<div class="wp_codebox"><table><tr id="p42613"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p426code13"><pre class="javascript" style="font-family:monospace;">require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> Foo1 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    Foo2 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'./foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    Foo3 <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'foobar'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">,</span>
    obj  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Foo1<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo2 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// true</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> Foo3 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// false</span></pre></td></tr></table></div>

<p>This was tested with node v0.3.1-pre. As far as I am concerned, this is a bug. <tt>Foo1</tt>, <tt>Foo2</tt> and <tt>Foo3</tt> are all in fact the same object, however Node will reimport the module when it is included via a relative path in <tt>require.paths</tt>. <em>If you use an absolute path in <tt>require.paths</tt>, this is not an issue.</em> To demonstrate this, consider the following complete example:</p>

<div class="wp_codebox"><table><tr id="p42614"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
</pre></td><td class="code" id="p426code14"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/**
 * foobar.js
 */</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'&gt;&gt;&gt; foobar module loaded'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
exports.<span style="color: #660066;">Foo</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #006600; font-style: italic;">/**
 * test.js
 */</span>
require.<span style="color: #660066;">paths</span>.<span style="color: #660066;">unshift</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">'.'</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> paths <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
    <span style="color: #3366CC;">'./foobar'</span><span style="color: #339933;">,</span>
    __dirname <span style="color: #339933;">+</span> <span style="color: #3366CC;">'/foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'././foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'../module-oddity/foobar'</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">'foobar'</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> funcs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    objs  <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    i     <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// load module and instantiate objects</span>
paths.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> path <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    funcs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span> path <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">Foo</span><span style="color: #339933;">;</span>
    objs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span>  <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> funcs<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Foo%d = %s&quot;</span><span style="color: #339933;">,</span> i<span style="color: #339933;">,</span> path <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    i<span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// compare objects</span>
<span style="color: #003366; font-weight: bold;">var</span> foo_i <span style="color: #339933;">=</span> obj_i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
funcs.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> foo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    foo_i<span style="color: #339933;">++;</span>
    obj_i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
&nbsp;
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    objs.<span style="color: #660066;">forEach</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> obj <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;Foo%d instanceof obj%d: %s&quot;</span><span style="color: #339933;">,</span>
            foo_i<span style="color: #339933;">,</span>
            <span style="color: #339933;">++</span>obj_i<span style="color: #339933;">,</span>
            <span style="color: #009900;">&#40;</span> obj <span style="color: #000066; font-weight: bold;">instanceof</span> foo <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
$ node test.js<br />
>>> foobar module loaded<br />
Foo0 = ./foobar<br />
Foo1 = ./foobar<br />
Foo2 = ././foobar<br />
Foo3 = ../module-oddity/foobar<br />
>>> foobar module loaded<br />
Foo4 = foobar</p>
<p>Foo1 instanceof obj1: true<br />
Foo1 instanceof obj2: true<br />
Foo1 instanceof obj3: true<br />
Foo1 instanceof obj4: true<br />
Foo1 instanceof obj5: false</p>
<p>Foo2 instanceof obj1: true<br />
Foo2 instanceof obj2: true<br />
Foo2 instanceof obj3: true<br />
Foo2 instanceof obj4: true<br />
Foo2 instanceof obj5: false</p>
<p>Foo3 instanceof obj1: true<br />
Foo3 instanceof obj2: true<br />
Foo3 instanceof obj3: true<br />
Foo3 instanceof obj4: true<br />
Foo3 instanceof obj5: false</p>
<p>Foo4 instanceof obj1: true<br />
Foo4 instanceof obj2: true<br />
Foo4 instanceof obj3: true<br />
Foo4 instanceof obj4: true<br />
Foo4 instanceof obj5: false</p>
<p>Foo5 instanceof obj1: false<br />
Foo5 instanceof obj2: false<br />
Foo5 instanceof obj3: false<br />
Foo5 instanceof obj4: false<br />
Foo5 instanceof obj5: true
</p></blockquote>
<p>According to the above test, you&#8217;ll notice that every relative path is considered to be an instance of the same Function. However, the single Function loaded from a <acronym title="Uniform Resource Locator">URL</acronym> that must be resolved though <tt>require.paths</tt> is considered to be a different Function. Also notice that the module is re-imported. Be sure to keep your naming conventions consistent.</p>
<p>So, there are two solutions to this issue:</p>
<ul>
<li><strong>Use absolute paths.</strong> You can avoid this issue by using <tt>require( __dirname + '/path' )</tt> rather than <tt>require( './path' )</tt>.</li>
<li><strong>Access modules consistently.</strong> If you use <tt>./path</tt>, always use that path when accessing the module.</li>
</ul>
<p>Hopefully in future versions of Node, this will not be an issue (I&#8217;m looking forwarding to putting a strikethru throughout this entire post). Until then, hopefully this saves you a confusing game of cat and mouse.</p>
<p>A patch for this problem is <a href="https://github.com/mikegerwitz/node/commit/46e909ac98c827bd2d4097a853289bd5e6676eb9">available on github</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/node-js-modules-and-instanceof-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Performance: Array Iteration</title>
		<link>http://mikegerwitz.com/php-performance-array-iteration/</link>
		<comments>http://mikegerwitz.com/php-performance-array-iteration/#comments</comments>
		<pubDate>Sun, 28 Mar 2010 12:32:11 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP Performance]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=273</guid>
		<description><![CDATA[In PHP, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over [...]]]></description>
			<content:encoded><![CDATA[<p>In <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>, arrays are used for virtually everything (I&#8217;ll spare my rant on that), therefore it&#8217;s likely that you&#8217;re going to use them abundantly in your scripts. The issue we&#8217;re going to be focusing on here isn&#8217;t with the arrays themselves &#8211; it&#8217;s the simple fact that you&#8217;re probably going to have to iterate over them in order to get the data you want. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a wide range of methods for doing so, so which do we use?</p>
<p>First, let&#8217;s generate an array to work with. We&#8217;re going for quantity here, not size of data. Therefore, we&#8217;ll simply generate an array containing a list of integers. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> offers a quick and easy way to do this using the <a href="http://us.php.net/manual/en/function.array_fill.php"><tt>array_fill()</tt></a> function:</p>

<div class="wp_codebox"><table><tr id="p27328"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p273code28"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>We want to use the <tt>array_fill()</tt> function rather than <tt>range()</tt> here because we will be summing up the values in the examples below. Range will generate incremental values, leading to integer overflows and inconsistent results across various architectures.</p>
<p><strong>Note:</strong> This article assumes that the reader is running at least <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.0.0. If you are running anything else, you should understand that <a href=http://www.php.net/archive/2008.php#id2008-08-07-1">official support for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 ended back in 2008</a> and you should consider upgrading immediately.</p>
<h3><tt>for</tt> loop</h3>
<p>Those coming from a procedural background may be most familiar with the <a href="http://us2.php.net/manual/en/control-structures.for.php"><tt>for</tt> loop</a> as a means of iterating through an array. Since it is so common, let&#8217;s take a look at that first:</p>

<div class="wp_codebox"><table><tr id="p27329"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code" id="p273code29"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$total</span>      <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$i</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Let&#8217;s run that test and see what type of results we get:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.127712s
</p></blockquote>
<p>Ouch. The time that is returned from the above test will depend on the speed of your system. I do know that, on my system, that&#8217;s a lot of time to spend simply adding up elements of an array. Let&#8217;s see how we may be able to improve that a bit.</p>
<p>The first thing that may jump out is the use of the <tt>count</tt> method in the condition of the <tt>for</tt> clause:</p>
<blockquote><p><tt>for ( $i = 0; $i &lt; <b>count( $data )</b>; $i++ )</tt></p></blockquote>
<p>It may help to understand exactly how <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> executes the above statement. The assignment portion of the loop is executed once before any iterations begin. The step/count, which is the <tt>$i++</tt> part, is executed after each iteration. The test expression (middle) is executed before each iteration to determine if the step should be executed and if we should continue with the loop.</p>
<p>Alright, that&#8217;s pretty straightforward. But that is the issue. That <tt>count</tt> function is being executed <em>for every iteration</em>. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is not caching the result of that function. That gets pretty expensive considering that it would be calling the function no less than 100,000 times.</p>
<p>Let&#8217;s see what happens when we change it up a bit:</p>

<div class="wp_codebox"><table><tr id="p27330"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p273code30"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// ...</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> 0<span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#91;</span> <span style="color: #000088;">$i</span> <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ...</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
for loop time: 0.046609s
</p></blockquote>
<p>Wow &#8211; that is a huge improvement! We nearly tripled the speed of the iteration simply by having the conditional check a precalculated value, rather than calling the function with each iteration. That&#8217;s pretty good, right?</p>
<p>No; it&#8217;s still pretty slow. We can do much better.</p>
<h3><tt>while</tt> loop and <tt>current()</tt></h3>
<p>Some people may prefer to use a <a href="http://us2.php.net/manual/en/control-structures.while.php"><tt>while</tt> loop</a> in order to loop through the elements of an array. One of the most common ways of doing that is to use the <a href="http://us2.php.net/manual/en/function.current.php"><tt>current()</tt></a> function in conjunction with <a href="http://us2.php.net/manual/en/function.next.php"><tt>next()</tt></a>:</p>

<div class="wp_codebox"><table><tr id="p27331"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p273code31"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/current"><span style="color: #990000;">current</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
    <a href="http://www.php.net/next"><span style="color: #990000;">next</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So how does that fair speed wise. It certainly <em>looks</em> a little less complicated than the <tt>for</tt> loop&#8230;that means it&#8217;s faster, right?</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.175094s
</p></blockquote>
<p>Yikes. We took a step in the wrong direction. Let&#8217;s pretend we didn&#8217;t see this.</p>
<p><a id="foreach"></a></p>
<h3><tt>foreach</tt> loop</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 4 introduced the <a href="http://us2.php.net/manual/en/control-structures.foreach.php"><tt>foreach</tt> construct</a> which provides a much more simple means of iterating through an array. Surely if it was designed for arrays, it should be faster than <tt>for</tt>:</p>

<div class="wp_codebox"><table><tr id="p27332"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p273code32"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Notice that, in the above snippet, we are also avoiding the use of a temporary variable <tt>$i</tt> to store the current index. While <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is undoubtedly doing so internally, we&#8217;re talking about a difference between compiled and optimized C code versus interpreted, sluggish <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> code. So that alone will provide us with a bit of a benefit, however slight.</p>
<p>Let&#8217;s see the result:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.037076s
</p></blockquote>
<p>Not too bad&#8230;we managed to shave off nearly 1/4th the time of the <tt>for</tt> loop. But I&#8217;m not satisfied.</p>
<h3>The Problem</h3>
<p>What do all of the above have in common? They&#8217;re looping structures. Great, so what&#8217;s the issue? If you&#8217;re coming from a compiled, high-performance language like C, there is no issue. Let&#8217;s see how long it&#8217;d take to iterate through an array containing 100,000 elements in C:</p>

<div class="wp_codebox"><table><tr id="p27333"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
</pre></td><td class="code" id="p273code33"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;stdio.h&gt;</span>
<span style="color: #339933;">#include &lt;sys/time.h&gt;</span>
&nbsp;
<span style="color: #339933;">#define DATALEN 100000</span>
&nbsp;
<span style="color: #993333;">int</span> main<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span>            data<span style="color: #009900;">&#91;</span> DATALEN <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">register</span> <span style="color: #993333;">long</span>  i<span style="color: #339933;">;</span>  
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">long</span>  total <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> timeval time_start<span style="color: #339933;">,</span> time_end<span style="color: #339933;">;</span>
    <span style="color: #993333;">long</span>           elapsed<span style="color: #339933;">,</span> uelapsed<span style="color: #339933;">;</span> 
    <span style="color: #993333;">double</span>         elapsed_total<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// create our data array</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> data<span style="color: #009900;">&#91;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> 1 <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// get current time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_start<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
    <span style="color: #666666; font-style: italic;">// loop through the array summing up the values</span>
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> i <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> DATALEN<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>   
        total <span style="color: #339933;">+=</span> data<span style="color: #009900;">&#91;</span> i <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>   
&nbsp;
    <span style="color: #666666; font-style: italic;">// get total running time</span>
    gettimeofday<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>time_end<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed       <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_sec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_sec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    uelapsed      <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> time_end.<span style="color: #202020;">tv_usec</span> <span style="color: #339933;">-</span> time_start.<span style="color: #202020;">tv_usec</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    elapsed_total <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> elapsed <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span> uelapsed <span style="color: #009900;">&#41;</span> <span style="color: #339933;">/</span> 1000000 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// output total and loop time</span>
    <a href="http://www.opengroup.org/onlinepubs/009695399/functions/printf.html"><span style="color: #000066;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #ff0000;">&quot;Total: %ld<span style="color: #000099; font-weight: bold;">\n</span>loop time: %fs<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        total<span style="color: #339933;">,</span> elapsed_total
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.000273s
</p></blockquote>
<p>Well, then. The point here is &#8211; <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> is a slow language, <em>especially</em> for looping. Wherever possible, you want to avoid it.</p>
<p>But you need to iterate through your arrays somehow! Does that mean you are stuck with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s slow loops?</p>
<p><a id="builtin"></a></p>
<h3>Use Built-In Functions</h3>
<p>Fortunately, there&#8217;s certain functions available to you that can drastically improve the speed of array processing. In fact, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> has a whole slew of <a href="http://us2.php.net/manual/en/ref.array.php">functions devoted to processing and altering arrays</a>.</p>
<p>Programmers coming form other languages such as C may prefer to write their own loops for processing data, because it is faster than a function call. This is not true at all with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; in fact, much of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s power comes from it&#8217;s built-in functions. <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> itself and its libraries are written in C. Therefore, if you can use a built-in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that will do the looping for you, that loop will be executed much more quickly.</p>
<p>Let&#8217;s take another look at our above example &#8211; summing up 100,000 values of an array. Is there a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> function that could do this for us? <a href="http://us2.php.net/manual/en/function.array-sum.php"><tt>array_sum()</tt></a> perhaps?</p>

<div class="wp_codebox"><table><tr id="p27334"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p273code34"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// sum up the values in the array</span>
<span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_sum"><span style="color: #990000;">array_sum</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Total: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$total</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Above, we are making a single function call to <tt>array_sum()</tt> rather than going in a loop and manually adding everything up. Let&#8217;s see how this method does:</p>
<blockquote><p>
Total: 100000<br />
for loop time: 0.012571s
</p></blockquote>
<p>Not bad. Using everything discussed here, we&#8217;ve cut our time down from the original 0.127712s to a mere 0.012571s. It&#8217;s still no C code, but our loop is <em>10 times faster</em> than it was originally. It&#8217;s still written in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>; we&#8217;re just using the tools we were given.</p>
<p>But wait &#8211; I said that <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s libraries were written in C. Why isn&#8217;t <tt>array_sum()</tt> just as fast as the C example provided above? Unfortunately, we&#8217;re still dealing with <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s variables and other internal representations here. It&#8217;s not just jumping to a position in memory like it does with an array in C. The <tt>array_sum()</tt> function still has to scan the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> array to retrieve all the values. There&#8217;s no getting away from that.</p>
<h3>Expense of Function Calls</h3>
<p>One must also be mindful of <em>what</em> their loop is doing. Take a look at the following example. It calls a function in order to count all the values in an array that contain the number 2.</p>

<div class="wp_codebox"><table><tr id="p27335"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code" id="p273code35"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
        ? 1
        <span style="color: #339933;">:</span> 0
    <span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// generate an array with 1/3 values containing 1, the rest containing 2</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>
    <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 10000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span>
    <span style="color: #339933;">+</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 10000<span style="color: #339933;">,</span> 20000<span style="color: #339933;">,</span> 2 <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$time_start</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$count</span> <span style="color: #339933;">+=</span> check_value<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// calculate total running time and output the result</span>
<span style="color: #000088;">$time_end</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span> <a href="http://www.php.net/microtime"><span style="color: #990000;">microtime</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$time_start</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Count: <span style="color: #009933; font-weight: bold;">%d</span><span style="color: #000099; font-weight: bold;">\n</span>for loop time: <span style="color: #009933; font-weight: bold;">%f</span>s<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$count</span><span style="color: #339933;">,</span> <span style="color: #000088;">$time_end</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Count: 20000<br />
for loop time: 0.043597s
</p></blockquote>
<p>Above, we called the <tt>check_value()</tt> function 30,000 times. Functions do an excellent job at improving code clarity and reducing duplicate code. However, function and method calls are expensive. Consider what happens when we get rid of the function and do all the processing in the body of the loop:</p>

<div class="wp_codebox"><table><tr id="p27336"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code" id="p273code36"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$val</span> <span style="color: #339933;">===</span> 2 <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$count</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If we run it again using that loop instead of the previous one, we get the following result:</p>
<blockquote><p>
Count: 20000<br />
for loop time: 0.013397s
</p></blockquote>
<p>That&#8217;s a significant speed improvement. Now, I&#8217;m not advocating that you get rid of all of your functions (though I would advocate preprocessor macros in <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>). However, consider the performance implications of excessive function or method calls where it is important. In most situations, the benefits you gain from legibility will outweigh the performance pitfalls.</p>
<p>It should also be noted that under many circumstances, the overhead of the function call may be insignificant. If your method takes 0.3 seconds to execute, you&#8217;re not going to care about milliseconds of overhead.</p>
<h3>Memory Tradeoff &#038; Apparent Processing</h3>
<p>Let&#8217;s continue with the example from the previous section (counting the number of elements in an array that contain the value <tt>2</tt>). <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> does not contain a function that can explicitly search an array for the given value and return the number of results. It does, however, have something very similar &#8211; <a href="http://us.php.net/manual/en/function.array-count-values.php"><tt>array_count_values()</tt></a>.</p>
<p>The <tt>array_count_values()</tt> function will loop through the array and maintain a count of each of the values. It will then return an array containing the results. Therefore, in our above example, it would return an array much like the following:</p>

<div class="wp_codebox"><table><tr id="p27337"><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code" id="p273code37"><pre class="php" style="font-family:monospace;"><a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span>
    <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 10,000 1s in our data array</span>
    <span style="color: #cc66cc;">2</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20000</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">// there are 20,000 2s in our data array</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>That&#8217;s a bit more information than we want. In fact, what if we had an array containing hundreds of different values? Wouldn&#8217;t it take longer to count every element than a single one (well, no, but one might assume)? That would account for a bit more memory usage as well, even though it does give us the answer we are looking for. Which is a more elegant solution?</p>
<p>When attempting to optimize code, developers must often have to choose between CPU time and memory footprint. This is one of those situations due to language limitations. Let&#8217;s take a look at how much time it takes for <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> to count the values for us:</p>

<div class="wp_codebox"><table><tr id="p27338"><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code" id="p273code38"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// count the values in the array greater than 50000</span>
<span style="color: #000088;">$count_data</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_count_values"><span style="color: #990000;">array_count_values</span></a><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$data</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$count</span>      <span style="color: #339933;">=</span> <span style="color: #000088;">$count_data</span><span style="color: #009900;">&#91;</span> 2 <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// output memory consumption</span>
<a href="http://www.php.net/printf"><span style="color: #990000;">printf</span></a><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Memory Consumption: <span style="color: #009933; font-weight: bold;">%d</span> bytes<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <a href="http://www.php.net/memory_get_usage"><span style="color: #990000;">memory_get_usage</span></a><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Memory Consumption: 6815744 bytes<br />
Count: 20000<br />
for loop time: 0.003805s
</p></blockquote>
<p>Even though we&#8217;re gathering more information, it&#8217;s still 3.5x faster than counting only a single value manually. In fact, I&#8217;m willing to bet that it even consumed less memory than our previous example, simply due to the fact that we aren&#8217;t having to deal with userland variables and function calls:</p>
<blockquote><p>
(Our example using check_value(), with memory output)</p>
<p>Memory Consumption: 7077888 bytes<br />
Count: 20000<br />
for loop time: 0.043128s
</p></blockquote>
<p>It seems that counting a single element using our <tt>check_value()</tt> method used <tt>262144</tt> more bytes than <tt>array_count_values()</tt>! Damn you, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>! Yes, it&#8217;s unfortunate that considerations like this must be made, but you&#8217;d be surprised how many internal methods really do provide both strong memory and CPU benefits even though they &#8220;do more work&#8221;. Userland consumes considerable amounts of memory.</p>
<p>That isn&#8217;t to say that <tt>array_count_values()</tt> can&#8217;t consume more memory. Like I said previously &#8211; if it returns an array with thousands of values, it&#8217;s going to offset that memory savings.</p>
<h3>ArrayObjects and Iterators</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 began a strong move toward Object-Oriented programming. It provides a number of powerful components that deal with iteration &#8211; specifically <a href="http://us.php.net/manual/en/class.traversable.php"><tt>Traversable</tt></a> objects and <a href="http://us.php.net/manual/en/class.iterator.php"><tt>Iterators</tt></a>. They provide a benefit to developers in the sense that it makes code much easier to understand and allows for powerful OO implementations, but does it provide any performance benefit?</p>
<p>Firstly, let&#8217;s see what happens when we loop through an <a href="http://us.php.net/manual/en/class.arrayobject.php"><tt>ArrayObject</tt></a> rather than a normal <tt>array</tt>:</p>

<div class="wp_codebox"><table><tr id="p27339"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p273code39"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// generate an array containing 100,000 elements, each with the value of 1</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayObject<span style="color: #009900;">&#40;</span> <a href="http://www.php.net/array_fill"><span style="color: #990000;">array_fill</span></a><span style="color: #009900;">&#40;</span> 0<span style="color: #339933;">,</span> 100000<span style="color: #339933;">,</span> 1 <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.061663s
</p></blockquote>
<p>This is compared to our <tt>foreach</tt> example with a result of <tt>0.037076s</tt>. So we did loose a bit of time there. However, that does not mean you shouldn&#8217;t use <tt>ArrayObject</tt>s. In fact, I use them a great deal in OO scripts, because <tt>ArrayObject</tt>s coupled with <tt>Iterator</tt>s provide powerful designs that aren&#8217;t quite as elegant when using procedural code. However, when performance is a requirement, I have to steer clear of them.</p>
<p>You will find that the <tt>Iterator</tt> object provides very little benefit over a conventional <tt>foreach</tt> loop as well. The only time I make use of an <tt>Iterator</tt> is when it contains additional logic that determines what results it should return (e.g. to iterate only over odd numbers), or if the implementation allows for an <tt>Iterator</tt> to be passed in at runtime to determine what results should be returned.</p>

<div class="wp_codebox"><table><tr id="p27340"><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code" id="p273code40"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// loop through each of the array items and sum them up</span>
<span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getIterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$total</span> <span style="color: #339933;">+=</span> <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<blockquote><p>
Total: 100000<br />
loop time: 0.291542s
</p></blockquote>
<p>In fact, that&#8217;s our slowest method yet. So, just be wise when weighing the costs and benefits. If performance doesn&#8217;t matter &#8211; go ahead. Use whatever implementation is best for you. That&#8217;s why you have the option. If performance <em>is</em> a factor, don&#8217;t even glance at those objects. Maybe future versions of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> will enhance the functionality to the point where their speeds are equivalent to the loop structures.</p>
<h3>Wall Of Text Summary</h3>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> provides a number of ways to iterate through arrays &#8211; rest assured that we haven&#8217;t nearly covered them all here. Out of all the loop structures available, <a href="#foreach"><tt>foreach</tt></a> is the fastest for iteration. However, it comes up short when compared to <a href="#builtin">built-in functions</a> designed for array processing. Where possible, those built-in functions should be used, even where it may seem at first glance that it may be slower. They often provide a very strong performance benefit &#8211; both CPU and memory wise.</p>
<p><acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5 introduced a number of new means of iteration through the use of objects. Certain implementations may do well to improve code legibility, and may enable the developer to implement some fairly powerful object-oriented designs, but when performance is a factor, you want to steer clear of them.</p>
<p>Hopefully this article helped to clear up some questions. I expect not to see any more <tt>for</tt> loops with <tt>count()</tt> in the loop test expression!</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/php-performance-array-iteration/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP Extension Writing: Namespaces</title>
		<link>http://mikegerwitz.com/php-extension-writing-namespaces/</link>
		<comments>http://mikegerwitz.com/php-extension-writing-namespaces/#comments</comments>
		<pubDate>Sun, 21 Jun 2009 08:53:47 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/?p=97</guid>
		<description><![CDATA[I&#8217;m fairly new to PHP extension writing. I&#8217;ve just gotten into it in order to optimize some existing PHP 5.3 code for my WebKernel project. However, there was one fundamental problem that I could not find the solution to anywhere online &#8211; how do you define a function within a namespace!? WebKernel used namespaces extensively, [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m fairly new to <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> extension writing. I&#8217;ve just gotten into it in order to optimize some existing <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.3 code for my WebKernel project. However, there was one fundamental problem that I could not find the solution to anywhere online &#8211; <em>how do you define a function within a namespace!? </em>WebKernel used namespaces extensively, so I needed to know the solution to the problem.</p>
<p>Namespaces are new as of <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 5.3. Therefore, there&#8217;s very few people actually writing extensions with namespace support. To make matters worse, documentation of the Zend core is very poor. Much of my time is spent reading through the code, trying to figure out what macros do what, etc. This was another one of those cases.</p>
<h1>Normal <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> Functions:</h1>
<p>Normal <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> functions are placed within the functions list using the <tt>PHP_FE</tt> macro. The actual function is defined using <tt>PHP_FUNCTION</tt>. So your code may look like this:</p>

<div class="wp_codebox"><table><tr id="p9748"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code" id="p97code48"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> function_entry my_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
PHP_FE<span style="color: #009900;">&#40;</span> myfunc<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
PHP_FUNCTION<span style="color: #009900;">&#40;</span> myfunc <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Function body here</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If you were to call <tt>myfunc()</tt> from within userland (within a <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> script), it would execute the C code defined within the body of <tt>PHP_FUNCTION( myfunc )</tt>. Simple enough. Now what if we wanted namespaces?</p>
<h1>Using Namespaces</h1>
<p><strong>The short answer:</strong><br />
Replace <tt>PHP_FE( myfunc, NULL )</tt> with <tt>ZEND_NS_FE( "myns", myfunc, NULL )</tt>. But if you actually want to know why, keep reading.</p>
<p><strong>The detailed answer:</strong><br />
<tt>PHP_FE</tt> is defined within <tt>main/php.h</tt>. That points us to the <tt>ZEND_FE</tt> macro defined within <tt>Zend/zend_API.h</tt>. While looking through this file, I noticed a <tt>ZEND_NS_FE</tt> macro in there, and figured it must be referring to namespaces. So I figured that logically, there would be a <tt>PHP_NS_FE</tt> macro, right? Nope. There&#8217;s no such macro defined anywhere. At least not at this point in time. Hopefully there will be for the final release of 5.3.<br />
 yourself, however that will ruin your chances of your extension becoming portable. Other developers aren&#8217;t going to want to patch their php.h file to use it.</p>

<div class="wp_codebox"><table><tr id="p9749"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p97code49"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_FE(name, arg_info)						ZEND_FENTRY(name, ZEND_FN(name), arg_info, 0)</span>
<span style="color: #339933;">#define ZEND_NS_FE(ns, name, arg_info)					ZEND_NS_FENTRY(ns, name, ZEND_FN(name), arg_info, 0)</span></pre></td></tr></table></div>

<p>Looking over the macros, you see that both macros make use of the <tt>ZEND_FN</tt> macro. A quick glance at that macro shows that it converts our &#8220;myfunc&#8221; function into &#8220;zif_myfunc&#8221;:</p>

<div class="wp_codebox"><table><tr id="p9750"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p97code50"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_FN(name) zif_##name</span></pre></td></tr></table></div>

<p>This means that internally, zend will use the same function name regardless of whether or not it is contained within a namespace. In other words, we don&#8217;t even have to touch our <tt>PHP_FUNCTION</tt> line! Great &#8211; so then what&#8217;s different? Let&#8217;s take a look at <tt>ZEND_NS_FENTRY</tt>:</p>

<div class="wp_codebox"><table><tr id="p9751"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p97code51"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_NS_FENTRY(ns, zend_name, name, arg_info, flags)		ZEND_RAW_FENTRY(ZEND_NS_NAME(ns, #zend_name), name, arg_info, flags)</span></pre></td></tr></table></div>

<p>The difference here is that Zend is making use of the <tt>ZEND_NS_NAME</tt> macro:</p>

<div class="wp_codebox"><table><tr id="p9752"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p97code52"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define ZEND_NS_NAME(ns, name)			ns&quot;\\&quot;name</span></pre></td></tr></table></div>

<p>Ah-ha! (Ignoring the fact that it&#8217;s, at present, at the top of the file.) While internally our function name is no different, Zend will only call it if it is prefixed with the namespace name from within userland. The above macro converts our function name to &#8220;myns\myfunc&#8221;. So, our updated code may look like this:</p>

<div class="wp_codebox"><table><tr id="p9753"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code" id="p97code53"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define MY_NS &quot;myns&quot;&lt;/code&gt;</span>
&nbsp;
<span style="color: #993333;">static</span> function_entry my_functions<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
ZEND_NS_FE<span style="color: #009900;">&#40;</span> MY_NS<span style="color: #339933;">,</span> myfunc<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span>
<span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
PHP_FUNCTION<span style="color: #009900;">&#40;</span> myfunc <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// Function body here</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>We can then call our function via &#8220;myns\myfunc&#8221;. Remember &#8211; you must use <tt>ZEND_NS_FE</tt>. As of this time of writing, <tt>PHP_NS_FE</tt> does not exist! Of course, that could easily be solved by adding it to your own source files yourself, but it&#8217;s still a messy task. I&#8217;ve submitted a bug report to the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> team detailing the additions (I couldn&#8217;t find where to submit a patch), so hopefully it&#8217;ll make it in. Until then, feel free to add this to your header file, along with any other <tt>ZEND_NS_*</tt> aliases you may need:</p>

<div class="wp_codebox"><table><tr id="p9754"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p97code54"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#ifndef PHP_NS_FE</span>
<span style="color: #339933;">#    define PHP_NS_FE ZEND_NS_FE</span>
<span style="color: #339933;">#endif</span></pre></td></tr></table></div>

<h1>Summary</h1>
<p>While diving into the C code isn&#8217;t necessary to use namespaces, it does greatly improve your understanding of how the system works. Because there is so little documentation, I encourage you to keep looking through <acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s source and see what you find. There are goodies hidden everywhere that may be incredibly useful to you, and save you a lot of time. Look through existing extensions within the <tt>ext</tt> folder and see if any of them use methods that you are interested in.</p>
<p>It was the unfortunate circumstance that none of the extensions did make use of namespace support. So I was on my own to look for it. I hope this helps save some frustration for others, especially those new to Zend that are just trying to figure out whether or not porting their code to C is worth the effort.</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/php-extension-writing-namespaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP6 Snapshot &#8211; Namespace Separator Patch Applied</title>
		<link>http://mikegerwitz.com/php6-snapshot-namespace-separator-patch-applied/</link>
		<comments>http://mikegerwitz.com/php6-snapshot-namespace-separator-patch-applied/#comments</comments>
		<pubDate>Thu, 18 Dec 2008 10:59:16 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/2008/12/18/php6-snapshot-namespace-separator-patch-applied/</guid>
		<description><![CDATA[In my MyCustomBB Blog a while back, I discussed the PHP&#8217;s decision to change the namespace separator to a backslash. Until recently, this patch hadn&#8217;t yet been applied. Therefore, I continued developing my code using the old static operator (::). Well, I recently installed the new PHP 6 snapshot from http://snaps.php.net/ and found that my [...]]]></description>
			<content:encoded><![CDATA[<p>In my MyCustomBB Blog a while back, I discussed the <a href="http://dev.mycustombb.com/blog/2008/10/27/namespaces-core-design-separator-change/"><acronym title="PHP: Hypertext Preprocessor">PHP</acronym>&#8217;s decision to change the namespace separator to a backslash</a>. Until recently, this patch hadn&#8217;t yet been applied. Therefore, I continued developing my code using the old static operator (::).</p>
<p>Well, I recently installed the new <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> 6 snapshot from <a href="http://snaps.php.net/">http://snaps.php.net/ </a>and found that my code was broken &#8211; a pleasent surprise I&#8217;m sure to many <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> developers. Just think about the ones who don&#8217;t even know about the change. Imagine that frustration. Anyway, I quickly realized that the separator had changed. Thankfully, it&#8217;s pretty much a simple search-and-replace to modify my namespaces to use the new separator using the sed utility. Be sure to back up your data before performing any batch replacing on your scripts.</p>
<p>I suppose I could help out by providing a script to help you make that change. In my situation, all I have to do is replace &#8220;webkernel::&#8221; with &#8220;webkernel\&#8221;, and &#8220;cli::&#8221; with, &#8220;cli\&#8221;. Simply paste the below command into a <acronym title="Bourne Again Shell">BASH</acronym> terminal within the root directory of your project, and it will recursively replace all occurances in all <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> files. Again, <em>BACK UP YOUR WORK!</em> I&#8217;m not to be held responsible for any irreversible damage done to your code. Be sure you understand the command before running it.</p>

<div class="wp_codebox"><table><tr id="p4356"><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code" id="p43code56"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/bash</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$#</span> <span style="color: #660033;">-lt</span> 2 <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> Usage: rsed <span style="color: #ff0000;">&quot;FILEPATTERN&quot;</span> <span style="color: #ff0000;">&quot;REGEXP&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">exit</span> 1
<span style="color: #000000; font-weight: bold;">fi</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">find</span> . <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;$1&quot;</span><span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #666666; font-style: italic;"># Make a backup</span>
    <span style="color: #c20cb9; font-weight: bold;">mv</span> <span style="color: #007800;">$i</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Perform the replacement</span>
    <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;$2&quot;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span> <span style="color: #000000; font-weight: bold;">&amp;</span>gt; <span style="color: #007800;">$i</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span>
&nbsp;
    <span style="color: #666666; font-style: italic;"># Remove the backup - comment out to keep backup</span>
     <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span>.bak&quot;</span>
<span style="color: #000000; font-weight: bold;">done</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">echo</span> Done.</pre></td></tr></table></div>

<p>Save the above as <tt>rsed</tt>, chmod +x, and run it using the following as an example: <tt>./rsed "*.php" "s/webkernel::/webkernel\\\\g/"</tt> (in the previous command, &#8220;\\\\&#8221; = &#8220;\&#8221;). Ensure you use an extension like &#8220;*.php&#8221; for the file pattern &#8211; don&#8217;t just use &#8220;*&#8221; or it&#8217;ll cause problems, as it&#8217;ll match directories as well. If you have files that contain no extension, replace them manually.</p>
<p>Alternatively, in the above script, you may instead decide to use <tt>grep -rl "webkernel::" ./</tt> to modify any file that contains the phrase you&#8217;re trying to replace, rather than search for only <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> files (this was more flexible in my case).</p>
<p><strong>NOTE:</strong> Be sure to check for strings containing the replaced expression in your code. The replacement may end up escaping part of the string, causing errors (e.g. &#8216;webkernel::&#8217; to &#8216;webkernel\&#8217;). Good luck!</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/php6-snapshot-namespace-separator-patch-applied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Stream Wrapper Irritation</title>
		<link>http://mikegerwitz.com/php-stream-wrapper-irritation/</link>
		<comments>http://mikegerwitz.com/php-stream-wrapper-irritation/#comments</comments>
		<pubDate>Thu, 01 May 2008 03:45:42 +0000</pubDate>
		<dc:creator>Mike Gerwitz</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.mikegerwitz.com/2008/05/01/php-stream-wrapper-irritation/</guid>
		<description><![CDATA[Well; after several wasted hours looking into what I believed to be a bug within the PHP core, I have figured it out. I want to post this in the hope that you, the reader, do not have to go through the stress I went through in attempting to resolve this issue (and almost restorting [...]]]></description>
			<content:encoded><![CDATA[<p>Well; after several wasted hours looking into what I believed to be a bug within the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> core, I have figured it out. I want to post this in the hope that you, the reader, do not have to go through the stress I went through in attempting to resolve this issue (and almost restorting to a standards-defiant workaround).</p>
<p>I speak of creating a <a href="http://us.php.net/manual/en/function.stream-wrapper-register.php">stream wrapper</a> (I&#8217;m creating the datapack wrapper for the <a href="http://www.mycustombb.com/">MyCustomBB</a> project). When you call <a href="http://us.php.net/manual/en/function.ftell.php">ftell()</a> on the handle to the stream, the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> core calls the stream_tell() function within the class defined for that wrapper (in this case, datapack::stream_tell). However, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> <em>only</em> seems to call this function after you use fseek() on your stream. Until then, it caches the file pointer position itself. Okay, seems reasonable. However, unless your stream_seek() function returns a value of TRUE (or any non-false value), <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> ignores the fact that the function was called, and continues to cache it itself.</p>
<p>Okay, so what&#8217;s the problem? If you check the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> documentation for the <a href="http://us.php.net/manual/en/function.fseek.php">fseek()</a> function, you will see the following quote for the return value:</p>
<p><quote>Upon success, returns 0; otherwise, returns -1. Note that seeking past <acronym title="End of file">EOF</acronym> is not considered an error.</quote></p>
<p>Okay; to be complaint with this standard, my stream_seek() function returned a value of zero upon success, and -1 upon failure. Rather, it simply returned the value that was returned by fseek() (<tt>return fseek( $this-&gt;handle, $seek_pos);</tt>). However, 0 = FALSE. Therefore, <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> ignored it and would not call stream_tell() until I had stream_seek() return a value of TRUE upon success:</p>
<p><code>return ( fseek( $this-&gt;handle, $seek_pos ) == -1 ) ? FALSE : TRUE;</code></p>
<p>Then, it worked. You can imagine my irritation. However, in the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> documentation, which I did overlook, it states that stream_seek() should indeed return TRUE on success. My bad, but very stupid of the <acronym title="PHP: Hypertext Preprocessor">PHP</acronym> developers to do.</p>
<p>And just to think; I almost had to return the position via fseek() and use <tt>fseek( $handle, 0, SEEK_CUR )</tt> in place of ftell().</p>]]></content:encoded>
			<wfw:commentRss>http://mikegerwitz.com/php-stream-wrapper-irritation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

