6 ms·
This is great news. PHP doesn't have many structured data types, so arrays (aka maps) are basically used for everything. Any improvement to them will impact the
by adunn 12y ago
This is great news. PHP doesn't have many structured data types, so arrays (aka maps) are basically used for everything. Any improvement to them will impact the entire application.
It would be nice to have separate types for arrays and maps though. I don't understand why they were combined to begin with. Simplicity? Seems like there are more edge cases and gotchas the way things are now.
- _RPM 12y ago> It would be nice to have separate types for arrays and hash tables though. I don't understand why they were combined to begin with. Simplicity? Seems like there are more edge cases and gotchas the way things are now. There is no "hash table" type in PHP user land.
- imaginenore 12y agoThere are no arrays in PHP. There are only hash tables that are called "array" for simplicity.
- colordrops 12y agoHow do they maintain their order?
- Retra 12y agoYou put things in order by memory address and they stay in order when you access them. An array is just a special case of a hash map -- one with a trivial hash function. That's why the lend themselves to the same syntax so well.
- _RPM 12y agoBy using a doubly linked list, where the first element in the "bucket", contains the next pointer to the next element in the hash table. Read zend_hash.h & zend_hash.c It is fairly complicated and explaining it in depth is beyond the scope of this comment. This "bucket" also handles the collisions by using separate chaining. There is actually two "next" pointers, one for the chains, and one for the next element in order of insertion. Very confusing and requires reading through the code and playing with it.
- patkai 12y agoYou made my day, guys, please don't stop :)
- jakobegger 12y agoThey aren't really hash tables either, because they additionally store the order of the keys (in a normal hash table, order would be arbitrary)
- adunn 12y agoYou're right, thanks for pointing that out.
- deleted 12y ago[deleted]
- inglor 12y agoPHP has a standard library with plenty of collections: http://php.net/manual/en/book.spl.php http://php.net/manual/en/book.spl.php Stack - http://php.net/manual/en/book.spl.php http://php.net/manual/en/book.spl.php Queue - http://php.net/manual/en/class.splqueue.php http://php.net/manual/en/class.splqueue.php PriorityQueue - http://php.net/manual/en/class.splpriorityqueue.php http://php.net/manual/en/class.splpriorityqueue.php Real Maps - http://php.net/manual/en/class.splobjectstorage.php http://php.net/manual/en/class.splobjectstorage.php It's a shame some people are not aware of these.
- fleetfox 12y agoNo primitive types: tuples, lists, sets. Having only arrays to work with and being weak typed results in a lot of headache.
- cranklin 12y agoThose aren't primitive types
- fleetfox 12y agoWhat are those called then? Wikipedia definition of primitive type is pretty vague. I don't think collections/containers can't be called primitive.
- debacle 12y agoCollections are generally not primitives unless they are implemented in the language as primitives.
- bitwalker 12y agoWhile I agree with you, Erlang for instance has collections as primitives (lists/tuples), but then offers more complex collections (such as gb_tree) as part of the stdlib.
- 12y ago
- aruggirello 12y ago> arrays (aka maps) are basically used for everything. This is interesting. In fact, I believe object properties share the same mechanism as associative arrays, that is, $a->b will actually lookup the hash of "b" in $a. Does this new hashtable layout influence object properties/methods too? That would be huge!
- juriansluiman 12y ago> Does this new hashtable layout influence object properties/methods too? Not sure, but I don't think so. Ppl often think object properties and arrays are much alike, but the array's HashTable struct and the object's store struct are very different. The main performance gains are not about the buckets (which are simple and quite alike) but the array's hashtable idea. Often objects (php>=5.4) have a better performance than arrays; arrays have an undefined length while with good code, all object properties are defined at compile time. Because of this, you don't need to store the data in a hashtable. Nikic has a post about this subject too: https://gist.github.com/nikic/5015323 https://gist.github.com/nikic/5015323
- cesarb 12y ago> It would be nice to have separate types for arrays and maps though. I don't understand why they were combined to begin with. Simplicity? Seems like there are more edge cases and gotchas the way things are now. Combining arrays and maps in one type was the cause for a remotely exploitable vulnerability in Drupal this year, https://www.drupal.org/SA-CORE-2014-005 https://www.drupal.org/SA-CORE-2014-005. I commented on that at https://lwn.net/Articles/618530/ https://lwn.net/Articles/618530/. Quoting from that comment: "[...] most uses will treat it either as an array (list of items) or as a key/value store (map from key to value, or sometimes set of values), but rarely as both at the same time. [...] In this vulnerability, the programmer expected a sequence, and was handed a mapping. [...] all uses of a single variable should be consistent (never use a sequence method on a mapping variable or a mapping method on a sequence variable). As shown in this vulnerability, "foreach ($data as $i => $value)" is a mapping method; it should never be used on a sequence, even if it works."