JIT Compiling Code in 5μs

(malisper.me)

58 points | by zX41ZdbW 3 hours ago

6 comments

  • MaxBarraclough 1 hour ago
    Reminds me of the 2024 blog post Look ma, I wrote a new JIT compiler for PostgreSQL [0]. Both articles lament that Postgres's LLVM-based JIT [1] takes a while to generate code.

    > The rarity of JIT compilers makes me believe that implementing a JIT compiler historically was too difficult for it to be worthwhile.

    That's only true of writing a JIT from scratch. There's no rarity of JITs, it's just that LLVM (and other frameworks) are often used. Every major interpreter has a JIT compiler. PCRE2 has a JIT compiler. There are JIT frameworks out there with much faster code-generation than LLVM: Cranelift, GNU Lightning, Mir. I doubt they could do code-generation faster than a custom copy-and-patch JIT, but they'd be much faster than LLVM.

    [0] https://www.pinaraf.info/2024/03/look-ma-i-wrote-a-new-jit-c... , discussed: https://news.ycombinator.com/item?id=39742916

    [1] https://www.postgresql.org/docs/current/jit-reason.html

  • hamilyon2 24 minutes ago
    It uses copy-and-patch compilation to archive that
  • varjag 15 minutes ago
    There’s been a meme circulating about how AI doesn’t help because “code was never the hard part.” I think that’s true in some domains, but in others, writing the code absolutely was the hard part. JIT compilers are a great example of that.
    • asdfsa32 9 minutes ago
      Anyone who thinks AI is good with writing code that is hard to write for the operator, not due to lack of basic software engineering know how but complexity of the domain, either has access to models beyond what is available to the public or is completely lost.

      I believe this because every time I use AI for domains that I consider myself above competent, if it is anything beyond UI components or a simple CRUD endpoints, I cringe at the quality of what it generates.

      This has made me to be extremely cautious of starting working in a new domain with AI if I want anything beyond throw away quick hacks or junk, shy of quick bug fixes perhaps.

  • glum64 1 hour ago
    Uhm, Common Lisp, where JIT is not only available but is also manageable: the programmer can decide what deserves to be compiled and what does not.

    Besides run time, JIT is available also when the code is compiled or loaded for execution (i.e., do you have a compilation or loading speed-up in mind? no problem, you can also compile that speed-up into native machine code, and so ad infinitum...).

  • glenjamin 2 hours ago
    pgrust sounds very interesting, but with the deep changes there’s no viable path to upstream it - is the end goal to be robust enough that it’ll get wide adoption?
  • roschdal 1 hour ago
    JIT compilation is unsecure.
    • asdfsa32 1 hour ago
      You're entirely correct because JIT requires violating Write xor Execute security policy. This is the reason on iOS, it is limited to Apple shipped software.

      https://en.wikipedia.org/wiki/W%5EX

      • shakna 57 minutes ago
        The wiki page mentions this is only a minor problem. Because everyone just writes, then switches and executes.
        • asdfsa32 37 minutes ago
          But this means that you have to decide who is allowed to switch.
    • sebzim4500 1 hour ago
      Maybe, but surely there are users who are willing to trust all users of their db instance.
      • asdfsa32 1 hour ago
        The issue is that it restricts from locking-down and securing the system with Write xor Execute memory. So it has system wide implication.

        https://en.wikipedia.org/wiki/W%5EX

        • PhilipRoman 49 minutes ago
          W^X is typically per mapping, not per memory page and does not interfere with JIT compilation.
          • asdfsa32 28 minutes ago
            Sure, but it still means that the OS has to decide who is allowed to do it and to what extent. Sophisticated worms like Stuxnet would be much harder with strict W^X for example, since CVE-2010-2568 and the like would be much harder to execute.
            • orf 7 minutes ago
              > Sure, but it still means that the OS has to decide who is allowed to do it and to what extent

              It has to do that anyway?

            • pjmlp 6 minutes ago
              Signed binaries with the proper assigned OS capabilities.
    • JackSlateur 16 minutes ago
      In rust, is jit equivalent to an "unsafe" block ?
    • dennis16384 1 hour ago
      It is the core of ClickHouse for example, for many years. Is it secure enough in your opinion?
    • pjmlp 9 minutes ago
      Machine code is insecure, we should all run interpreted code in a formally verified interpreter.

      Alternatively, only allow for the execution of cryptographly signed static linked binaries, this naturally includes the interpreter above.