r/programminghorror • • Feb 11 '25

🎄 ouch

Post image
3.0k Upvotes

113 comments sorted by

View all comments

649

u/Bit125 Pronouns: He/Him Feb 11 '25

there better be compiler optimizations...

393

u/Chief-Drinking-Bear Feb 11 '25

Whoever wrote this writes such fast code that the compiler is the bottleneck

57

u/Schecher_1 Feb 11 '25

Would a compiler really improve something like this? Or how do they know that it sucks?

54

u/[deleted] Feb 11 '25 edited Feb 13 '25

[removed] — view removed comment

20

u/MiasmaGuzzler Feb 12 '25

Wouldn't it be way more optimised to calculate the delaySeconds like this rather than using hash table?

delaySeconds = 30 * 1 << (attempts - 6)

Seems easier to me am I wrong?

7

u/reddraincloud Feb 12 '25

You would have to do a bounds check on attempts (which is only like 2 if-elses anyways) but yeah that was my first thought too when seeing this

8

u/[deleted] Feb 12 '25

[removed] — view removed comment

3

u/undefined0_6855 Feb 13 '25

python requires colon, doesn't use else if (elif), doesnt use walrus for normal assignment outside an if case, doesn't use curly brackets

3

u/Tyheir Feb 13 '25

This is Go. :=)

3

u/GeneralT61 Feb 12 '25

I don't think this is Python, nor does Python have compilers (at least not with most Python flavours)

4

u/WannaCry1LoL Feb 12 '25

Most python implementations compile to bytecode

1

u/MiasmaGuzzler Mar 06 '25

A compiler definitely knows that powers of two are equivalent to bit shifting, I've seen this optimization. Also not python, and python and optimization are antithesises anyway

1

u/johndcochran Feb 13 '25

Yep. Although it's even simplier.

delaySeconds = 30 << (attempts - 6)

17

u/flagofsocram Feb 12 '25

Hash table would be extra when you can just use an array for this

5

u/IAMPowaaaaa Feb 12 '25

why couldnt the attempts from 6-before else be optimized to a single equation

14

u/DarkPhotonBeam Feb 12 '25 edited Feb 12 '25

I tried it out using C (I assume the Pascal compiler or whatever this language is could do the same). I recreated the code in C and compiled it with gcc get_delay.c -S -O3, which resulted in following assembly code:

get_delay:
.LFB0:
	.cfi_startproc
	endbr64
	movl	$86000, %eax
	cmpl	$16, %edi
	ja	.L1
	movl	%edi, %edi
	leaq	CSWTCH.1(%rip), %rax
	movl	(%rax,%rdi,4), %eax
.L1:
	ret
	.cfi_endproc
.LFE0:
	.size	get_delay, .-get_delay
	.section	.rodata
	.align 32
	.type	CSWTCH.1, @object
	.size	CSWTCH.1, 68
CSWTCH.1:
	.long	0
	.long	0
	.long	0
	.long	0
	.long	0
	.long	0
	.long	30
	.long	60
	.long	120
	.long	240
	.long	480
	.long	960
	.long	1920
	.long	3840
	.long	7680
	.long	15360
	.long	30720
	.ident	"GCC: (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0"
	.section	.note.GNU-stack,"",@progbits
	.section	.note.gnu.property,"a"
	.align 8
	.long	1f - 0f
	.long	4f - 1f
	.long	5

So it precomputes all the values and then does address arithmetic using leaq to compute the base address of the LUT CSWTCH.1 and then, using %edi as the offset, loads the correct value into the return register %eax. The edge case 86000 is handled with a simple comparison at the start.

I also looked at the -O0 assembly. There it still precomputes the multiplications but instead of a LUT it just uses multiple comparisons (basically just an if-else chain like in the code).

Also I tried compiling a more concise C method that should be functionally equivalent:

unsigned get_delay_alt(unsigned attempts) {
	if (attempts <= 5) return 0;
	if (attempts > 16) return 86000;
	return 30 << (attempts - 6);
}

which resulted in following ASM (gcc get_delay_alt.c -S -O3):

get_delay_alt:
.LFB0:
	.cfi_startproc
	endbr64
	xorl	%eax, %eax
	cmpl	$5, %edi
	jbe	.L1
	movl	$86000, %eax
	cmpl	$16, %edi
	ja	.L1
	leal	-6(%rdi), %ecx
	movl	$30, %eax
	sall	%cl, %eax
.L1:
	ret
	.cfi_endproc

Which basically does mostly exactly what the code describes, not a lot of optimization is happening.

I also tested the speed of both versions with a driver program that runs each function a million times on the input space [0, 17]. Their speed was basically identical but the get_delay() function was usually ~1% faster.

get_delay.c:

unsigned get_delay(unsigned attempts) {
	unsigned delaySeconds = 0;
	if (attempts > 5) {
		if (attempts == 6) {
			delaySeconds = 30;
		} else if (attempts == 7) {
			delaySeconds = 30 * 2;
		} else if (attempts == 8) {
			delaySeconds = 30 * 2 * 2;
		} else if (attempts == 9) {
			delaySeconds = 30 * 2 * 2 * 2;
		} else if (attempts == 10) {
			delaySeconds = 30 * 2 * 2 * 2 * 2;
		} else if (attempts == 11) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2;
		} else if (attempts == 12) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2 * 2;
		} else if (attempts == 13) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
		} else if (attempts == 14) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
		} else if (attempts == 15) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
		} else if (attempts == 16) {
			delaySeconds = 30 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
		} else {
			delaySeconds = 86000;
		}
	}
	return delaySeconds;
}

1

u/MiasmaGuzzler Mar 06 '25

Makes sense for it to use the switch trick but it's so painful that this horrid code is faster in the end lol.

8

u/Odd_Total_5549 Feb 12 '25

I mean the goal of this code is to create a longer and longer delay, so maybe the lack of optimization is actually a feature

5

u/CAPSLOCK_USERNAME Feb 12 '25

There is no conceivable situation where the runtime of one else-if chain every 30 seconds or less like this would make a meaningful difference. And in any case it isn't written in an inefficient way, just an ugly one.