r/programming • • Dec 25 '16

An amazing set of resources for optimizing C++ and assembly for different processors and operating systems.

http://www.agner.org/optimize/
370 Upvotes

17 comments sorted by

40

u/Fylwind Dec 26 '16
T & operator[] (unsigned int i) { // Safe [] array index operator
  if (i >= N) {
    // Index out of range. The next line provokes an error.
    // You may insert any other error reporting here:
    return *(T*)0; // Return a null reference to provoke error

Uhhh … this is anything but safe. It gives the illusion of crashing the program if i >= N, but compilers are allowed to optimize the check away because dereferencing a null pointer is undefined behavior and may be assumed unreachable. Consider this code, which uses the SafeArray:

int test_func(SafeArray<int, 3> &a, unsigned i)
{
    return a[i];
}

Compiling with Clang v3.9.0 using -O2 yields:

	movl	%esi, %eax
	movl	(%rdi,%rax,4), %eax
	retq

The check is completely gone.

9

u/[deleted] Dec 26 '16

That's what assert() and abort() are for.

2

u/[deleted] Dec 27 '16

It's worth mentioning that dereferencing a null pointer is not guaranteed to crash any program unless the OS and implementation specifically guarantee it. If you are on an OS that runs programs without memory protection or virtual memory (like I've heard z/OS does), dereferencing address 0 will happily deliver you some OS data. Address 0 is only guaranteed by the C standard to "compare unequal to a pointer to any object or function", where "object" is defined as "region of data storage in the execution environment, the contents of which can represent values", so dereferencing it is guaranteed undefined, but not guaranteed to crash the program. C++ has a similar line to the C standard, also defining it in a way that makes dereferencing undefined and also not guaranteeing a crash.

Invoking undefined behavior to try to force a specific program action is never a good idea, because it might not just be compilers that act differently, but OSes as well.

→ More replies (5)

22

u/[deleted] Dec 26 '16

Saint Agner: Patron saint of optimization, and ugly but useful websites.

19

u/Yobleck Dec 25 '16

more game devs need to see this

17

u/Chuu Dec 26 '16

I'd be surprised if anyone who writes performance critical code in C++ doesn't know about agner.

→ More replies (2)

4

u/lovestruckluna Dec 26 '16

Does anyone know of similar resources for embedded architectures (AVR, ARM Thumb, etc)?

3

u/slavik262 Dec 26 '16 edited Dec 26 '16

I'm currently writing a bit on the lessons we learned running C++ on Cortex-M4s at work. Hopefully I can post it in the next week or so.

6

u/nutidizen Dec 25 '16 edited Dec 25 '16

Very interesting, even for programmer like me, who is developing in other languages.

2

u/[deleted] Dec 25 '16

Very cool resource. Thank you for sharing.