I just realized something that never occurred to me before - every
property is actually stored as a hash.

This test-script will demonstrate:

  <?php

  define('NUM_TESTS', 1000);

  $before = memory_get_usage(true);

  $test = array();

  class Foo
  {
    public $aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
    public $bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb;
    public $cccccccccccccccccccccccccccccccccccccccccccccccccccc;
    public $dddddddddddddddddddddddddddddddddddddddddddddddddddd;
  }

  $bytes = 0;

  for ($i=0; $i<NUM_TESTS; $i++) {
    $foo = new Foo;

    $foo->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 'a';
    $foo->bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb = 'b';
    $foo->cccccccccccccccccccccccccccccccccccccccccccccccccccc = 'c';
    $foo->dddddddddddddddddddddddddddddddddddddddddddddddddddd = 'd';

    $bytes += 4;

    $test[] = $foo;
  }

  $after = memory_get_usage(true);

  header('Content-type: text/plain');

  echo ($after-$before).' bytes used; '.$bytes.' bytes of information stored.';

Output is this:

  786432 bytes used; 4000 bytes of information stored.

I know this an extreme example, I just did it to see if what I
suspected was actually correct.

How come it's necessary to store the property-names of every property
in every object? For properties that have been defined in classes, why
can't they be stored in a more efficient manner? (using lookup tables)

I know the nature of PHP is dynamic, and I know that dynamic
properties would have to be stored in a key/value form internally...
but if you look at modern PHP software, dynamic properties is actually
something very few people use.

My suspicion is that all this memory-overhead has performance
implications as well? Allocating and deallocating memory for all of
these repeated property-names, it can't be very efficient?

I don't know much about the inner workings of PHP or C in general, but
if the property-names are in deed stored repeatedly, and if the
string-type uses a pointer, wouldn't it possible to point all of the
property-name strings to same address in memory, sharing the
property-name strings, instead of storing them repeatedly?

Just a thought...

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to