tekx – new spl features in php 5.3

Image of Matthew Turland from Twitter
Image of Matthew Turland

Matthew Turland is presenting. If you haven’t heard of him, you’re a nub (k, not really but I really wanted to put that in a post somewhere). He’s an auther for php|architect and author of Web Scrapign with PHP, a contributer to Zend Framework, lead developer of Phergie. So yeah, kind of a big deal. He currently works at Synacor which provides internet solutions to ISPs, media companies and advertisers.

The biggest change to SPL in 5.3 is the containers. Why containers? Arrays aren’t always great. The underlying hash table algorithm is not always ideal for the task at hand.

We’ll be looking at a lot of benchmarks. The code is available on github so you can compare the performance results for yourself.

Starting simple with a list. The corresponding structure for this is SplFixedArray. It is like an array but with fixed length. Only allows integers >= 0 for keys. As you have more elements, SPL overtakes arrays in being better for memory usage.

SplDoublyLinkedList

Same unlimited size as arrays without the associated hash map algorithm. There is less performance but better memory usage.

SplStack

Two very basic operations: push and pop. LIFO (Last In, First Out) data structure. The class doesn’t get better performance. There seems to be such an odd bottleneck. He’ll get to filing a bug later. ;p

SplQueue

Two operation: enqueue and dequeue. FIFO (First In, First Out) data structure. Again a correlation of element size to performance improvement.

SplHeap

Heaps operate based off of a comparison function. It internally reorders items based on the comparrison. Worst case is you can override SplHeap::compare(). Sorting happens as you insert the items. Again, smaller number of elements, you should stick with arrays but if you’re dealing with a large number of elements, you will see a performance difference.

SplPriorityQueue

Priority queues are very similar to triage. Similar to a heap, in fact it uses a heap internally for storage. Accepts a priority with the element value. Also compare can be overridden.

Sets

Similarity between doing queries with joins. Slide shows some crayons, wonder what the analogy is or maybe he likes crayons. I like crayons. Queries with UNION operation is a set operation. Hash table with objects for keys is the best visual for understanding composite hash map.  Set focuses on a group of values rather than individual values with operations like union, intersection, difference, etc. Using SplObjectStorage for the code examples.

Etienne Kneuss did a lot of the work on the new SPL features in PHP 5.3 so we can thank him for the new hotness. Etienne’s blog is a good resource for SPL as well as the SPL entries in the PHP manual. Elizabeth Smith has written up a post called “SPL to the Rescue” which is useful. Elazar will also put this presentation as a blog post on his site.

Future of SPL

  • Graphs
    • contains nodes and edges connecting them
    • directional/non-directional
    • cyclic/acyclic
    • connected/unconnected
    • edge costs
  • Trees
    • Acyclic unidirection graph
    • hierarchical in nature

Elazar did a great job of taking concepts that would normally go over my head and making them understandable.

Reblog this post [with Zemanta]

7 comments on “tekx – new spl features in php 5.3Add yours →

  1. Re: the crayon analogy, you generally buy crayons in a box (i.e. as a set), right? Maybe I need a box of them for a picture instead. Only other visual I could think of was a Venn diagram, which may not have been as intuitive to people. I'm open to suggestions for a better visual to explain sets, though. 🙂

    1. It makes sense. I think that the crayon thing was creative. Venn would be nice too. Personally I'd put twins or triplets. Either way, just putting the word Sets on that slide with the visual should be sufficient on bridging the visual connection. You might have been too clever there bro. =D

Leave a Reply to Matthew Turland Cancel reply