0

The Power of Native

 2 years ago
source link: https://www.blinkingcaret.com/2021/09/29/the-power-of-native/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The Power of Native

September 29, 2021 by Rui Figueiredo 0 Comments

I’ve recently wondered if a Raspberry Pi 4 would be capable to work as a digital photo frame that you could just plug into your 4K TV. Raspberry Pi 4 supports 4K 60fps output so in theory this should be possible.

Image of a colourful fish poking out of a hole

The idea was that instead of just slideshow’ing through images, the transitions and the images themselves would be animated with some panning and zooming (similar to the Ken Burns effect) and some “merging” between one image and the next.

My first attempt on this was to do it in the browser. That failed spectacularly. At 4K, and even 1080p, any type of transition or animation would yield single digit fps, not to mention that it would have the CPU working close to 100% most of the time. Clearly not ideal if what you want is to take advantage of your mostly idle Raspberry Pi 4 that you already use for other things.

Going native on the other hand, well, just have a look (it looks better and smoother than the captured video but this can give you a good idea):

Your browser does not support video

I called this little app Mesmerize (github repo here, and get it here as an AppImage). I have this running on my living room TV all the time, although I use 1080p as that is smoother than 4K and I can’t really tell the difference in image quality, I guess the TV does a really good job upscalling. Also, it uses almost no CPU.

From time to time I’ll change the TV’s input to the Raspberry Pi and see some of those pictures I took years ago and never looked at before, like the ones in the video above. It’s good fun.

I ended building Mesmerize with C++ and OpenGL, both of which I had brief experiences with more than 15 years ago never to use them again until now. At the time there as no modern C++ (it was just C++ at the time). It ended up being a pleasant experience (I wanted to know what the hype was about).

That’s not the reason I’m writing this though. You see, a while ago I created a library that allows (more or less seamlessly) running in Node.js code from other languages. I ended up only creating the library that enables running C# from Node.js.

Another thing that I did was, I named the project electron-cgi, which in hindsight was a bad idea because using it with Electron (which in the end is a Node.js process) is just one of its possible uses (I wanted to try to port a windows app to linux with it, which I did, it’s called Memory Ace, and Electron was the easiest tool to achieve that).

That definitely shaped the way the project was received, and I experienced first hand the negativity people have towards Electron.

I think it’s unjustified. I’m writing this in VS Code (which uses Electron) running in Ubuntu, and that’s just an example, there are so many apps that I use daily that are electron based that I suspect would never come out of Windows/Mac if it were not for Electron.

That being said, I use a modern desktop most of the time, so I’m never really worried about battery life or burning my lap.

Anyway, I also have to say that native is better. And, of course it is, it uses a tiny fraction of the resources a non-native approach does.

In some scenarios (like with the raspberry pi here) it is the only way to go.

What I wanted to highlight is that native is much much harder (and take with a grain of salt given that my experience with native is less than with web tech).

Just getting OpenGL to show a triangle is an ordeal, not to mention that besides C++ features like multi-threading (can’t stop everything to load the next image, that would break the animations), event handling (there doesn’t seem to be an out of the box way to register arbitrary callbacks, for example for when the window changes size), I also had to get very familiar with CMake which as far as I know is the only reasonable way of getting something similar to what you get with npm packages.

With CMake you can “add” other projects to your own and use their “targets” and header files relatively easily without having to manually compile and create library files.

All of these things are not very hard to do, but there are a lot of more things you need to understand, and which are arguably more complicated, to be productive when doing native development. Not to mention that you need to be aware of which platform you are targeting most of the time (#ifdef this and that is not fun).

In sum, native is better but it’s also much harder. I do think that for most companies it does not make sense to do native. It’s just too risky and costly. And, the native version probably has a higher probability of being flaky and providing a bad experience than the non-native (just because there are more things to mess up, like memory management).

Maybe an alternative to Electron will come along. Maybe that’s Flutter (why did it have to use dart though?). Maybe there are optimizations that can be done to Electron that lower it’s memory usage to acceptable levels. Or maybe we’ll just get better hardware that make all of these concerns largely irrelevant.

It's only fair to share...

Filed Under: Node.js Tagged With: C++, Electron, Node.js, OpenGL


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK