4 ms·
Doesn't the restrict keyword solve this?
by jonathrg 5y ago
Doesn't the restrict keyword solve this?
- user-the-name 5y agoI don't think anyone has ever used the restrict keyword, or understood what it does.
- moonbug 5y agoderp.
- jonathrg 5y agoWhat? I use it whenever I have a function that takes two or more pointers if I know they can't refer to overlapping memory. And it's part of the signature for memcpy since C99
- IshKebab 5y agoWhen he said "anyone" he meant "almost anyone". You're an outlier if you use `restrict` regularly. I checked on grep.app, there are 10k results for `restrict` for C code, compared to 700k for `struct` (I know they're not directly comparable but that gives an idea.
- jonathrg 5y agoThat seems like about the right proportion to me - struct solves a much more common problem than restrict does. And to be fair, it is a lesser known feature. But user-the-name is implying that restrict is somehow difficult to use or understand, which I don't agree with at all.
- icedchai 5y agoThat probably explains it. In many shops, C may as well have stopped at C89. I've been working with C since the early 90's. I've never seen any code use restrict.
- enjoy-your-stay 5y agoAlso large chunks of libc use it as well, e.g. the printf family of functions.
- tjalfi 5y agoThe paper Why Programmer-specified Aliasing is a Bad Idea[0] evaluated the effectiveness of restrict in 2004. They found that adding optimal restrict annotations provided only a minor performance improvement, on average less than 1% across the SPEC2000 benchmarks. [0] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.94.140&rep=rep1&type=pdf http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.94....
- dralley 5y agoHow much of this is because nobody puts effort into these optimizations? The Rust compiler has repeatedly found critical bugs in LLVM's restrict / noalias support, bugs that would impact C / C++ as well if any real-world C / C++ programs actually used it. If compilers produce straight-up broken code in these situations, I can only imagine they're not putting a lot of effort into these optimization strategies.
- tjalfi 5y ago> How much of this is because nobody puts effort into these optimizations? restrict is rare in C and C++ but common in Fortran; array parameters in Fortran aren't allowed to alias. Intel and IBM both have great Fortran compilers so I would expect their C and C++ compilers to have good support for restrict.