Skip to content

Conversation

@TysonAndre
Copy link
Contributor

Track an 8-byte pointer as the key instead of 16-byte zval.
(4 byte on 32-bit builds)

The memory used for a set of 1 million objects decreased from
114 MiB to 106 MiB.

Use the new Z_PARAM_* APIs to parse the zend_object* pointer directly

<?php
call_user_func(function () {
    $startMem = memory_get_usage();
    $count = 1000000;
    $start = hrtime(true);
    $s = new SplObjectStorage();
    for ($i = 0; $i < $count; $i++) {
        $s->attach(new stdClass(), $i);
    }
    $end = hrtime(true);
    $endMem = memory_get_usage();
    // Before: 0.119 seconds 113.939 MiB (due to change from zval to pointer being stored)
    // After : 0.094 seconds 105.939 MiB (due to change to Z_PARAM_* to parse zend_object* directly)
    printf("Time to add %d objects to SplObjectStorage: %.3f mem=%.3fMiB\n", $count, ($end - $start)/1e9, ($endMem - $startMem) / 1000000);
});

Copy link
Member

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Track an 8-byte pointer as the key instead of 16-byte zval.
(4 byte on 32-bit builds)

The memory used for a set of 1 million objects decreased from
114 MiB to 106 MiB.

Use the new `Z_PARAM_*` APIs to parse the `zend_object*` pointer directly
@TysonAndre TysonAndre force-pushed the SplObjectStorage-optimize branch from 85ae80a to 230534d Compare January 4, 2021 14:55
@php-pulls php-pulls closed this in e622e86 Jan 4, 2021
@TysonAndre TysonAndre deleted the SplObjectStorage-optimize branch January 4, 2021 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants