logoalt Hacker News

raverbashingyesterday at 2:08 PM1 replyview on HN

Optimizing out a write to (example) an array on the stack seems fine to me.

Optimizing out a function call to a heap pointer (especially memset) seems wrong to me. You called the function, it should call the function!

But it's again the C language saving time not wearing a seatbelt or checking the tire pressure for saving 10s on a 2h trip


Replies

fluoridationyesterday at 2:35 PM

The whole point of the optimizer is that it can detect inefficiencies by treating every statement as some combination of simple, fundamental operations. The compiler is not seeing "call memset() on pointer to heap", it's seeing "write of variable size" just before "deallocation". For some, optimizing that will be a problem, for others, not optimizing it will leave performance on the table.

There are still ways to obtain the desired behavior. Just put a call to a DLL or SO that implements what you need. The compiler cannot inspect the behavior of functions across module boundaries, so it cannot tell whether removing the call preserves semantics or not (for example, it could be that the external function sends the contents of the buffer to a file), so it will not remove it.

show 1 reply