facebook / hhvm

A virtual machine for executing programs written in Hack.
https://hhvm.com
Other
18.18k stars 2.99k forks source link

feature request - force JIT on every request to optimize marked blocks of code #1791

Closed ghost closed 10 years ago

ghost commented 10 years ago

simple example file: <?php main(); function main() {

cached JIT code

$a = $_GET['optionA']==='1'; $b = sanitize_blah($_GET['optionB']); define('myConst', $_GET['foo']==='1'); etc etc

blah

FORCE JIT START = $a, $b, myConst

for ($i=0;$i<1000000000;++$i) { if ($a===true) {#unnecessary expressions will get removed from the hot code

blah

} else {

blah

}

more hot loop code

}

FORCE JIT END

more cached code

} ?>

we save the JIT engine time by naming specifically what variables and constants we want to be optimized.

A note about constants Constants are useful in HHVM because they're fast and global so you don't have to pass as many variables around as params for every function. The "global is bad" idea doesn't apply to constants because they can't mutate after they've been set. Recently by changing my string hashes to constants (set to integers) I got a 21% speed gain. eg: $giantArray[$i]['foo']; became $giantArray[$i][foo];

scannell commented 10 years ago

Thanks for reporting this. Feel free to submit a PR if you (or someone else reading this) get to it. We likely won't get to this for a while since it won't provide much benefit for large scale applications beyond RepoAuthoritative + WholeProgram mode and we don't want people to have to micro-optimize like this.

ghost commented 10 years ago

Thanks Scannell. When do you guys plan to have the profiler working and modifying code in realtime? That would be good for big loops, observing them at a low priority and then cutting out the unnecessary operands or expressions, without requiring manual optimization as above.

scannell commented 10 years ago

Performance work on HHVM is ongoing and continues to be one of the main focuses of the team.

ottoni commented 10 years ago

lope, the JIT already has a profiling mechanism. However, the JIT doesn't recognize loops yet, so it can't do loop optimizations (including loop unswitching in your example). We're working to increase the size of the JIT compilation regions, which will allow it to capture loops and then apply loop optimizations. But it'll be a few months until we get there.

ghost commented 10 years ago

Thanks for sharing your awesome work guys. I'm looking forward to contributing some PRs when I can.

fredemmott commented 10 years ago