r/programming • • Oct 04 '13

What every programmer should know about memory, Part 1

http://lwn.net/Articles/250967/
660 Upvotes

223 comments sorted by

View all comments

Show parent comments

5

u/felipec Oct 04 '13

Optimizing compilers came along and made that unnecessary.

It's still necessary in many cases.

3

u/adrianmonk Oct 04 '13

Fair point. It's unnecessary in a lot fewer cases. It used to be that nearly every programmer could potentially need to do it. Now it's probably 1 in 100 or fewer.

2

u/[deleted] Oct 04 '13

Not really. Unless you're writing code that deals with custom hardware. The closest you should get to ASM imo is compiler intrinsics (SSE and the like).

6

u/felipec Oct 04 '13

Wrong. In multimedia and graphics processing people still need to write a lot of assembly code because compilers make tons of mistakes. Same with bitcoin mining and checksuming.

Basically, if you need something CPU-optimized, sooner or later you will need assembly.

2

u/[deleted] Oct 05 '13

I don't know what platforms or compilers you're working on, but on x86 and arm the compilers are great. We (I work someplace you've heard of doing graphics and multimedia) have literally no assembly code. There is no reason why a good optimizing compiler can't generate fast asm, and often it thinks of optimizations that even our best engineers may miss.

You should try racing the compiler sometime on something more complex than a line or two of code. It will most likely win.

Also keep in mind that inserting asm throws off a ton of optimizations because the compiler can't guarantee you're following the c/c++ standards in your asm.

Edit: accidentally submitted to early.

1

u/felipec Oct 05 '13

I don't know what platforms or compilers you're working on, but on x86 and arm the compilers are great.

That's what you think, but they don't.

http://hardwarebug.org/2009/05/13/gcc-makes-a-mess/

We (I work someplace you've heard of doing graphics and multimedia) have literally no assembly code.

You are probably doing things wrong.

There is no reason why a good optimizing compiler can't generate fast asm, and often it thinks of optimizations that even our best engineers may miss.

I trust FFmpeg/x264 engineers over any gcc compiler.

You should try racing the compiler sometime on something more complex than a line or two of code. It will most likely win.

Wrong.

http://hardwarebug.org/2008/11/28/codesourcery-fails-again/

I also have worked in someplace you've heard doing multimedia, and I can assure you FFmpeg's assembly code is way faster than the C version, that's the whole reason why they have assembly code.

Do you want me to compile both and show you how wrong you are?

1

u/[deleted] Oct 06 '13

So what your examples of people needing to write ASM because it's faster are two obvious bugs in gcc? Ok.

1

u/felipec Oct 06 '13

You can never trust the compiler to generate the most optimal code, and the generated code can never be as optimal as what an expert engineer would write.

I guess you don't want me to compare the performance of C-only FFmpeg vs. C+ASM FFmpeg. Right?

I guess you already know what performs better.

1

u/ethraax Oct 05 '13

Maybe. But it's not necessary in most cases. You can often get by with doing optimization in C. If you need some vector operations, you can use intrinsics, which look like basic functions.

The only two reasons to drop to assembly now-a-days is when speed is of the utmost importance (say, certain parts of a video encoder) or if you need direct access to parts of the machine (like writing an OS). But usually you can get the same performance while staying in C, and even an OS only needs a small amount of assembly to manage the hardware.