- Implemented expected() and unexpected() compile-time functions for branch prediction hints - Added C++ macro generation for EXPECTED(...) and UNEXPECTED(...) in compiled output - Updated documentation to include new branch prediction functions - Added comprehensive tests for branch prediction macro generation - Modified compiler base to handle expected/unexpected function type detection - Added parser support for branch prediction function calls with proper argument validation - Included polyfill implementations for expected and unexpected functions - Updated version number from 1086 to 1087pull/34/head
parent
90b61fbc42
commit
576222998c
9 changed files with 107 additions and 3 deletions
@ -0,0 +1,12 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
function phpunit_branch_prediction(bool $likely, mixed $unlikely): int |
||||||
|
{ |
||||||
|
if (expected($likely)) { |
||||||
|
return 1; |
||||||
|
} |
||||||
|
if (unexpected((bool) $unlikely)) { |
||||||
|
return 2; |
||||||
|
} |
||||||
|
return 3; |
||||||
|
} |
||||||
@ -0,0 +1,27 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase; |
||||||
|
use TypePhp\CompilerTest; |
||||||
|
|
||||||
|
final class BranchPredictionTest extends TestCase |
||||||
|
{ |
||||||
|
public function testGeneratesExpectedAndUnexpectedMacros(): void |
||||||
|
{ |
||||||
|
global $translator; |
||||||
|
$compiler = CompilerTest::create(ROOT_PATH); |
||||||
|
$translator = $compiler; |
||||||
|
$file = __DIR__ . '/../code/branch-prediction.php'; |
||||||
|
$compiler->addFiles([$file]); |
||||||
|
$compiler->prepareFile($file); |
||||||
|
$cppFile = $compiler->convertFile($file); |
||||||
|
$code = file_get_contents($cppFile); |
||||||
|
|
||||||
|
$this->assertStringContainsString('if (static_cast<bool>(EXPECTED((likely))))', $code); |
||||||
|
$this->assertStringContainsString( |
||||||
|
'if (static_cast<bool>(UNEXPECTED((php::toBool(unlikely)))))', |
||||||
|
$code, |
||||||
|
); |
||||||
|
$this->assertStringNotContainsString('php_expected(', $code); |
||||||
|
$this->assertStringNotContainsString('php_unexpected(', $code); |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,39 @@ |
|||||||
|
--TEST-- |
||||||
|
expected and unexpected provide branch prediction hints without changing condition semantics |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
function predicted_condition(int &$calls, bool $result): bool |
||||||
|
{ |
||||||
|
$calls++; |
||||||
|
return $result; |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$calls = 0; |
||||||
|
|
||||||
|
if (expected(predicted_condition($calls, true))) { |
||||||
|
echo "expected\n"; |
||||||
|
} |
||||||
|
|
||||||
|
if (unexpected(predicted_condition($calls, false))) { |
||||||
|
echo "unexpected-true\n"; |
||||||
|
} else { |
||||||
|
echo "unexpected-false\n"; |
||||||
|
} |
||||||
|
|
||||||
|
if (\expected(condition: predicted_condition($calls, true))) { |
||||||
|
echo "fully-qualified\n"; |
||||||
|
} |
||||||
|
|
||||||
|
var_dump(expected(1), unexpected(0), $calls); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
expected |
||||||
|
unexpected-false |
||||||
|
fully-qualified |
||||||
|
bool(true) |
||||||
|
bool(false) |
||||||
|
int(3) |
||||||
@ -1 +1 @@ |
|||||||
1086 |
1087 |
||||||
|
|||||||
Loading…
Reference in new issue