Live data from Hacker News

PHP 7's new hashtable implementation

nikic.github.io

91–100 of 137 posts

Re: PHP 7's new hashtable implementation

#91
post #82

Earlier quoted context omitted.

It results in different hidden class because the whole point is to be able to reference named fields by fixed offsets from the object location in memory (same as for example reading struct fields in C). If the order changes, so will the offsets too, so same names with different order must have different hidden classes. Integer keys are not practical to treat as fixed because they are used as array indices 99% of the…

I don't think your explanation makes much sense. Hidden classes describe the layout of objects: if two insertion orders produced the same hidden class, it would mean they have the same layout, with the same fixed field offsets. The only difficulty I can see with doing that is that is insertion order.

I am not sure what you are even saying.

My point is that if the most optimal way would result in some other iteration order, that would be the iteration order experienced by users and that it is just a coincidence that the most optimal way results in insertion order.

Re: PHP 7's new hashtable implementation

#92
post #59
post #53

Earlier quoted context omitted.

PHP isn't written in C++. It's written in some pretty macro dense C. Even if it were, isn't a hash table.

But std::unordered_map is. I wonder what would be the actual C++ memory consumption using that.

C++ unordered map does not maintain the order of the elements, which PHP does. So it is not a fair comparison.

Re: PHP 7's new hashtable implementation

#93

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…

Perl used to let you do this, btw, but they moved away because apparently some denial of service issues were found regarding hashmaps, and so there are security reasons not to. I think you can tell Perl still you don't want that security and it will behave in this way. THere are other ways to do this only on some hashes but they have something of a performance penalty.

IIRC a Perl hash has never been in the order of key add order, but rather that two hashes, with its keys added in the same order, returned their keys in the same order, even in separate executions. Now hash key order is always "random".

Tie::IxHash [1] is available on Cpan.

[1] https://metacpan.org/pod/Tie::IxHash

Re: PHP 7's new hashtable implementation

#94
post #63

> The hash returned from the hashing function (DJBX33A for string keys) is a 32-bit or 64-bit unsigned integer I thought there was a big hooha about PHP and other dynamic languages using ill-suited hash functions and ultimately most runtimes moved to SipHash?

Can you provide a citation for "most"?

I'm pretty sure the Perl, Python and Ruby reference implementations all use it.

Re: PHP 7's new hashtable implementation

#95
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…

His point was: x = [1]; x["test"] = 555; x.map(function (y) { return y; }); // [1] That's [1], not [1, 555]. You can access the array from the map interface, but not the other way around.

The array prototype functions are only specified by the standard to deal solely with numeric keys, but the lines between and object and array are still pretty blurry. The use case for each is different, but the original question far above was "array/map combo. Have you ever seen anything like that in another language?", and clearly JavaScript is very similar even if not exact.

  var x = {};
  x[0] = 1;
  x.length = 1;
  x.map = Array.prototype.map;
  x.map(function(y) {return y;});
  //[1]

Re: PHP 7's new hashtable implementation

#96
post #59

Earlier quoted context omitted.

But std::unordered_map is. I wonder what would be the actual C++ memory consumption using that.

C++ unordered map does not maintain the order of the elements, which PHP does. So it is not a fair comparison.

Something approximately equivalent would be Boosts multi_index with a couple of indexes.

    using php_array_t = multi_index_container,
            hashed_unique>
        >
    >;
There are so many design considerations at play though that such a comparison would be pointless.

Re: PHP 7's new hashtable implementation

#97
post #82

Earlier quoted context omitted.

I don't think your explanation makes much sense. Hidden classes describe the layout of objects: if two insertion orders produced the same hidden class, it would mean they have the same layout, with the same fixed field offsets. The only difficulty I can see with doing that is that is insertion order.

I am not sure what you are even saying. My point is that if the most optimal way would result in some other iteration order, that would be the iteration order experienced by users and that it is just a coincidence that the most optimal way results in insertion order.

I'm saying that argument is wrong, because there is no efficiency loss due to different insertion orders mapping to the same hidden class. In fact there are efficiency advantages because more objects would pass through class-based guard checks into favourable paths. The only reason I can see to not do that is because it would lose iteration order.

Sketch out an example of hidden class transitions and it should be clear:

In the order preserved case (what JS engines actually do):

  start with {} of hidden class 
  add .x, transition to hidden class  with field .x at 0
  add .y, transition to hidden class  with field .x at 0 and field .y at 1

  start with {} of hidden class 
  add .y, transition to hidden class  with field .y at 0
  add .x, transition to hidden class  with field .y at 0 and field .x at 1
In the order discarded case:

  start with {} of hidden class 
  add .x, transition to hidden class  with field .x at 0
  add .y, transition to hidden class  with field .x at 0 and field .y at 1

  start with {} of hidden class 
  add .y, transition to hidden class  with field .y at 0
  add .x, transition to hidden class  with field .x at 0 and field .y at 1
In the second case the field .y changes offset, but that's fine because the hidden class also changes to indicate the difference in structure. The only problem is that the order of the fields changes.

Re: PHP 7's new hashtable implementation

#98
post #85

An important variable determining memory consumption is going to be the maximum bucket load factor. Does anyone know what it is as currently implemented?

The maximum load factor is 1. A lower load factor only makes sense if open addressing is used.

Re: PHP 7's new hashtable implementation

#99
post #83

Copied from /r/php (care of http://www.hhvm.rocks ): dev@aerilon ~/dev $ php --version PHP 5.5.20-pl0-gentoo (cli) (built: Dec 22 2014 13:44:21) dev@aerilon ~/dev $ hhvm --version HipHop VM 3.5.0-dev (rel) dev@aerilon ~/dev $ php memusage.php 13.97 MBs [14649088 bytes] dev@aerilon ~/dev $ hhvm memusage.php 2 MBs [2097152 bytes] So basically this implementation still uses 100% more RAM (hhvm is 64bit) by default compa…

Did you miss the multiple times PHP 7 was referenced?

The code you pasted shows PHP 5.5.20-pl0-gentoo

Re: PHP 7's new hashtable implementation

#100
post #83

Copied from /r/php (care of http://www.hhvm.rocks ): dev@aerilon ~/dev $ php --version PHP 5.5.20-pl0-gentoo (cli) (built: Dec 22 2014 13:44:21) dev@aerilon ~/dev $ hhvm --version HipHop VM 3.5.0-dev (rel) dev@aerilon ~/dev $ php memusage.php 13.97 MBs [14649088 bytes] dev@aerilon ~/dev $ hhvm memusage.php 2 MBs [2097152 bytes] So basically this implementation still uses 100% more RAM (hhvm is 64bit) by default compa…

The funny thing 5.5 isn't even the current stable version of PHP, which is 5.6. Let alone the whole post is about PHP 7. It's like you're not even paying attention, just doing the whole post for the last phrase.
Post reply on HN