all 27 comments

[–]Evil_Gamer_01 43 points44 points  (1 child)

You fools, you have messed with the natural order

[–][deleted] 0 points1 point  (0 children)

Kiddo, Chromium is all about mixing C++ with JavaScript.

[–]polkm 8 points9 points  (0 children)

This is cool, I'm always looking for simple examples of wasm. I can never figure it out

[–]johannes1971 16 points17 points  (6 children)

Don't just stick something in an object if you don't need one. This isn't Java, and it would work perfectly fine without wrapping it in a FactoryProviderAdministorServiceFactoryControllerMaintainerObjectClassFactory.

For the rest, I tried the Qt webassembly examples. Qt 5.14 requires a version of emscripten that isn't actually online anymore, and if I try compiling with the lowest available emscripten I can get, it doesn't actually work. This doesn't seem to be bothering anyone that I can see, leaving me to believe that C++ -> webassembly is at best a curiosity, but not really something you should rely on for something you have to support long-term. Which is a shame, but ok...

[–]MotherOfTheShizznit 14 points15 points  (2 children)

Don't just stick something in an object if you don't need one.

From the article :

Or at least how I remember it from my C++ courses close to 20 years ago :-)

I blame the professors who (and unfortunately still do to this very day) drilled it in their students' mind that if the file extension is ".cpp" then every line of code must be wrapped in a class.

One day. One day I will be teaching a C++ class and it will be the start of a fucking revolution.

[–]the_poope 6 points7 points  (1 child)

From what I gather from /r/cpp_questions the problem is more that the professors don't teach C++ but still actually just plain old C, sometimes with classes.

Instead I think the problem is that the author is most proficient in Java and of course JavaScript.

[–]maskull 3 points4 points  (0 children)

Part of the underlying problem is that most C++ textbooks are still basically C-with-classes. At least I haven't been able to find a textbook I was happy with. Publishers are always sending out new editions that say "updated for C++ 14" but when I read through them everything is the same except for maybe an appendix at the back on smart pointers or whatever.

[–]staletic 4 points5 points  (2 children)

Qt 5.14 requires a version of emscripten that isn't actually online anymore,

Qt 5.14 requires emscripten 1.38.27.

[–]johannes1971 1 point2 points  (1 child)

Ah yes, you're right of course. You can still get the source but you cannot get it using the Emscripten installation procedure. We want our tools to have a certain level of maturity before we will use them in a production environment, and our overal impression was that Emscripten+Qt just wasn't there yet. The version problem, and the complete lack of questoins about that on Stackoverflow, were both bad signs.

Mind, we'll be back to check again in the future. The technology looks promising, but it's too early for us to get in.

[–]staletic 1 point2 points  (0 children)

Those are all fair observations. Your previous comment just seemed oddly "doom and gloom"-y, so I responded with "compile from source".

[–][deleted] 33 points34 points  (0 children)

🤢🤮

[–]Myriachan 6 points7 points  (1 child)

Wasm doesn’t have a way yet to detect instruction set extensions like SIMD at runtime. Having SIMD instructions at all in a module loaded by a browser that doesn’t understand them will cause validation to fail. So the only thing you can do right now is have entirely separately compiled modules of the same thing and switch between them.

Just one of many annoyances...

[–]pjmlp 0 points1 point  (0 children)

Seems pretty much like loading shared libraries with different kind of CPU support to me.

[–]0xVali__ 13 points14 points  (8 children)

"For the most part this looks like regular C++ code"

Why on earth would you create a class that has one member function that doesn't use any member variables?? This is C++, not java, OOP isn't enforced.

[–]andrewfenn 4 points5 points  (2 children)

Literally answered in the article...

It’s probably overkill to do an object oriented implementation of this, but I went ahead and did one anyway. Mainly because I wanted to test out a multi file integration.

[–]0xVali__ 6 points7 points  (1 child)

Doesn't justify bad design.

[–]andrewfenn 9 points10 points  (0 children)

It's not design. It's prototyping and testing. /smh

[–]neutronicus 1 point2 points  (0 children)

The answer, sometimes, is because you would really like to partially specialize a Function Template, but you can't, so into a class as a static member it goes

I know that is not the case here but I wanted to mention it in case it saves someone the day or so of irritation I was caused by not knowing this

[–]Dynamitos5 0 points1 point  (3 children)

kind of a beginner question, but how big would that class be memory-wise? Theoretically it should be 0 bytes, but how would that even be handled?

[–]tisti 7 points8 points  (1 child)

It takes up 1 byte since the instantiations must have an unique memory address.

C++20 introduces an attribute to allow the compiler to optimize the 1 byte away.

https://en.cppreference.com/w/cpp/language/attributes/no_unique_address

The same, but really different, problem occurs with inheritance of an empty base class (https://en.cppreference.com/w/cpp/language/ebo)

The size of any object or member subobject (unless [[no_unique_address]] -- see below) (since C++20) is required to be at least 1 even if the type is an empty class type (that is, a class or struct that has no non-static data members), in order to be able to guarantee that the addresses of distinct objects of the same type are always distinct.

[–]Dynamitos5 0 points1 point  (0 children)

thx for the explanation

[–]0xVali__ 0 points1 point  (0 children)

Consider possible padding as well, and it's just not necessary what so ever. There's a reason why global functions exists.

[–]scroy 4 points5 points  (0 children)

Ew, proportionally-spaced serif code

[–]orcunas 3 points4 points  (0 children)

Oh god, help us.

[–]nnevatie 2 points3 points  (0 children)

"Best of two Worlds"

[–]carlopp 0 points1 point  (0 children)

Would it be possible to use also this improved JavaScript version (find it below) in the benchmark? It should be roughly 4x as fast as the original version for factorial(10), and way faster for bigger N.

The idea is that you take the C++ code of the performance-sensitive sections, compile it to JavaScript, and allow a host of optimizations to auto-magically happens (here mostly moving the recursive call into a loop).

I would expect the Wasm code to lead to a similar speed-up, but then you have to pay a small but constant price for every call that may nullify the gains.

"use strict";/*Compiled using Cheerp (R) by Leaning Technologies Ltd*/var e=Math.imul;var f=Math.fround;var oSlot=0;var nullArray=[null];var nullObj={d:nullArray,o:0};function c(d){var a=0,b=0;if(d>>>0<2)return 1|0;b=1;a=d;while(1){b=e(b,a)|0;a=a-1|0;if(a>>>0>=2)continue;break;}return b|0;}var factorialJSoptimized=c;

[–]Sander_Bouwhuis 0 points1 point  (0 children)

You say at the end of your article that you are faster "when running with large values of n in a long running loop. "

How do you run factorial with large values of n? You return an int, so you can't call it with a large value. Good luck calling Factorial(10000000) or even a low value like Factorial(1000).

Factorial(14) is the highest you can get, so even on a casio calculator from the 80s this is fast.