<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: SPL Talk at PHP London</title>
	<atom:link href="http://blog.soal.org/index.php/2009/05/spl-talk-at-php-london/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.soal.org/index.php/2009/05/spl-talk-at-php-london/</link>
	<description>None of this is true. The rest is.</description>
	<lastBuildDate>Sat, 23 Apr 2011 16:05:06 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
	<item>
		<title>By: Robin</title>
		<link>http://blog.soal.org/index.php/2009/05/spl-talk-at-php-london/comment-page-1/#comment-37</link>
		<dc:creator>Robin</dc:creator>
		<pubDate>Sat, 06 Jun 2009 20:00:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.soal.org/?p=61#comment-37</guid>
		<description>&lt;a href=&quot;#comment-35&quot; rel=&quot;nofollow&quot;&gt;@Phil&lt;/a&gt; 
Hi Phil,

SPLFixedArray implements the &lt;a href=&quot;http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html&quot; rel=&quot;nofollow&quot;&gt;ArrayAccess interface&lt;/a&gt;, which provides the various offset*() methods. 

When the engine encounters an indexed access (using the [] notation) on an object, it will check to see whether that object implements ArrayAccess. If so, it will invoke the appropriate offset*() method. If not, you get an error like &quot;Fatal error: Cannot use object of type C as array&quot;.

In other words, it&#039;s by implementing ArrayAccess that a class can provide custom behaviour when its instances are accessed with []. Therefore, any class which does this must provide an implementation of the offset*() methods - SPLFixedArray included.

&lt;br/&gt;
&lt;br/&gt;
You performance observation is really interesting. My guess is that it highlights the overhead of PHP method lookup &amp; invocation:
&lt;br/&gt;
- When an indexed read ([]) is encountered on an instance of a user-space class which implements ArrayAccess, the engine just looks up the offsetget() method and executes it. So there should be very little difference between reading with [] and offsetget().
&lt;br/&gt;
- However, extensions classes like SPLFixedArray (implemented in C) can bypass this behaviour. The [] access can lead to a direct invocation of a C function which does the read - no need to lookup and invoke the PHP-space method... hence the performance improvement when using [] instead of calling offsetGet() explicitly on an an SPLFixedArray.
&lt;br/&gt;
&lt;br/&gt;
For the details of how SPLFixedArray achieves this, see &lt;a href=&quot;http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c&quot; rel=&quot;nofollow&quot;&gt;spl_fixedarray.c&lt;/a&gt;:
&lt;br/&gt;
- &lt;a href=&quot;http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c#1063&quot; rel=&quot;nofollow&quot;&gt;In the class setup&lt;/a&gt;, we see that the standard read_dimension object handler (which would normally have done the slow lookup &amp; invocation of offsetGet()) is replaced by spl_fixedarray_object_read_dimension.
&lt;br/&gt;
- &lt;a href=&quot;http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c#344&quot; rel=&quot;nofollow&quot;&gt;spl_fixedarray_object_read_dimension&lt;/a&gt; calls back into PHP-space &lt;b&gt;only&lt;/b&gt; if we&#039;re in a subclass of SPLFixedArray and offsetGet() has been overridden. Otherwise, it delegates to spl_fixedarray_object_read_dimension_helper. This is the fast path - no lookups, no call back into PHP-space.
&lt;br/&gt;
&lt;br/&gt;
I&#039;m deducing this from looking at the code, so I may well be wrong... could be worth profiling to confirm. But method invocation is known to be quite expensive in PHP. I think there was some discussion on the internals list about improving this by caching invocation targets when the receiver type is sure not to change - I can&#039;t find a reference right now but will let you know if I do.</description>
		<content:encoded><![CDATA[<p><a href="#comment-35" rel="nofollow">@Phil</a><br />
Hi Phil,</p>
<p>SPLFixedArray implements the <a href="http://www.php.net/~helly/php/ext/spl/interfaceArrayAccess.html" rel="nofollow">ArrayAccess interface</a>, which provides the various offset*() methods. </p>
<p>When the engine encounters an indexed access (using the [] notation) on an object, it will check to see whether that object implements ArrayAccess. If so, it will invoke the appropriate offset*() method. If not, you get an error like &#8220;Fatal error: Cannot use object of type C as array&#8221;.</p>
<p>In other words, it&#8217;s by implementing ArrayAccess that a class can provide custom behaviour when its instances are accessed with []. Therefore, any class which does this must provide an implementation of the offset*() methods &#8211; SPLFixedArray included.</p>
<p>You performance observation is really interesting. My guess is that it highlights the overhead of PHP method lookup &#038; invocation:<br />
<br />
- When an indexed read ([]) is encountered on an instance of a user-space class which implements ArrayAccess, the engine just looks up the offsetget() method and executes it. So there should be very little difference between reading with [] and offsetget().<br />
<br />
- However, extensions classes like SPLFixedArray (implemented in C) can bypass this behaviour. The [] access can lead to a direct invocation of a C function which does the read &#8211; no need to lookup and invoke the PHP-space method&#8230; hence the performance improvement when using [] instead of calling offsetGet() explicitly on an an SPLFixedArray.</p>
<p>For the details of how SPLFixedArray achieves this, see <a href="http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c" rel="nofollow">spl_fixedarray.c</a>:<br />
<br />
- <a href="http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c#1063" rel="nofollow">In the class setup</a>, we see that the standard read_dimension object handler (which would normally have done the slow lookup &#038; invocation of offsetGet()) is replaced by spl_fixedarray_object_read_dimension.<br />
<br />
- <a href="http://lxr.php.net/source/php-src/ext/spl/spl_fixedarray.c#344" rel="nofollow">spl_fixedarray_object_read_dimension</a> calls back into PHP-space <b>only</b> if we&#8217;re in a subclass of SPLFixedArray and offsetGet() has been overridden. Otherwise, it delegates to spl_fixedarray_object_read_dimension_helper. This is the fast path &#8211; no lookups, no call back into PHP-space.</p>
<p>I&#8217;m deducing this from looking at the code, so I may well be wrong&#8230; could be worth profiling to confirm. But method invocation is known to be quite expensive in PHP. I think there was some discussion on the internals list about improving this by caching invocation targets when the receiver type is sure not to change &#8211; I can&#8217;t find a reference right now but will let you know if I do.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Phil</title>
		<link>http://blog.soal.org/index.php/2009/05/spl-talk-at-php-london/comment-page-1/#comment-35</link>
		<dc:creator>Phil</dc:creator>
		<pubDate>Fri, 05 Jun 2009 20:21:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.soal.org/?p=61#comment-35</guid>
		<description>Thanks for the great talk on the SPL class - Whats the point of the offsetGet function on the SPLFixedArray class? I benchmarked the [] and the offsetGet function and found it to be 2.5x slower than accessing the array via [].</description>
		<content:encoded><![CDATA[<p>Thanks for the great talk on the SPL class &#8211; Whats the point of the offsetGet function on the SPLFixedArray class? I benchmarked the [] and the offsetGet function and found it to be 2.5x slower than accessing the array via [].</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Benchmarking SPLFixedArray access speed &#171; PHP Programming</title>
		<link>http://blog.soal.org/index.php/2009/05/spl-talk-at-php-london/comment-page-1/#comment-34</link>
		<dc:creator>Benchmarking SPLFixedArray access speed &#171; PHP Programming</dc:creator>
		<pubDate>Fri, 05 Jun 2009 20:17:57 +0000</pubDate>
		<guid isPermaLink="false">http://blog.soal.org/?p=61#comment-34</guid>
		<description>[...] a talk at PHPLondon on the SPL library last month I decided to play about with SPL. I was curious as to the difference in speed between [...]</description>
		<content:encoded><![CDATA[<p>[...] a talk at PHPLondon on the SPL library last month I decided to play about with SPL. I was curious as to the difference in speed between [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

