5 ms·
> making a dynamic language perform well Perhaps the advantage here is that PHP really all that dynamic. PHP is much closer in design to taking Java and compi
by wvenable 8y ago
> making a dynamic language perform well
Perhaps the advantage here is that PHP really all that dynamic. PHP is much closer in design to taking Java and compiling it on every request than it is like Python or Ruby.
- skrebbel 8y agoWell, you can do $moo = "hello"; $zork = "moo"; echo $$zork; // hello It doesn't get much more dynamic than that.
- wvenable 8y agoIt can. Python, for example, builds up classes at runtime. PHP builds up classes at compile time. There are plenty of other examples. PHP is really dynamic through eval() but that's more rare. Although PHP does support the above syntax, it's actually pretty rare in production code. PHP can be optimized like JavaScript is: perform direct variable/member access but provide a slow path for these kinds of dynamic lookups.
- viraptor 8y agoYou can't just wave away the impact of having eval though. Just the fact that it exists means you need to keep around some supporting state like variable names. You can't do full constant folding and eliminate all intermediate steps for example if there a chance a runtime-created code will reach into your frame and want them back.
- rujuladanh 8y agoSimilar things are done by JS engines and others nowadays. They optimize and keep the original stuff in case assumptions stop holding.
- wvenable 8y agoeval() is scoped to the current block. If you even just ignored JITing any function with eval() in it then you'd still get quite far.
- keithnoizu 8y agoI used to use that pattern $$ frequently for library code.
- EGreg 8y agoI wish PHP had classes spread across several files like ASP.NET.
- bufferoverflow 8y agoYou can do this in PHP: <?php eval('class Hello {public static function you(){echo "Yes!";}}'); Hello::you(); or this: <?php file_put_contents('c.php', '<?php class Hello {public static function you(){echo "Yes!";}}'); require('c.php'); Hello::you(); Thus generating a class at runtime-ish.
- kyberias 8y agoThat looks it merely needs some reflection support (variable name lookup).
- chessturk 8y agoIsn't reflection slow as hell? If not please correct me.
- a1369209993 8y agoIt's less fast than direct lookups, but if the compiler supports it properly, it shouldn't be any slower than a associative array lookup. echo $FOO[$BAR]; echo $$BAZ; >>> push FOO.ptr push BAR.ptr call hash_get push ax call print push local_table // static per compiled scope push BAZ.ptr call hash_get push [ax+24] // entry->value call print You'd probably want to add error handling (to both cases) and accessing a global variable that didn't exist when the code was compiled will likely be slow, but if becomes a preformance bottleneck you can always rewrite it to something easier to compile.
- inglor 8y agoAnd of course a smart JIT compiler can even put the variable access lookup in an inline cache on the function so that in the future it's a direct type + key = offset calculation rather than a memory lookup and the hash_get call is avoided.
- a1369209993 8y agoTrue. I was aiming for the simplest example that could pass as real general-case-ish assembly; there's plenty of room for improvement.
- chrisseaton 8y agoThe whole point of a JIT is that you don't do naive variable name lookup. With a proper intermediate representation you'd connect that $$ variable lookup directly to the original variable's edge.
- Tarean 8y agoIt's also very much frowned upon so a slow path is fine. The _get and _set magic methods (called when getting/setting missing properties) are quite common especially for frameworks.