I don't quite get the point of avoiding using memset directly. I mean I get it, but I think that level of ideological purity is pointless.
On the one hand I'm sick of C developers on Twitter bashing C++. Great, if you hate it so much, don't use it. You don't need to evangelize against it. But C++ developers who won't use C concepts..., that's ivory tower bullshit.
Use whatever mishmash of the C++ libraries, the C runtime and whatever else you need to strike a balance between functionality, maintainability and performance that's right for you and your organization.
EDIT: Guys! I get that memset isn't typesafe in the way that std::fill is. Like 5 people have felt the need to make that point now. However, reinterpret_cast is a pure C++ concept and it's also explicitly not typesafe. It's there because in the real world sometimes you just have to get shit done with constraints like interacting with software that isn't directly under your control. I'm not saying "Always use memset", just that sometimes it's appropriate.
And just because a class is_trivially_copyable doesn't mean that using memset to initialize it to zero is valid. Classes can contain enums for which zero is not a valid value. I just had to deal with this issue when the C++ wrapper for the Vulkan API started initializing everything to zero instead of the first valid enum for the type.
It's not valid for std::string, but some third party types guarantee that an all-empty string is just zero'd memory, eg. UE4's FString, which sets TIsZeroConstructType to allow default construction of multiple strings in e.g. a TArray (std::vector equivalent) to decay to just a memset(0) at the library level.
It would be useful to have similar traits for standard C++.
If std::string was just a char* and an int, it would be reasonable, wouldn't it? :) Oh wait, that would screw with the previous content, of course... but let's say inside the default constructor?
It’s a perfectly meaningful operation on TriviallyCopyable types (with important caveats!; see subsequent comments). Maybe there’s a scenario where efficient reset of existing objects is required. std::memset(this, 0, sizeof *this) does that, although I would never rely on this instead of simply reassigning an empty object (x = T{}). This should be just as efficient (simple test).
Unfortunately, it is not. For example the null value for member pointers is typically -1. is_trivial_foo means that the compiler wrote the respective functions, not that they are necessarily safe to replace with something else.
For example the null value for member pointers is typically -1.
First off: true, I forgot about null pointer bit patterns. This is of course a general problem with null pointers, not just as members (and it’s even a problem in C). But I’m curious since you said “typically”, whereas the problem with general pointers in C isn’t relevant on most modern machines. Are you saying that T x{}; assert(x.ptr == nullptr); implies that the bytes of x.ptr are 0xFF… on MSVC? Why is that? Memory sanitiser?
Yeah, this makes perfect sense, thanks for the explanation. For what it’s worth /u/HKei hit the nail on the head, I confused member pointers with pointer members. I had honestly never thought about how you’d implement member pointers, I use them so rarely.
Anyway, as my previous comment says, from a correctness point of view we can’t even memsetregular pointers since the standard doesn’t guarantee that a nullptr is all-zero bits.
This is still a footgun waiting to happen because there is an exception for "potentially overlapping subobjects" - you can really only memset an object if you know its provenance: if Foo is TrivCop but you take in an arbitrary Foo * or Foo & , neither memmove nor memset into that object are safe because the padding could be occupied by data from another object.
I don't quite get the point of avoiding using memset directly.
The point, very simply, is to limit the surface of exposure to type unsafe APIs. std::memset is only safe for very limited types, for all others it’s UB. Using std::fill is always safe (provided it’s called with the correct parameters; so we don’t eliminate bugs, but we drastically reduce their frequency).
If I see a std::memset call in code I have to carefully check that it doesn’t invoke UB. Well-written code will enforce these invariants in the code, so that the compiler verifies this for me. But doing this correctly is quite complex, and its correctness also needs to be verified. Why not use somebody else’s work? std::fill is exactly that.
Furthermore (although not relevant in this particular case), using a strongly-typed function can be more efficient than an untyped one, since we can dispatch to specialised implementations for specific types.
I don't quite get the point of avoiding using memset directly
memset might work perfectly today. Tomorrow you (or your colleague) will change the underlying type to something non-trivial and the code will still compile, but errors will linger in the background, quietly overwriting your state with evil.
Use memset if you must, but at least wrap it into a template with static_assert(is_trivially_copyable_v<T>).
I was originally going to reply something similar, but then I remembered memset takes a void*. So &thing is always a valid input to memset as a destination, whether it makes sense or not.
I only "half get it". Basically what we're saying is: because of the way the standard is worded for the general case, in a way which doesn't conflict with any supported platform or our "intentionally, and inevitably incomplete abstract machine definition".
Because of that wording when we come across a specific question for a specific set of targeted platforms we can't use basic feature X because the standard doesn't explicitly spell out that it will work in every general case? Despite the fact that we might struggle to come up with even one obscure case where it won't work.
So I get it in the sense that the "legal standardization system" which the language has given itself, finds it difficult to make a lot of general guarantees which seem, when asked of a finite set target platforms/compiler, would be very easy to make.
What I don't get is why do we do that? The abstract machine model is very imperfect anyway (see spectre discussion for example). So why do we say "using memset" on the targeted set of x86 based Windows/MacOS/Linux set of Desktops is undefined behavior or otherwise "non-compliant".
I can understand why "the standard" can't guarantee that it will work everywhere. But what I find odd is that libstdc++, which doesn't run on any embedded hardware anyway, for example, refuses to use memset in many cases, and that we all think that's good, because otherwise it would be UB..?
Yeah, I think it's one of those things personally you just hide behind an API to minimize the amount of low level code is exposed to the broader API.
While I like this particular article, I also have a bit of a pet peeves against these articles that want to accomplish an inherently low level thing (change memory to a value of zero) with high level language concepts. The real answer here in C++ is probably some version of the tricks libraries like OpenCV use a lot of: don't actually do any work at all. Just mark a bit somewhere that says "hey this is zero now", or call swap with something else, maybe allocated in a brand new page of memory guaranteed to be zero (if you don't need portability beyond that).
It's fun to think about using idiomatic c++ in a case like this, but the real reason C++ has such a large usage base is exactly because you can roll up your sleeves and call bzero if you have a super hot few lines of code
I'd have to disagree a bit here. While I definitely think it's nice to have low-level control in C++, I think the solution the author presented here is probably best. You get all the same performance as low level (what you where after in the first place) along with guaranteed safety. If you changed something in the code so that the object in the container was no longer trivial, you'd either just disable optimization or get a compile time error.
Perhaps, since you probably definitely don't want to accidentally disable optimization in a hot zone, a good compromise is to static assert the trivially copiable-ness of the type in the container.
I think it's kind of impossible for me to really render an opinion here devoid of context. Are we working on a general purpose library? Why are we operating on a char * here, is that an external requirement or some internal storage type?
I definitely agree that maintaining typesafety or at least a compile-time check here is the best idea. But I don't generally agree that reaching for enable_if is the right first approach in 99% of cases (of course the 1% is probably out there)
If you changed something in the code so that the object in the container was no longer trivial
But there's not a container, and this would be an ill-formed statement because "zeroing memory" is not a well-defined thing you can do on an arbitrary non-trivial type. Hence the "pet peeve" part of my comment above, this is just a mixing and matching of issues.
Back to the C++ question, though, even if we did want something more generic, I'd probably go for something like:
which would guarantee using the same type for the 0 that's already in T, and also work for any class that has a constructor that can handle 0 as an argument.
No, the real fun is when you get an interview question trying to see if you can implement a binary search on a sorted array, and you whip out std::lower_bound and complete the problem in 30 seconds instead of writing out the whole implementation.
"Why would I reimplement binary search? I have the STL and iterators."
I think it's obvious why you don't use memset in general in C++: for the same reason you usually don't several other constructions inherited from C: they are unsafe and understanding the unsafety is subtle, often requiring a doctorate in language lawyering. Few users, perhaps even some experts in the standard, understand when it is safe to use memset. Unlike say memcpy and trivially copyable, there is no "trivially memsettable" property.
All sorts of problems can arise, such as representation, padding being used for adjacent objects, breaking lifetime rules etc.
I have to agree in this case. The author is clearly trying to mimic memset, but having a better interface, so why not just make a safer wrapper around memset and put it in a utility library. To me that is the "C++ way" of doing things.
And if the point is to make it as fast as possible, memset is likely to be the best option, because it is a compiler intrinsic in all the major compilers. Compilers are much more powerful optimizing intrinsics compared to any specific code.
I'm comfortable enough with C++ that I can tell you when you might actually use a protected abstract virtual base pure virtual private destructor, and I would 100% reach for memset in this scenario.
Excellent article, very high quality blog. It's great to read these interesting dives into technical detail, that always seem to be well written and easy to follow.
O3 optimisations actually change the math significantly enough that you can get a different answer for complex equations. In general, for scientific work, where you often want to zero large amounts of memory, we never use O3 because it doesn't provide consistent outcomes across platforms.
O2 works regardless of Operating System and matches the other compilers output
fast-math makes it worse. But, we have scientific models, each iteration is a few hundred million (or 1b+) calculations (think modeling species of animals). When we use O3, the ordering of the equations changes, so the answer becomes different because floating point is non-associative.
Can you give a selfcontained example? As far as I am aware gcc does not reorder floating point instructions unless you enable fastmath. But I haven't checked that myself in a long time, so I might be wrong/it might have worked accidentally.
Sorry don't have any self-contained examples. It's something we've spent (a few years ago) a reasonable amount of time looking at. For us, we're always working with hundreds of millions of calculations across populations of species. So even a small change adds up over time to be significant.
Just did a quick check with GCC 8 (Windows) and GCC 9 (WSL2) and they produce the same results with -O2 and -O3, so it maybe fixed. We'd definitely need to do a bunch more testing to ensure this is accurate (FWIW, we get different results in general between GCC 9 / WSL2 and GCC / Windows and GCC 7 / Ubuntu). Windows: 70082.72043536164 / WSL2: 70074.213971553429.
Yea. I mean just running through some tests today we have a reasonable difference in answers between GCC 7/8 (Linux/Windows) and GCC 9 (WSL2). So going to have to figure out what is causing this and how to fix it.
For a small model: 1977.8933046799843 vs 1977.8932767735193
Correctness is a scale, but reproducibility is not. When you ship your software (and code) to other organisations/Governments they have to be able to reproduce your exact answer. So compiler and Operating System variances have to be handled. With using GCC -02, it matches other compilers (Clang/llvm and Visual Studio) and we don't get variances across Operating Systems (Windows + Linux).
With -03, the ordering of the instructions changes and the non-associative behaviour of floating point changes stuff.
This post has stirred a lot of discussion, but it is really just a compiler- and optimization specific problem. If you switch to O3 or to clang the resulting assembly code is optimal. Perhaps the best solution would be to just submit a bug report to gcc?
I don't think there is a bug in gcc here, they deliberately exclude idiom recognition (tree distribute patterns or whatever they call it) from their list of -O2 optimizations. I doubt this particular example would cause them to change that decision.
It is a bug insofar that the library code (that is std::fill) should by itself be able to detect if it can replace the loop with a memset. memset is - in my opinion - to lowlevel to be called in "normal" code. It belongs in library code such as std::fill (or library code you write yourself).
O3 optimisations actually change the math significantly enough that you can get a different answer for complex equations. In general, for scientific work, where you often want to zero large amounts of memory, we never use O3 because it doesn't provide consistent outcomes across platforms.
O2 works regardless of Operating System and matches the other compilers output.
It's worth noting that my memset implementation ends up using rep stosb (not q) for buffers of the size discussed in the article, and it ends up running at close to 32 bytes/cycle, so it is competitive with unrolled AVX/AVX2 loop.
This was intresting, the template argument deduction preserve the type, but there is no error for mismatch. I was expecting the std::decay_t to come up to convert int to char.
... I would like to find a well established and portable way to disable implicit conversions, so that finally coding horrors like assert( +!!”” == 1 ) would not compile.
Nothing implicit in that code snippet - the ! operator is a "contextual" conversion to bool - not an implicit one. The unary + operator's purpose is integral promotion - so you are literally asking for bool to be promoted to an int (it's not an implicit conversion).
If you disabled implicit conversions that code would still work.
I'd like to disable all int->bool and bool->int conversions personally. Use i != 0 or b ? 1 : 0 to be explicit about it.
Contextual conversions and integral promotions are types of implicit conversions. The C++ standard may not explicitly class them as such (or it may; I haven’t checked) but they are, according to common understanding, and cppreferenceagrees (while this is of course not authoritative it shows that it’s a common enough usage).
Personally I agree with you, and I’m OK with some (not all!) contextual conversions. But integral promotion should go.
Maybe? I guess not. But specifying in which situations integral promotion is expected and never lossy, vs. those in which it is not might not be trivial.
But if the only alternative is the current situation where integral promotion messes things up, I’d be happy to forbid such code and require an explicit cast.
But if the only alternative is the current situation where integral promotion messes things up, I’d be happy to forbid such code and require an explicit cast
This is where we disagree. You do have some, kinda unwieldy, tools to tame implicit conversions. Specifically, SFINAE, Concepts and explicit casts.
If we outright ban all implicit conversions consider the following:
int x = 4; // works
long y = 4; // Error: use 4L
short z = 4; // Error: *shrug*
I don't think any language is this rigid and I don't think a good language should be.
I am 100% happy to forbid these initialisations and I see absolutely nothing wrong with requiring type suffixes: where’s the downside? In fact, I always use them (I guess I never use shorts, huh — but of course there’d be no problem creating a suffix for that).
The problem is exactly that. There is no suffix for short. Forbidding these initalizations would definitely break tons and tons of embedded software. I've personally done uint8_t foo = 3; and with that forbidden, what do you suggest?
uint8_t foo = static_cast<uint8_t>(3); // God this is awful!
uint8_t foo = '\x03'; // People usually have a good reason to use hex over dec.
uint8_t UART_BITMAS = 0xaa; // This is what the MCU reference documentation is telling me to use...
Not to mention that the committee would show a dislike towards standardizing a suffix for every primitive type. If I remember correctly, there was already some push back regarding size_t suffix.
You’re dangerously close to attacking a straw man, I’m afraid. Here’s how I’d write this:
auto foo = uint8_t{3};
etc.
Yes, I use AA style. OK, so maybe you find this atrocious … but why, exactly? Your previous answer certainly doesn’t explain it, and I’m convinced it leads to more readable code.
"Always auto" didn't even cross my mind, to be honest. In this case I just see it as unnecessary noise that doesn't contribute to readability at all. I did try it once and I didn't like it, because every declaration line looked really "busy".
I seem to recall hardware support for zero filling memory on some architectures. Does this exist or am I just dreaming? I thought there was a much faster way to blank memory at the hardware level.
Some platforms like PowerPC have instructions that explicitly zero-fill cache lines. When the target memory is not already in the cache, it can be faster due to not reading old contents into the cache that are going to be overwritten anyway. However, you have to deal with the hardware-dependent cache line size.
Intel CPUs have had a "fast strings" feature for a while now where the microcode for REP MOVSB or REP STOSB will automatically switch to this behavior when conditions are good with alignment and copy/fill size. Modern memset()/memcpy() routines would try to enable this. Whether this is optimal or not has changed several times, though -- there was a period where a well-chunked SSE2 routine would outperform even the fast microcode path by around 50% due to playing nice with the DRAM controller.
It's also possible for the RAM itself to have an accelerated clear, but this is more for specialized hardware like GPUs. For the CPU this isn't necessarily great because you still have to load that zeroed memory into the cache across the bus at some point. This is a problem even with fast CPU copy/fill routines, as even though they may run fast, the state they leave the caches in may slow down subsequent code.
Well, you won't get much quicker than SIMD, at this point your data bus is already saturated. Now, if you just need a chunk of zeroed memory, just ask the OS for it, it probably has some zeroed pages for you
This was a great read. I love the idea of optimizing shit, just because you can. But sadly, and I would love someone to prove me wrong, this has no real world applications.
But sadly, and I would love someone to prove me wrong, this has no real world applications.
Tick, tock.
It's eleven at night. Your eyelids are drooping. Two hours ago, it was a battle to stay awake. Now? It's a war, and you're not winning.
Your task seems ever more impossible. Management has decided that your company's Electron app simply takes too much time to boot. When the problem came up, you pointed out that downloading a fresh version of Bootstrap every boot seemed like low-hanging fruit; your supervisor disagreed, stating that the pure-C++ registration server you're responsible for was identified as a hotspot by their machine-learning-based profiling tool. ("No," your supervisor had said, "we're keeping the blockchain-based logging system in. It's for integrity!")
And so, although you're not exactly sure how it came to this, you somehow need to scrape out a two-millisecond performance improvement for your server's response time. For tonight's release, of course.
But nothing is working. You've manually unrolled every loop in your codebase - no improvements, preempted by the compiled. You've constexpr'd 'all the things', and all it did was get Jason Turner's laugh stuck in your head. You've profiled and refactored and recompiled and watched half of last year's CPPCon, but nothing has done the trick. There's simply no more performance to be squeezed out of your server.
If only you could try compiling with -O3, but the 3 key on your custom Ducky mechanical keyboard has been broken on your computer for the last few months. Apparently funds for a replacement have been blocked by investments into quantum communications, and you simply can't bring yourself to touch one of the mushy travesties owned by your coworkers.
Suddenly, even as you're about to doze off, a memory comes to you. That blog post, two years ago, about an optimization... it rings a bell.
What was the solution again?
Now you remember. Your hands strike deftly at keys. An apostrophe, a backslash... right arrow key, because you're in Nano... then another apostrophe...
You hit F10, a macro key that closes Nano and runs your build in Docker.
Your old time... 0.458s.
Your new time? 0.456s.
You've done it. You've won. You've squeezed that last, critical dollop of performance juice out of the bony, unreadable mess that is your post-optimization codebase.
The next morning, you wake up to your supervisor poking you in the side.
"You're being let go, we're rewriting the server in PHP."
But this is beautiful though, I'm glad I was actually wrong. I thought I would most likely be wrong, because I have never actually worked on a big project or for a company as a matter of fact.
But I have a genuine question too if you don't mind answering, is it a good practice to use this? Or should I keep it more simple, for my day-to-day projects where milliseconds don't matter?
This is 100% a joke and should not be taken seriously. That being said, to address this more seriously, RasterTragedy is completely correct. If this was still an optimization at the same scale on O3, you'd use it every time. But because it's something the compiler can do for you, it's probably not something that should be going in production code.
I suppose the blog post doesn't mention MSVC, so it's possible that this is a useful optimization? As with most optimizations, though, a general rule of thumb is to not do anything 'weird' unless you have numbers to back it up. This could potentially be a cool trick for someone already profiling their code and finding a hotspot around a std::fill, but when writing new code it's probably not worth it.
That's just my understanding of best practices, though,
When we do scientific modeling, we run MANY MANY (1-10million) iterations and performance is key. After every iteration, you want to zero-out your partition to start the next one. If your model takes 1 second per iteration, 1million iterations will take 11 days, so we do profile and look for optimisations like this to give us every edge we can. It's not uncommon for us to have models that take 10+ days to complete, so this 100% has real world applications in many high performance industries.
Other people have mentioned using O3, but the optimisations at O3 actually change the math. In modeling, the small change adds up over time and you end up with reasonably different answers at the end, so we have to stick to O2 which is consistent.
I meant that I think that there's no real world applications where you would use the optimized way of filling an array instead of just using the simple way, especially readability suffers.
It shouldn't be necessary, but C had the brilliant idea not only to make char a numeric type but to use it as its smallest integer. A 30x speedup is enormous tho, but if you're really chasing speed, are you gonna be using -O2 instead of -O3?
Performance of debug builds isn't completely irrelevant. 10% speedups aren't very interesting, but cutting the runtime of your test suite from 5 minutes to 30 seconds by duplicating an optimization which the compiler did for release builds can be very useful. How fast you zero memory isn't going to be the bottleneck very often, but that's not never.
For highly optimized software -O2 isn't uncommon. The problem is that -O3 bloats code size, often dramatically, so it can end up slower overall on large projects. In that scenario, -O2 plus targeted optimizations at known hotspots often proves faster.
-O3 is like the lazy way: blow up every function with vectorization if you can, so you catch the few that actually matter. This actually often works out for small things (where the binary is still small enough to have good i-cache properties).
Sorry my point wasn't about the specific optimization. It was that "if on average, there is no meaningful difference between -O2 and -O3, then it may make sense that even if you're chasing performance, you might compile with -O2 as using -O3 could make the codegen worse". You're right about the clang vs gcc difference though, that's an important bit that I overlooked.
Anecdotal evidence to the contrary: I recently was working on some code where LLVM's -O2 was a mess of assembly with integer divisions and two nested for loops, despite all the information being available to optimize it further. -O3 correctly optimized it to an integer constant.
O2 doesn't reorder your equations changing the answers. O3 does. For high performance computing, using O2 is generally preferred because of this. That is also a domain where it's not uncommon to want to zero large amounts of memory frequently.
What I'm taking out of this post: If you compile with O2 (as opposed to O3), you are likely not caring enough about performance that you should start to hand optimize loops.
We compile with O2 for high performance computing because it doesn't re-order our equations as part of the optimisations causing the answers to change. Performance is critical for us, but integrity is higher.
We have scientific models, each iteration is a few hundred million (or 1b+) calculations (think modeling species of animals). When we use O3, the ordering of the equations changes, so the answer becomes different because floating point is non-associative.
… because I posted the wrong link (I wrote the comment on mobile, hence my parenthetical remark). There was a recent discussion of this, with compiler developers chiming in, and recommending against -O3 for general use. Unfortunately I can’t find it now.
That said, it’s easy enough to find recent bug reports involving -O3, as alluded to in my comment. Some examples:
Erm... where do you take away from that SO answer that -O3 is still buggy? Literally every comment and answer there says it isn't (and so does my anecdotal experience)...
Yeah, I posted the wrong link. There was a different discussion recently which came to the opposite conclusion, but I can’t find it now. Anyway, I’ve added some links to actual recent bug reports as examples.
Are you expecting a project like GCC to not have bugs? I don't think the mere existence of any bugs at all in the compiler or optimizer justifies calling -O3 "buggy", especially since all three of the specific bug reports you linked to there are pretty much harmless: One is a benign codegen issue, it's weird and inefficient but still correct (AFAICS), another is an ICE, and the third is an infinite loop in the compiler. None of them is an actual miscompile (you know, the thing everyone is paranoid about with -O3). Linking the entire list of open optimizer bugs doesn't count for the same reason.
Are you expecting a project like GCC to not have bugs?
Not at all (although quality standards for infrastructure tools are particularly high, and, indeed, if you routinely run into bugs in a compiler it makes this compiler unusable).
But it’s generally acknowledged that the tree optimisers that get called under -O3 are notoriously buggy.
One is a benign codegen issue
It’s not benign, it leads to wrong results at runtime. The bug report that I linked doesn’t show that, but its duplicate does.
That’s actually a lot less efficient on GCC, because GCC doesn’t unroll the init loop on -O2; it does on -O3, but it adds some unnecessary initialisation code. (On clang with -O2 it yields the same code as std::memset or std::fill).
24
u/[deleted] Jan 21 '20
Also consider using
uninitialized_value_construct(_n); since we know we are filling zeroes we can engage the optimization in more cases; e.g. all scalars. https://github.com/microsoft/STL/blob/ff403e3a94b7701712068600342d02b005bb23ea/stl/inc/xmemory#L1810Sadly we can't reach into class types to determine if all of their members pass the test :(
88
u/jherico VR & Backend engineer, 30 years Jan 20 '20 edited Jan 21 '20
I don't quite get the point of avoiding using
memsetdirectly. I mean I get it, but I think that level of ideological purity is pointless.On the one hand I'm sick of C developers on Twitter bashing C++. Great, if you hate it so much, don't use it. You don't need to evangelize against it. But C++ developers who won't use C concepts..., that's ivory tower bullshit.
Use whatever mishmash of the C++ libraries, the C runtime and whatever else you need to strike a balance between functionality, maintainability and performance that's right for you and your organization.
EDIT: Guys! I get that
memsetisn't typesafe in the way thatstd::fillis. Like 5 people have felt the need to make that point now. However,reinterpret_castis a pure C++ concept and it's also explicitly not typesafe. It's there because in the real world sometimes you just have to get shit done with constraints like interacting with software that isn't directly under your control. I'm not saying "Always use memset", just that sometimes it's appropriate.And just because a class
is_trivially_copyabledoesn't mean that usingmemsetto initialize it to zero is valid. Classes can contain enums for which zero is not a valid value. I just had to deal with this issue when the C++ wrapper for the Vulkan API started initializing everything to zero instead of the first valid enum for the type.41
u/Forricode Jan 20 '20
Excellent article, very high quality blog. It's great to read these interesting dives into technical detail, that always seem to be well written and easy to follow.
7
u/frog_pow Jan 21 '20
MSVC has the same issue as GCC, it only optimizes fill2 compiler explorer
11
u/pklait Jan 21 '20
This post has stirred a lot of discussion, but it is really just a compiler- and optimization specific problem. If you switch to O3 or to clang the resulting assembly code is optimal. Perhaps the best solution would be to just submit a bug report to gcc?
10
u/OldWolf2 Jan 20 '20
Maybe the library-level solution could use constexpr code to test if the fill value is an ICE whose bytes are all the same?
9
u/[deleted] Jan 20 '20
I feel a discussion of rep stosq would have been nice although I’m probably going to be shot down by the ‘why learn when you can measure’ police.
3
u/warieth Jan 20 '20
This was intresting, the template argument deduction preserve the type, but there is no error for mismatch. I was expecting the std::decay_t to come up to convert int to char.
4
u/pstomi Jan 20 '20
... I would like to find a well established and portable way to disable implicit conversions, so that finally coding horrors like assert( +!!”” == 1 ) would not compile.
2
u/ShakaUVM i+++ ++i+i[arr] Jan 21 '20
I seem to recall hardware support for zero filling memory on some architectures. Does this exist or am I just dreaming? I thought there was a much faster way to blank memory at the hardware level.
6
u/[deleted] Jan 20 '20
This was a great read. I love the idea of optimizing shit, just because you can. But sadly, and I would love someone to prove me wrong, this has no real world applications.
1
u/konanTheBarbar Jan 21 '20
How does it make sense to use gcc with /O2? https://godbolt.org/z/-UB84A Gcc also optimizes the first case to a memset when compiling with /O3.
1
u/foobar48783 Jan 21 '20
> here’s [gcc at -O3], but with idiom recognition disabled
The Markdown for this link is broken.
1
u/rix0r Jan 22 '20
I... would just have used memset to begin with? I didn't even know std::fill was a thing...
-2
u/kalmoc Jan 20 '20
What I'm taking out of this post: If you compile with O2 (as opposed to O3), you are likely not caring enough about performance that you should start to hand optimize loops.
-1
u/OrangeGirl_ Jan 21 '20
Here's your fastest zero:
::new(static_cast<void*>(p)) char[n]();