this post was submitted on 08 Aug 2024
52 points (98.1% liked)

Programming Horror

1874 readers
1 users here now

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there's also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @[email protected]

Rules

Credits

founded 1 year ago
MODERATORS
 

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.

top 11 comments
sorted by: hot top controversial new old
[–] [email protected] 11 points 2 months ago* (last edited 2 months ago) (3 children)

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 5 points 2 months ago

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

[–] [email protected] 4 points 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.

[–] [email protected] 3 points 2 months ago

LOL. That could be its own post in this community.

Thanks for sharing that.

[–] [email protected] 10 points 2 months ago (2 children)

One of the codebases on my team used to deploy everything with a 26,000 line Bash script

[–] Kissaki 3 points 2 months ago* (last edited 2 months ago)

That's quite bashful

[–] [email protected] 1 points 2 months ago* (last edited 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

[–] [email protected] 6 points 2 months ago (1 children)

Yandere Simulator has/had a smaller, but similar file with a mountain of if statements. It was meant to power the NPCs' AI.

ETA: some more context from someone who's more experienced

[–] UndercoverUlrikHD 3 points 2 months ago

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

[–] [email protected] 5 points 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?

[–] [email protected] 3 points 2 months ago

"Add more days."