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.

    • toynbee@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      2 months ago

      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

  • TootSweet@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    2 months ago

    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.

    • Tyoda@lemm.ee
      link
      fedilink
      arrow-up
      0
      ·
      2 months ago

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

  • blusterydayve26@midwest.social
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 months ago

    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?