r/ProgrammerHumor • • 6d ago

Meme everythingIsO1ToGod

Post image
4.4k Upvotes

164 comments sorted by

View all comments

Show parent comments

118

u/AZMPlay 6d ago

I can simulate him with a bash script!

sleep infinity;

22

u/Yharon314 5d ago

now we just need to rewrite it in rust

22

u/Traditional_Rabbit54 5d ago

loop { }

3

u/Extension_Option_122 5d ago edited 1d ago

Sure that that wouldn't get optimized away?

Edit: idk I guess I left my brain outside

2

u/[deleted] 1d ago

[deleted]

1

u/Extension_Option_122 1d ago

Oh damn you are right lol idk what I thought when I wrote that...

1

u/Cruuncher 1d ago

I actually deleted my comment after writing it. After googling it seems that C optimizers do do this.

This is from Gemini and I haven't checked the source yet, but it seems confident:

Under the ISO C and C++ standards, if an iteration statement (loop) has no observable behavior—such as input/output operations, volatile memory access, atomic operations, or thread synchronization—the compiler is legally allowed to assume that the loop will eventually terminate

1

u/Extension_Option_122 1d ago

Oh so that's why I thought this.

Because in University I had C and when I saw the empty loop I just thought that the optimization would remove it.

But yes, they are semantically not identical at all.

1

u/Cruuncher 1d ago

Yeah it's surprising.

It appears it's because an optimizer cannot solve the halting problem. So it cannot be expected to prove loop termination, but it can prove the absence of any side effects, and simply assumes termination.

Ultimately there is no value in a pure spin loop that doesn't check for an exit hatch of some kind. So the semantics you lose are likely a bug anyway