Live data from Hacker News

PHP 7's new hashtable implementation

nikic.github.io

41–50 of 137 posts

Re: PHP 7's new hashtable implementation

#41
This is probably not a popular opinion, but I believe that PHP's associative array is one of the best-designed data structures in programming languages.

Its main distinguishing property, as mentioned in this article, is that values can be indexed by key, but are still iterated in the order they were set. This is "do what I want" in so many cases that it's just nuts.

Sure, just as often it's just needless overhead, but as a programmer who prefers to reason about domain and not performance, I often don't care about that. I hate that many other languages, including C#, Ruby and Python, force me to choose between either an unordered map or a list of (key, value) tuples. EDIT: clearly, i'm behind the times with that remark. thanks commenters :-)

I wish more languages had a native data type like this. It scares me that in practice JS objects have the same property, but officially the iteration order is not specified.

(that said, PHP's choice to mix regular arrays and associative arrays into a single type strikes me as a bit odd. i've also never seen a good use case of arrays with mixed string/int keys)

Re: PHP 7's new hashtable implementation

#42

Awesome, but I still don't think it's enough. In benchmarks we did the memory usage of PHP array() was horrific. Sorry I don't have actual numbers to post, but we ended up using pack() and unpack() to store stuff that should have been in an array because it would grow to 100's of megs using PHP's array() and using a binary structure it stays under 10 megs. I just don't think a 2.5X improvement is going to come close…

> we ended up using pack() and unpack() to store stuff that should have been in an array because it would grow to 100's of megs using PHP's array() and using a binary structure it stays under 10 megs.

How many items were you storing / how big was the data in each item?

Re: PHP 7's new hashtable implementation

#43

This is probably not a popular opinion, but I believe that PHP's associative array is one of the best-designed data structures in programming languages. Its main distinguishing property, as mentioned in this article, is that values can be indexed by key, but are still iterated in the order they were set. This is "do what I want" in so many cases that it's just nuts. Sure, just as often it's just needless overhead, bu…

Python has https://docs.python.org/2/library/collections.html#collectio...

I've never wanted this though. I just discovered OrderedDict when I was looking for something like std::map.

Re: PHP 7's new hashtable implementation

#44

This is probably not a popular opinion, but I believe that PHP's associative array is one of the best-designed data structures in programming languages. Its main distinguishing property, as mentioned in this article, is that values can be indexed by key, but are still iterated in the order they were set. This is "do what I want" in so many cases that it's just nuts. Sure, just as often it's just needless overhead, bu…

Python has https://docs.python.org/2/library/collections.html#collectio... I've never wanted this though. I just discovered OrderedDict when I was looking for something like std::map.

Cool! I didn't know that!

Re: PHP 7's new hashtable implementation

#45

This is probably not a popular opinion, but I believe that PHP's associative array is one of the best-designed data structures in programming languages. Its main distinguishing property, as mentioned in this article, is that values can be indexed by key, but are still iterated in the order they were set. This is "do what I want" in so many cases that it's just nuts. Sure, just as often it's just needless overhead, bu…

> I hate that many other languages, including C#, Ruby and Python, force me to choose between either an unordered map or a list of (key, value) tuples

Another commenter mentioned Python's OrderedDict, and Ruby's hashtables are ordered (from 1.9+): https://www.igvita.com/2009/02/04/ruby-19-internals-ordered-... It looks like C# also has an OrderedDictionary class: http://msdn.microsoft.com/en-us/library/system.collections.s...

I never had to use any of these classes even if I do use hashmaps all the time, so IMHO the Python way is best (default to a 'normal' unordered hashmap, offer an ordered alternative)

Re: PHP 7's new hashtable implementation

#46
post #14

Earlier quoted context omitted.

PHP has a standard library with plenty of collections: http://php.net/manual/en/book.spl.php Stack - http://php.net/manual/en/book.spl.php Queue - http://php.net/manual/en/class.splqueue.php PriorityQueue - http://php.net/manual/en/class.splpriorityqueue.php Real Maps - http://php.net/manual/en/class.splobjectstorage.php It's a shame some people are not aware of these.

No primitive types: tuples, lists, sets. Having only arrays to work with and being weak typed results in a lot of headache.

Those aren't primitive types

Re: PHP 7's new hashtable implementation

#47
Make sure you don't miss this part:

> PHP uses hashtables for all arrays. However in the rather common case of continuous, integer-indexed arrays (i.e. real arrays) the whole hashing thing doesn’t make much sense. This is why PHP 7 introduces the concept of “packed hashtables”.

> [...] We keep these useless values around so that buckets always have the same structure, independently of whether or not packing is used. This means that iteration can always use the same code. However we might switch to a “fully packed” structure in the future, where a pure zval array is used if possible.

It's nice that they're starting to consider the fact that "real" arrays are unnecessarily mixed with hashtables, which comes with a pretty significant overhead. Let's hope they'll soon add that different separate type for arrays (or "fully packed hashtables" if they prefer :)).

Re: PHP 7's new hashtable implementation

#48
post #35

Earlier quoted context omitted.

Adding non-numeric keys does not remove an Array's "arrayness" in JavaScript var x = []; console.log(Object.prototype.toString.call(x)); //[object Array] x[0] = 1; console.log(x[0]); //1 x["test"] = 2; console.log(x["test"]); //2 console.log(Object.prototype.toString.call(x)); //[object Array] x.map //function map() { [native code] } var y = {}; console.log(Object.prototype.toString.call(y)); //[object Object] y.map…

It does internally. Pretty sure that's even defined in the standard.

If you can't observe a difference, does it matter?

Re: PHP 7's new hashtable implementation

#49

Awesome, but I still don't think it's enough. In benchmarks we did the memory usage of PHP array() was horrific. Sorry I don't have actual numbers to post, but we ended up using pack() and unpack() to store stuff that should have been in an array because it would grow to 100's of megs using PHP's array() and using a binary structure it stays under 10 megs. I just don't think a 2.5X improvement is going to come close…

Check the part about packed hashtables (in case you're not storing key/value pairs). They said they're considering a more compact type for actual arrays.

Re: PHP 7's new hashtable implementation

#50
post #25
post #22

Earlier quoted context omitted.

Well, yeah, I agree that the SPL types are nice to have. My complaint is that PHP's only first-class array type is a weird array/map combo. Have you ever seen anything like that in another language? The SPL types are definitely a welcome addition to the language, but they feel like add-ons. Definitely not first-class. The standard array functions don't work with SPL types (array_map, etc.) even though the SPL types a…

> My complaint is that PHP's only first-class array type is a weird array/map combo. Have you ever seen anything like that in another language? Um, Javascript?

Nope.

Array are objects,but all objects aren't Arrays.

    var a={};

    a instanceof Array // false
Furthermore,Javascript objects aren't maps.
Post reply on HN