Live data from Hacker News

How big are PHP arrays (and values) really?

nikic.github.com

1–10 of 90 posts

Re: How big are PHP arrays (and values) really?

#2
As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. Arrays weren't designed to store integers efficiently or anything else for that matter. They were designed to be fast and easy to use.

Re: How big are PHP arrays (and values) really?

#3
PHP arrays are not really arrays, they are sort of hash-maps.

You can do things like

    $arr = array(1 => 10, "1" => 11);
Or even

    $arr = array('他妈的我的生活' => 5);
But at the same time you can treat them as regular zero-based arrays.

    $arr = array();
    $arr[] = 1;
    $arr[] = 2;
    $arr[] = 3;
    $arr[] = 'dog';

Re: How big are PHP arrays (and values) really?

#4
post #2

As the author mentions, if you run into a use-case where you need to store 100000 integers in memory, then you should use one of the many alternative structures available. Some of them were explicitly designed to store integers in an efficient manner. Arrays weren't designed to store integers efficiently or anything else for that matter. They were designed to be fast and easy to use.

I wouldn't call using 56 bytes to store 8 bytes of actual data efficient.

Re: How big are PHP arrays (and values) really?

#7
post #3

PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';

And once you relalize that they are really hashes you also realize that a fast and memory efficient representation will use at least nine machine words or 72 bytes per value: 1 + x for the pointer in the bucket array (you don't want 100% occupancy to avoid hash collisions), 2 for the cons cell of the list stored in the bucket (the second element of which will usually be NULL if you have a good hash function and low occupancy), 2 for the key-value pair pointed to by the first element of the cons cell representing the index and the value, and two each for the integer key and value plus their tag words). And you may also want to store some metadata (like the length of the list stored in each bucket) with the hash, so the PHP array in the example is within a factor of two from the naive optimum for a dynamically typed language without specialized arrays.

Its just the price you have to pay for not caring about the type of your "array" keys.

Re: How big are PHP arrays (and values) really?

#8
post #3

PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';

A PHP array is in fact just an ordered hash-map (IE.. hash map with an associated linked list which tracks the iteration order).

When you do an array append, the logic PHP runs is it tries to guess what the most logical key would be, add that to the hash-map and then to the end of the linked list.

  $a = array();
  $a[] = 'A';
  $a[] = 'B';
  print_r($a);

  Array
  (
      [0] => A
      [1] => B
  )

  $b = array();
  $b[5] = 'A';
  $b[100] = 'B';
  $b[] = 'C';
  print_r($b);

  Array
  (
      [5] => A
      [100] => B
      [101] => C
  )

Re: How big are PHP arrays (and values) really?

#9
post #3

PHP arrays are not really arrays, they are sort of hash-maps. You can do things like $arr = array(1 => 10, "1" => 11); Or even $arr = array('他妈的我的生活' => 5); But at the same time you can treat them as regular zero-based arrays. $arr = array(); $arr[] = 1; $arr[] = 2; $arr[] = 3; $arr[] = 'dog';

It's funny. I don't have much experience with php, and I had to actually run your last example to know that it appended the values (instead of recreating the array with a single value each time).

Is there an append operator, or something more explicit ? '+=' seems to do something strange..

Post reply on HN