8 ms·
Zeroing Memory is Hard: VC++ 2015 arrays
- brucedawson 10y agoApparently gcc and clang are not fooled by { 0 }, so that's good: https://twitter.com/areuugee/status/754888773498867712 https://twitter.com/areuugee/status/754888773498867712
- Kristine1975 10y agoI'd wager they have an optimization pass that coalesces adjacent constant stores.
- AstralStorm 10y agoAmong others. This is likely caught as zero initialisation already in the front-end, before even GIMPLE/LLVM JIT is generated.
- harry8 10y agoWhy is this on the front page? Not very interesting, not very useful, a footnote for the windows vc++ dev team maybe at best. Probably a fair way down footnotes to their list of priorities though.
- eyelidlessness 10y agoI recently stopped regularly visiting Slashdot, and started regularly visiting HN. Besides regularly finding more appealing content, I have found that typically HN comments are less hateful, disparaging and isolating. One (minor) version of this distinction is the constant debate on Slashdot about what should be published, and what should not. There, an assumed standard is expected for something to be considered "news for nerds" or whatever. For the most part I haven't seen that here. I've appreciated that fact. Because fuck me, if you can't just pass by a link among thirty or so, give up and stop using the web. It's full of shit you don't think belongs there. Just stop shitting on things I want to read just because you don't want to read it too. [Edited because upon re-reading, I really wished it had line breaks.]
- TruthAndDare 10y agoI recently returned to HN, having read it a little a few years back. I remembered it as a low-volume, high-quality feed, where I was hesitant to speak, because all the other comments were so full of knowledge. Now, HN seems to have become like any other place, full of noise like politics, social commentary, and just regular news. I guess the commenters are still knowledgeable about technological subjects, though.
- deleted 10y ago[deleted]
- 72deluxe 10y agoOut of interest, where would you go for news that was like the old HN? I used to really enjoy ArsTechnica but the quality of the journalism seems to have dropped slightly.
- iand 10y agoPerhaps https://lobste.rs/ https://lobste.rs/ might be an addition to your reading habits?
- lmm 10y agoIs michaelochurch still getting upvoted there? I used to enjoy lobste.rs, but seeing yet another heavily upvoted rant about "open-plan agile mouthbreathing drones" or similar would ruin my whole morning.
- DanBC 10y agoJudge for yourself: https://lobste.rs/threads/michaelochurch https://lobste.rs/threads/michaelochurch
- Etheryte 10y agoThe inevitability of every good site is that it will eventually grow, and with it, the diversity of the interests of the user-base. While I come to HN mainly for the technical news, I find it convenient that it also keeps me in the loop about a wider variety of topics I would surely miss otherwise. Your preference may obviously vary.
- CamperBob2 10y agoAlso note that you can't zero-initialize an array of objects that don't have a default constructor, and that if you just add an empty default constructor to shut the compiler up, the memory occupied by your array will not be initialized to zero or anything else. Seems obvious and elementary enough, but it got me good the other day.
- AndrewGaspar 10y agoIf you need to manually define a default constructor, and you want the behavior of default-initializing all members, declare the constructor as: T() = default;
- typelist 10y agoExplicitly defaulting the constructor behaves the same as an empty constructor: all members are default-initialized. The problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized.
- CamperBob2 10y agoThe problem is usually that 'default-initializing' doesn't zero out primitive (POD) types, but leaves them uninitialized. Exactly, that's what hosed me. Some double-precision floats ended up with garbage values and I couldn't see how it was happening.
- gpderetta 10y agoExplicitly using the T(), T{}, T = {} syntax will value-initialize the object, which will zero-initialize trivially constructible types.
- AndrewGaspar 10y agoYes, forgot to mention this crucial detail.
- 10y ago
- cleeus 10y agoAt this point we must ask which Update of VS 2015 as MS just replaced the Optimizer with an SSA based one. https://blogs.msdn.microsoft.com/vcblog/2016/05/04/new-code-optimizer/ https://blogs.msdn.microsoft.com/vcblog/2016/05/04/new-code-...
- brucedawson 10y agoA good point, but already answered in the article: > I recently noticed that it’s still an issue in VC++ 2015 Update 3
- gratilup 10y agoI'm the main developer of the new optimizer. It's a bit too much to say it replaces the old one, it's more of an addition. I was aware of this issue with initializing local arrays, it was on the TODO list - hopefully for the next VS.
- voltagex_ 10y agoAre you allowed to respond directly to the author in this case?
- apardoe-MSFT 10y agoThere's not much preventing any engineer at Microsoft from talking to developers. No lawyers need to be involved :-)
- ryuuchin 10y agoWhy isn't the ssa optimizer mentioned in the Update 3 release notes? Is it enabled by default in Update 3?
- brucedawson 10y agoI look forward to a fix, especially if the (I think) larger issue of large aggregates not being initialized efficiently is addressed. It feels like it is another instance of the same issue. I hope the Python script is helpful for finding some of the odd variants that currently exist.
- someguydave 10y agoFar worse is trying to grow a std::vector with a std::vector.resize() and subsequently trying to opt out of array intialization.
- andreyv 10y agoThen you might need std::vector::reserve() instead.
- brucedawson 10y agoBut reserve() doesn't work if you are going to fread() into the buffer or otherwise fully initialize it. std::vector<char> basically requires you to double-initialize the buffer if you pass it to a function that just needs a raw pointer to fill, which can be an issue in some high-performance contexts. I hit this in a game server where a few percent of CPU time (that was enough to count as low-hanging-fruit) was being spent on memset inside of resize().
- honkhonkpants 10y agoWhat was the solution? Using a custom allocator?
- deadmutex 10y agoI am not OP, but you could get around it by having a default constructor that is empty. This might've not been possible if you need a different default constructor that initializes values.
- gpderetta 10y agoUsing a custom allocator is the standard-blessed solution for this problem, although it can be inconvenient.
- someguydave 10y agoYes you could use a custom allocator except that changes the type of the std::vector - making a large ripple in our code base. I have not solved it yet. The best solution - don't use std::vector for anything critical.
- bertan 10y agoOMFG!
- userbinator 10y agoIf you ask the compiler to optimise for size you should really be getting a REP STOS for anything that needs more than the ~3 instructions which that takes. (It's not the fastest way for small sizes --- it actually is for larger sizes, just like REP MOVS --- but the user asked for smaller, not faster code.)
- deleted 10y ago[deleted]
- kazinator 10y ago= { 0 } is an "initialize almost anything" idiom in C; it's worth recognizing specially in a compiler.
- apardoe-MSFT 10y agoBruce, thanks for the kick! Six years is a long time to wait for a bug resolution. It turns out that our compiler has a minimum size limit in a memset optimization. I’m sure the size limit was there for a Very Good Reason (TM) at one point in time, but we are investigating whether we can remove it. Step one is understanding why it was there in the first place.