8 ms·
Deep Dive into PHP 8's JIT
- untog 6y agoTiny nitpick: the article describes JavaScript as an interpreted language without JIT when most major JS engines do have JIT optimisation. It’s difficult to make this call about any language with more than one implementation. But anyway, kudos to the PHP folks for yet another improvement. It’s been a long time since I used it but I’m continually impressed by how far it’s come. And it’s a useful test to how flexible a developer is willing to be to ask them to code some PHP, there are fewer and fewer reasons against it these days other than personal style preference.
- viraptor 6y agoThe whole idea of "language X is interpreted/compiled" is a simplification I wish people were more careful with. Cpython is interpreted, pypy has JIT, mypyc is compiled. It's the implementation, or even implementation's specific runtime options that decide about things like that, not the language.
- userbinator 6y agoThe statement would be far more accurate as "language X is [almost always/sometimes/often/commonly/etc.] interpreted/compiled". Unfortunately a lot of people seem to like speaking in absolutes. C can be interpreted too: https://en.wikipedia.org/wiki/CINT https://en.wikipedia.org/wiki/CINT
- lights0123 6y agoAnd the Rust compiler ships with a Rust interpreter, used to evaluate const fns (functions whose output can be determined at compile time).
- richardwhiuk 6y agoconst fns can only use a subset of the language AIUI
- colejohnson66 6y agoTheoretically, any language can be interpreted. Compilation is just interpreting what a program does and producing machine code (or other code in the case of transpilers) that computes the same thing. Interpreters are just that, but instead of producing code, they run code in themselves thats computes the results directly. One could even consider machine code just “obfuscated” assembly code. In that sense, machine code is just another language that also could be interpreted (intepretation-based emulators like Bochs) or compiled again (JIT-based emulators like DOSBox). This brings up a slightly related question: is there a language that can’t be compiled?
- tyingq 6y ago"question: is there a language that can’t be compiled?" There is some question whether Perl could be, because parsing it without running it has some ambiguity. https://www.perlmonks.org/?node_id=663393 https://www.perlmonks.org/?node_id=663393
- Kednicma 6y agoBy wielding the Futamura projections [0][1][2], any interpreter may be turned into a compiler. Perl is handled as one case. Crucially, the resulting compiler need not be fast; if the interpreter is slow, then the compiler will be slow too (this is a special case of the central meme from [3], "if the N'th Futamura projection has quality Q, then the N+1'th Futamura projection will also have quality Q.") [0] https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_projections https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_pr... [1] http://blog.sigfpe.com/2009/05/three-projections-of-doctor-futamura.html http://blog.sigfpe.com/2009/05/three-projections-of-doctor-f... [2] https://www.gwern.net/docs/cs/2009-gluck.pdf https://www.gwern.net/docs/cs/2009-gluck.pdf [3] https://www.itu.dk/people/sestoft/pebook/ https://www.itu.dk/people/sestoft/pebook/
- viraptor 6y agoThat's not really a barrier. It's "can't be parsed without ambiguity", not "can't be parsed". You compile cases like that by compiling a check for the relevant condition, then compile the possible versions in the if/else branches. Unless you can prove which case it will be from other code - then you can simplify anyway.
- coldtea 6y ago>Unfortunately a lot of people seem to like speaking in absolutes. People like speaking in the general case. It saves time from enumeating any inconsequential / statistically not relevant exception. The real problem is people misunderstanding casual discussion absolutes (which mean "for the large majority/for the ones people care about") with mathematical absolutes (i.e. "X is Y for each and every X")
- coldtea 6y agoWell, the idea being that CPython is 99% of what people use, and the other are nearly irrelevant projects...
- agumonkey 6y agoPHP's story really is something to remember as to show no matter how low you start, in a fertile soil, you can grow long and far.
- nawarian 6y agoI should probably add this kind of thing to the article then. It is nearly impossible to state "Language X is compiled/interpreted/jitted". I didn't explicitly say that JS doesn't come with JIT, because I'd be putting Node.JS and every browser engine in the same bag. I simply can't be sure about all of them. I'll add very soon this caveat that the dialect (js, php, python) doesn't really matter and make it clear that I'm talking about engines, possibly directly point to them. I hope you understand, though, that I wanted to make JIT as a concept understandable. The language comparisons are secondary. Thanks a lot for your feedback!
- deleted 6y ago[deleted]
- google234123 6y agoDidn't Facebook also develop is JIT for their PHP? Or have they moved on frmo PHP?
- tyingq 6y agoHack/HHVM no longer has a goal to be PHP compatible, but it looks pretty much the same. It does have a JIT: https://hhvm.com/blog/2027/faster-and-cheaper-the-evolution-of-the-hhvm-jit https://hhvm.com/blog/2027/faster-and-cheaper-the-evolution-...
- ksec 6y agoDo we know anyone outside of Facebook uses Hack/HHVM in production?
- muglug 6y agoSlack is the only other company you might have heard of (but still, Slack).
- nym3r0s 6y agoWikipedia runs on HHVM IIRC. https://hhvm.com/blog/7205/wikipedia-on-hhvm https://hhvm.com/blog/7205/wikipedia-on-hhvm
- njuw 6y agoWikipedia was moved back to PHP: - https://phabricator.wikimedia.org/T176370 https://phabricator.wikimedia.org/T176370 - https://phabricator.wikimedia.org/T229792 https://phabricator.wikimedia.org/T229792 - https://launchdarkly.com/blog/how-the-wikimedia-foundation-successfully-migrated-to-php7/ https://launchdarkly.com/blog/how-the-wikimedia-foundation-s...
- nym3r0s 6y agoDidn't know that. Thanks for the info!
- freelancercv 6y ago"PHP as fast as C" - People who are in php language development may die laughing reading this statement. Truth can be harsh, but people sometime overvalue to such an extent is hard to understand.
- chubot 6y agoWhat I've noticed is that JITs often can reach C speed in workloads like this: sum = 0 for i in xrange(n): for j in xrange(i): sum += A[i][j] And when they reach that milestone, some people call it "as fast as C". Never mind that that's not what people actually write in Python or PHP. It's a synthetic benchmark, not a real workload. The workloads in those languages are generally oriented around strings, hash tables, and function/method calls. And the JITs don't seem to do nearly as good a job there. I tested PyPy on Oil [1] a few years ago, and it made it slower, not faster. And it used more memory. (Though PyPy is an amazing project in many respects.) [1] https://www.oilshell.org https://www.oilshell.org
- remram 6y agoThis is not what people write in Python or PHP, but this is what people write in C extensions for Python or PHP. Having your JIT be that fast allows you to forego those extensions and write the low-level hot loops in the same language, and that's a huge improvement. You usually don't care how your matrix multiplication/regex matching/unicode normalization/JSON parsing is implemented, but people had to make those, and they are users of the language too. Even though it might not change the bottom-line for your high-level app.
- chubot 6y agoWell, the problem is that Python and PHP are actually bad languages for expressing code like that. For expressing C. For one, they're not statically typed. Julia is a dynamic language that seems to do better because it was designed for this purpose. But it doesn't seem to have panned out in practice in Python, or PHP as far as I know. Those languages have huge piles of C, and whenever you call into C, the JIT gets confused. People don't seem to rewrite their huge piles of C in Python or PHP. In Python, it's more likely Cython. I'd like to see pointers to counterexamples -- where people actually wrote some C-like code in Python or PHP and let the JIT do its work. I haven't seen it, aside from the PyPy project itself, and maybe a few other examples. I think you would still take a significant performance hit. The issue is that C compilers in 2020 are even better at compiling the example I showed. They do amazing things with that kind of code that state-of-the-art JITs don't in practice.
- CountHackulus 6y agoI definitely misread this title as the P8 JIT that IBM was working on for a while.
- ksec 6y agoMay be everyone on HN has Adblock/ PiHole on. But this Blog, a single page has 5 Google Ads in it. I dont mind one or two, top and bottom. But 5, right in the middle of every section.
- iKevinShah 6y agoI am out of advertising for a long time so I might not be aware but back in 2009s-10s, Adsense had a limit for 3 ads per page. Is that not a thing anymore? Genuinely curious Edit: Yes, some initial searches reveal that it's not a thing anymore.
- geek_at 6y agoBlogs like these are the main reason to use ad blocks. And from their perspective: everyone using adblocks is the reason they need 5 ads on the site
- nawarian 6y agoHey man, author here. Thanks for the feedback. I recently added adsense there and I'm testing with the amount of ads in the page. Currently I'm letting AdSense decide how many ads it should place and where. But if you believe the amount is so harmful for your experience, don't worry. I'll be more than glad to reduce this amount. Cheers!
- rurban 6y agoNot a deep dive, a rather very shallow dive. Deep dives were handled before: https://wiki.php.net/rfc/jit https://wiki.php.net/rfc/jit And its discussion https://externals.io/message/103903 https://externals.io/message/103903
- nawarian 6y agoI've read both documents a couple of times. I wonder what is in there that you believe should be in a "deep dive" or what is missing there in your opinion. Thanks for your feedback and I'm keen to hear more from you :)
- pjmlp 6y agoUnless we are speaking about Java before Java 1.2, it is definitely not interpreted, there are plenty of JIT and AOT implementations without any kind of interpretation step. Since 25 years, time to learn that implementations and languages are not the same.
- ptx 6y agoGood point, but are there really "plenty" of Java implementations that lack an interpreter? There's Graal and Excelsior JET for AOT. Any others? Which implementations do JIT without any interpretation?
- pjmlp 6y agoBEA J/Rockit was one of them. Although not the right kind of coffee, Android during versions 5 and 6. Also most JVMs with interpretation step, have command line options to set the interpretation threshold to zero, thus directly JIT compiling on load.
- nawarian 6y agoI might be wrong here, as I'm not so close to Java development. But a language implementing JIT, at least to me, is interpreted. Could you please point an implementation detail where a JIT-capable engine doesn't include interpretation in its runtime? In every case, thanks a lot for your feedback!
- jashmatthews 6y agoV8 had only baseline compilation until much later https://v8.dev/blog/ignition-interpreter https://v8.dev/blog/ignition-interpreter It's not super exotic to do this.
- pjmlp 6y agoWell, from CS compiler theory point of view it is not. For example in .NET, MSIL goes directly into a pipeline that produces native. You can easily validate that RyuJIT has no interpretation. Or for example, watchOS applications packaged with bitcode, get JIT compiled at installation time.
- fetbaffe 6y agoThere is a small error in the text. When describing the opcache.jit setting it shows examples where the third flag is set to 5, eg 1255, but the table of possible values for the third flag, 'T - JIT trigger', goes from 0 to 4.
- nawarian 6y agoThanks a lot! I blindly fetched this from the reference mentioned there. I'll update this as soon as I have time (probably during this week) and also let the author of the referenced post know about it.
- jashmatthews 6y agoThis article is actually now quite out of date as PHP has switched to a tracing JIT which appears to be heavily based on LuaJIT. https://github.com/php/php-src/pull/5874 https://github.com/php/php-src/pull/5874 https://github.com/php/php-src/commit/4bf2d09edeb14467ba79551a08d84efdff314899 https://github.com/php/php-src/commit/4bf2d09edeb14467ba7955...
- pansa2 6y agoAny ideas why they’ve moved from a method-based JIT to tracing? AFAIK tracing JITs are generally inferior to method-based ones, which is why none of the major JavaScript engines use tracing. Their only advantage seems to be (relative) simplicity, which is essential for the lightweight LuaJIT but not for PHP.
- MaxBarraclough 6y agoThat doesn't sound right. How would a trace-based JIT be easier to implement? There was a research project which made HotSpot into a trace-based JIT. It reported both faster compilation times, and superior quality of generated code. (I don't know if the HotSpot folks ever considered adopting the changes.) http://www.ssw.jku.at/Research/Papers/Haeubl11/ http://www.ssw.jku.at/Research/Papers/Haeubl11/
- pansa2 6y ago> How would a trace-based JIT be easier to implement? When compiling at method-granularity, with incomplete information about types, complex features are required e.g. On-Stack Replacement (if the current method becomes "hot", it needs to be replaced by a compiled version), Inline Caches, Hidden Classes and Deoptimization. In a trace-based JIT, all these features can be replaced by "just compile another trace".
- jashmatthews 6y agoWe need all those complex features with tracing JITs too. Getting into a trace is basically OSR. Exiting a trace and restoring the interpreter state is basically an OSR exit or deoptimization. It's really just different terminology for the same thing. Inline caches on method calls become guards. We can't just ignore them. LuaJIT does without hidden classes or property caching because on-trace it's able to eliminate table access and allocation. Performance tanks on OO Lua once you fall off trace. You can work around it with "compile another trace" in a sense that works as long as you have infinite cache.
- nawarian 6y agoThank you all for sharing your comments and thoughts. I'm unfortunately out of time today, but I'll try to address your comments asap :) Cheers!