ApiResult::isMetadataKey is an extremely hot method that was already micro-optimized in the past, going from substr to string index access (r660062) and then from string index access to ord (r662076). Any optimization needed not only to be as fast as possible, but also support the string potentially being empty or it being an integer. While ord() is the fastest solution we could find in PHP <8, PHP 8.0 introduces str_starts_with, which seems roughly equally faster, and also gracefully handles the integer and empty string cases.
I wrote a quick and oversimplified test script:
<?php $num = 100000000; $str = 'abcdefghijklmno'; $s = hrtime(true); for ( $i = 0; $i < $num; $i++ ) { $res1 = ord( $str ) === 95; } $time = (hrtime( true ) - $s)/10**6; echo "ORD: $time\n"; $s = hrtime(true); for ( $i = 0; $i < $num; $i++ ) { $res3 = str_starts_with( $str, '_' ); } $time = (hrtime( true ) - $s)/10**6; echo "START: $time\n";
which yields results like these on my machine (ignore PHP8.2 being much slower, likely some local issue):
$ php8.1 test.php ORD: 1588.489258 START: 1560.760053 $ php8.2 test.php ORD: 5365.255071 START: 5210.782069
Other test runs report str_starts_with as being slower than ord, but the values are always very close, so we will need some more accurate benchmarking. Also, and most importantly, str_starts_with is much more readable, so we should switch to it if possible once we support PHP 8+.