8 ms·
Mysterious Memset
- fguerraz 5y agoNot really mysterious or a surprise. Passing count as a reference was a huge red flag from the very beginning.
- ReactiveJelly 5y agoIt surprised me, cause I forgot that aliasing is a problem for C. Guess I'm one of today's 10,000.
- jhgb 5y agoI'm sort-of, kind-of aware of the issue but what surprised me was how std::string pulls this into picture here. Is this because of what str[i] gets compiled into when str is a basic_string of chars? That seems non-obvious to me because I'm just accustomed to dealing with C++ strings as fairly opaque entities. If they somehow expose to the compiler that you're really manipulating characters via pointers, I can understand how this complicates things.
- Diggsey 5y agoIt would be a surprise to anyone not very familiar with C++'s strict-aliasing rule, which I imagine is quite a lot of people (and even many C++ programmers...) Very few other languages have a similar rule (eg. Rust explicitly does not have this rule)
- ahefner 5y agoThe surprising thing to me is that 'char8_t' (a new C++ 20 thing I'd never heard of) is not just a typedef to char with all the same aliasing implications, but a new and distinct type to which the magic 'char' alias rules don't apply (also, unsigned).
- matheusmoreira 5y agoI agree, it's surprising. Types like uint8_t* are widely used to reinterpret other structures as byte arrays which implies they are universal aliases just like char*. Not sure what makes char8_t different.
- wahern 5y ago> Types like uint8_t* are widely used to reinterpret other structures as byte arrays which implies they are universal aliases just like char* Alternatively, it implies that there's alot of broken code out there. So much broken code that they've accidentally found safety in numbers, and compilers are unlikely to change a coincidental behavior upon which they wrongly relied. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66110 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66110
- MauranKilom 5y agoAlso, char is a distinct type from both signed char and unsigned char (even though it has the same size as both and the same signedness as one of them).
- jhgb 5y agoIs signed char and unsigned char subject to the same "can alias anything" rule? I never bothered to think about this in the past. Now I'm not sure. I've known that the three types are distinct, but that just means that that the rule doesn't have to apply to all of them, not that it doesn't.
- rsstack 5y agoTIL: https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule https://stackoverflow.com/questions/98650/what-is-the-strict...
- kevincox 5y agoAlthough Rust doesn't have this rule it isn't nearly as relevant due to the way immutable (shared) references work. While `a: &Foo` and `b: &Bar` may alias it doesn't matter much because Rust knows that nothing can write to them so it can do basically all of the optimization it needs. Rust also knows that `c: &mut T` doesn't alias with anything so in this case it can make loads of assumptions. One chink in this rule is interior mutability. This is why it is better to think of `&T` as a shared reference than an immutable reference. For example in the following code Rust can't assume that `a.get() == 5`. (Even if the second argument is changed to i8) pub fn test(a: &std::cell::Cell<u8>, b: &std::cell::Cell<u8>) -> u8 { a.set(5); b.set(7); a.get() } https://rust.godbolt.org/z/e1c6Kx4eb https://rust.godbolt.org/z/e1c6Kx4eb Commenting out the write to b does allow Rust to hardcode the return value as 5.
- bonzini 5y agoReplace *count with this->count and you can see why this can cause pessimization.
- fguerraz 5y agoWhat would that change? If this->count was a pointer you'd still have to de-reference it explicitly. Or am I missing the point of your argument?
- bonzini 5y agoI agree that in this toy example passing a count argument by reference makes little sense, but you'd get the same thing if you accessed the count field field of struct s { int count; char *chars; };
- andi999 5y agoNice. Simplest fix would be to introduce a local variable int c=*count before the loop. (which you also would do with a non optimizing compiler so the count could be in a register.
- ncmncm 5y agoThe point, which they probably should have spelled out better, was that std::string contains a char* that aliases everything there might be a pointer to. It is often hard to prevent pointers being created to things; calling a function that takes a "T const&" argument, the compiler is typically obliged to assume that a pointer to the argument was taken and retained. So, the "*count" in the example is a stand-in for a zillion other things you would actually have written, and that would also be affected by aliasing assumptions.
- taneq 5y agoI've been coding commercially in C and C++ (not full time but pretty regularly) for over 20 years and I'm starting to think the simplest fix is to not use C or C++.
- hermitdev 5y agoTo the author: the initial for loop is not equivalent to a do/while loop. The condition of the for loop is executed before the loop runs. e.g. the loop body will never run if *count == 0.
- codetrotter 5y agoThere’s an if statement around the do while loop on that page, so that the do while loop also will not run if *count == 0. The code snippets for (int i = 0; i < *count; ++i) { str[i] = 0; } and if (*count > 0) { long idx = 0; do { str->__data[idx] = 0; idx += 1; } while (idx <= *count); } Will produce the same changes to the string data given the same count.
- einpoklum 5y agoI have one word for us all: restrict ... https://en.cppreference.com/w/c/language/restrict https://en.cppreference.com/w/c/language/restrict Still missing from C++ after all these years. Get it in there, people! See also: https://stackoverflow.com/q/776283/1593077 https://stackoverflow.com/q/776283/1593077
- awoimbee 5y agoCouldn't the author pass a reference to a std::string instead of switching to an std::u8string, is that a C++ limitation ? And I really don't understand why the compiler doesn't assume strict aliasing in the first example.
- LAC-Tech 5y agoInteresting, yet terrifying, as is all C++ wizardry to me.
- spyremeown 5y agoI had to write business logic in C++20 for an embedded Linux box and o-m-g. It sucked. So much. The developer experience was horrendous. All the PRs dragged along because of micro-optimizations every step of the way. "Let me see the generated assembly in Godbolt" why, why? I know this is probably not all due to the language, but at least one bit of it has to be. It's really cool if you're writing bare-metal/RTOS level embedded stuff and you're worrying about how many assigments can you put into a loop round to optimize the cache lines, but I don't understand why anyone would ever try to talk to the web using C++.
- AshamedCaptain 5y ago> "Let me see the generated assembly in Godbolt" why, why? This is a embedded thing, not a C or C++ thing. The other day someone was saying here on HN that writing Verilog feels like following a process where "you already more or less know which circuit do you want, you are just trying to figure which is the specific Verilog code that will get your synthesizer to generate that circuit". On embedded platforms (or generally anywhere where you count memory usage in units of KB or less), that's exactly what many people do. They already know more or less the assembly code, they are just looking for the right higher-level program that will translate to that assembly. That's one of the reasons they get angry when the language tries to be too smart. The reason you just don't code in assembly directly is because it's still a pain and your chances of mistake increase (e.g. doing complex arithmetic expressions).
- vanderZwan 5y agoDoes that also mean that embedded programming involves being conservative about compiler updates? Because otherwise those choices might become completely invalid one upgrade later
- rurban 5y ago__restrict would have also helped. in the case of memset, it's questionable if that library call is actually faster for count < 128
- SuchAnonMuchWow 5y agoI believe restrict is a C only keyword, so no, it couldn't have helped.
- adzm 5y agoWhile not standardized, restrict or __restrict are indeed supported by most C++ compilers. Visual C++ even supports a declspec(restrict) that can apply to the function rather than just individual arguments.
- kevingadd 5y agosince it's std::memset, the compiler could theoretically inline it or have a small-count path that uses vector intrinsics
- bonzini 5y agoRecent Intel processors have "fast rep stos" which basically allows the compiler to always inline memset (which they couldn't do in the aliasing case).
- fefe23 5y agoI don't get the reference to undefined behavior. This is an aliasing issue, which is pretty well defined. Am I missing something? Other than that: Interesting insight!
- masklinn 5y ago> I don't get the reference to undefined behavior. > [...] > Am I missing something? `int*` and `char8_t*` aliasing is UB under strict aliasing. You can call it "an aliasing issue, which is pretty well defined". As far as the compiler is concerned it's no different than every other UB it uses for constraints inference.
- sharikous 5y agoI think the idea is that if the user had aliased *count and the string they would have got undefined behavior. So the compiler can assume they didn't and is allowed to optimize away the loop with memset.
- andi999 5y agoOr do more nefarious stuff.
- eklitzke 5y agoAliasing in the first example is well defined, so no UB in the first example. Aliasing in the second example would be undefined in the sense that the strict aliasing rules forbids the two pointer types to be aliased in the first place.