With modern tools, you have to try very hard to do something stupid, because the tools (rightly) recognize you’re doing something stupid. [Andreas Karlsson] can speak to that first hand as he tried to get four billion if statements to compile.

You may ask what state space requires four billion comparisons to evaluate? The answer is easy: the range of an unsigned 32-bit integer. The whole endeavor started with a simple idea: what if instead of evaluating whether an integer is even or odd with a modulo or bit mask, you just did an if statement for every case? Small ranges like 0-10 are trivial to write out by hand, but you reach for more automated solutions as you pass 8 bits and move towards 16. [Andreas] wrote some Python that outputs a valid C program with all the comparisons. For 16 bits, the source only clocks in at 130k lines with the executable less than 2 MB.

Of course, scaling to 32 bits is a very different problem. The source file balloons to 330 GB, and most compilers barf at that point. Undeterred, [Andreas] modified the Python to output x86_64 assembly instead of C. Of course, the executable format of Windows (PE) only allows executables up to 4 GB, so a helper program mapped the 40 GB generated executable and jumped into it.

What’s incredible about this whole journey is how performant the program is. Even large numbers complete in a few seconds. Considering that it has to thrash 40 GB of an executable through memory, we can’t help but shake our heads at how even terrible solutions can work.

  • TootSweet@lemmy.world
    link
    fedilink
    English
    arrow-up
    11
    ·
    edit-2
    3 个月前

    Reminds me of this file. This file’s only 1,300-ish lines, so not on the same scale at all, but the vibe has some amount of similarity. Except the one I linked here apparently is intended for serious real-world use.

    • UndercoverUlrikHD
      link
      fedilink
      arrow-up
      5
      ·
      3 个月前

      Overloading a createArray up to 249 times… I can only assume the person was paid per character typed.

    • Tyoda@lemm.ee
      link
      fedilink
      arrow-up
      4
      ·
      3 个月前

      If you don’t have to apologize for the class in its documentation, then it wasn’t worth writing in the first place.

    • toynbee@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      3 个月前

      Part of the reason I got hired at my current job was because I solved their coding challenge in bash (which was my strongest scripting language at the time, many years ago).

      My solution was definitely not 26,000 lines, though.

      edit: closing -> coding

    • UndercoverUlrikHD
      link
      fedilink
      arrow-up
      3
      ·
      3 个月前

      It’s what happens when someone takes the joke “AI is just a bunch of if-statements” too literal.

  • blusterydayve26@midwest.social
    link
    fedilink
    English
    arrow-up
    5
    ·
    3 个月前

    I want to see the companion program that queries the size of your number and then decides which part of the executable to mmap. It’d be so much faster and just require one or two more pieces in the analysis pipeline. We can totally set up a k8s cluster for this, right?