3 ms·
What's the alternative? Returning a"Result" struct doubles the size. This is one less register to use. Exception handling is even more invasive. They are gre
by j16sdiz 3y ago
What's the alternative?
Returning a"Result" struct doubles the size. This is one less register to use.
Exception handling is even more invasive.
They are great for high(-er) level language, but less prefect on lower level where performance is critical.
EDIT: Linux kernel use negative return value for error. It's good and efficient when it work. But it is not always an option when you need the full register width
- bdd8f1df777b 3y agoMost of the POSIX functions that use errno for error signaling simply return 0 for success and -1 for error. They could have returned errno directly.
- tedunangst 3y agoWhat about the rest?
- deleted 3y ago[deleted]
- bdd8f1df777b 3y agoPut the original return value in a pointer argument, and return errno (or -errno).
- ronsor 3y agoLast I checked, the Linux kernel ABI does return -errno for errors. Then libc mangles that all up.
- wahern 3y agoSome newer POSIX APIs, such as pthreads, do return the error this way. But many legacy APIs, such as dup or read, use the positive integer space, thus the negation pattern you often see in syscalls. Notably, POSIX guarantees <errno.h> values to be positive integers.
- bdd8f1df777b 3y ago`dup` and `read` returns -1 on error and set the `errno` variable. If they were redesigned, they should just return `-errno` on failure.
- burntsushi 3y agoThere are a lot of alternatives, and it's not clear why the ones you've suggested are inappropriate. You've listed some perceived costs, but I don't see why those costs are greater than the ones paid by the status quo. Linux even shows you a path, yet you reject it for reasons that don't seem compelling to me.
- Dylan16807 3y ago> Returning a"Result" struct doubles the size. This is one less register to use. One less register right before and after returns doesn't sound like a big problem, especially with 16+ registers.
- josefx 3y agoYou are saving one register at the cost of having a thread local variable that is visible to signal handlers, so none of its uses can be optimized away. Which results in things like gcc having to decorate every math instruction with code to set errno on the off chance that someone somewhere might read it (no one ever does).
- gpderetta 3y agoThe performance cost of having otherwise pure functions clobber global mutable memory defeating many optimization passes, is way higher than clobbering another register for the result.