JIT Compiling Code in 5μs

(malisper.me)

53 points | by zX41ZdbW 3 hours ago

5 comments

  • MaxBarraclough 55 minutes 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 6 minutes ago
    It uses copy-and-patch compilation to archive that
  • 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 1 hour 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 45 minutes 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 39 minutes ago
        The wiki page mentions this is only a minor problem. Because everyone just writes, then switches and executes.
        • asdfsa32 19 minutes ago
          But this means that you have to decide who is allowed to switch.
    • sebzim4500 48 minutes ago
      Maybe, but surely there are users who are willing to trust all users of their db instance.
      • asdfsa32 44 minutes 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 31 minutes ago
          W^X is typically per mapping, not per memory page and does not interfere with JIT compilation.
          • asdfsa32 10 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.
    • dennis16384 1 hour ago
      It is the core of ClickHouse for example, for many years. Is it secure enough in your opinion?