Compiler Optimizations Debate

Discussions center on whether compilers should or can optimize away code such as checks, stores, or functions, often debating issues like undefined behavior, side effects, and language semantics.

➡️ Stable 0.6x Programming Languages
4,013
Comments
19
Years Active
5
Top Authors
#9733
Topic ID

Activity Over Time

2008
4
2009
18
2010
79
2011
70
2012
75
2013
109
2014
240
2015
216
2016
215
2017
282
2018
224
2019
244
2020
315
2021
401
2022
385
2023
339
2024
383
2025
390
2026
24

Keywords

e.g CPU OP FFI cppreference.com C99 LTO godbolt.org string.h O0 compiler optimization optimize code function gcc dead optimizations int void

Sample Comments

TeMPOraL Sep 12, 2017 View on HN

Not if there was a proper optimization pass while compiling.

nextaccountic Jan 10, 2022 View on HN

Does this feature inhibit any kind of optimization that current compilers could perform?

amelius Apr 30, 2015 View on HN

Shouldn't the compiler be able to figure out that such an optimization is possible?

steveklabnik Mar 31, 2022 View on HN

No, it is a language semantics issue. You can get the compiler to optimize it away today, if you're careful. But that's a bad user experience. You should be able to specifically request this semantic, and be sure it works.

omaranto Oct 25, 2021 View on HN

Well, if it didn't, they could optimize the compiler by removing those optimizations. :)

jokoon Jul 4, 2021 View on HN

So the compiler cannot always optimize...

saagarjha Apr 28, 2019 View on HN

I feel like a compiler is within its rights to optimize out that check if it so wishes, though.

summm Feb 11, 2024 View on HN

wouldn't any compiler optimize this anyway?

10098 Nov 26, 2013 View on HN

It's weird to me that a post like this even needs to be written. Isn't it kind of obvious that it's highly problematic for the compiler to do this optimization because the function, generally speaking, may have side effects?

kaba0 Jul 9, 2024 View on HN

Well, the compiler says undefined behavior and optimizes it away :D