Skip to content

Instantly share code, notes, and snippets.

@kaja47 kaja47/lol.php
Last active Aug 29, 2015

Embed
What would you like to do?
PHP polymorphic inline caches
<?php
class A {
public $somePropertyWithReasonablyLongName = 1;
}
class B {
public $somePropertyWithReasonablyLongName = 1;
}
// AAAA
$arr = [];
for ($i = 0; $i < 256; $i++) {
$arr[$i] = new A;
}
$s = microtime(1);
for ($i = 0; $i < 10000000; $i++) {
$arr[$i & 0xff]->somePropertyWithReasonablyLongName;
}
echo microtime(1) - $s, " s\n";
// ABAB
$arr = [];
for ($i = 0; $i < 256; $i++) {
$arr[$i] = (($i % 2 === 0) ? new A : new B);
}
$s = microtime(1);
for ($i = 0; $i < 10000000; $i++) {
$arr[$i & 0xff]->somePropertyWithReasonablyLongName;
}
echo microtime(1) - $s, " s\n";
// PHP 5.6 PHP 7.0
// -------------------------
// AAAA 0.6 s 0.3 s
// ABAB 0.7 s 0.4 s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.