Just my 2ct: I'm not sure it's worth the trouble to optimize for that specific case which is fairly exotic IMHO (a giant dynamic array of a small built-in type). As soon as the type is not a simple builtin type and has a constructor, it will be slow on native platforms as well since it needs to run the constructor 48 million times.
I did a quick test with clang on OSX, and there's indeed no initialization loop over the array elements, only a single allocator call, so I guess this does the 'get zero-initialized memory' trick, but to me this is an under-the-hood optimization of that specific platform that shouldn't be depended on (the C++ standard only says that elements will be default-initialized, which in the case of built-in types yields 0, but implementations are free how this is achieved). As I said, just my 2ct :) -Floh. Am Mittwoch, 18. Mai 2016 18:13:46 UTC+2 schrieb Alon Zakai: > > Ok, looks like there are 2 issues here. First, IE and Edge might have a > specific bug, worth filing that for them. > > Second, native code can avoid initializing memory, as modern OSes have a > way to just get an already-zero-initialized page. That means allocating > zero'd memory can be essentially free. In JS though we have to actually > zero out the memory. > > There is actually one case where we don't, the first time memory is used, > it is guaranteed to be zerod out since that's how typed arrays start. We > might be able to add hooks into dlmalloc (or however it requests those > pages), but i'm not sure offhand how easy it would be. But, it would just > be for the first use of the memory, so in a long-running app it would not > help. I filed https://github.com/kripken/emscripten/issues/4334 > > On Wed, May 18, 2016 at 7:32 AM, 'Sören König' via emscripten-discuss < > [email protected] <javascript:>> wrote: > >> hmm this would explain the different timings. but 2 seconds to zero out a >> larger bunch of memory?!? is the resulting javascriptcode really so slow? >> here are some timing on a native build on the same machine: >> >> malloc/free: 0.0002104s >> vector ctor: 0.019273s >> vector resize: 0.0157814s >> vector reserve: 0.0001271s >> >> this is about 20 times faster than firefox and 100 times faster than IE >> for the vector ctor. >> >> >> Am Mittwoch, 18. Mai 2016 14:14:07 UTC+2 schrieb AlainC: >>> >>> For malloc and reserve (with an initially empty array), there is no >>> initialization of the elments in the array. >>> For std constructor and resize, each element will be initialized >>> individually (to 0) in a loop I think. >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "emscripten-discuss" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "emscripten-discuss" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
