Article note: #niceseries

This is the 1st article in a 3-part series:

  1. A crash course in memory management
  2. A cartoon intro to ArrayBuffers and SharedArrayBuffers
  3. Avoiding race conditions in SharedArrayBuffers with Atomics

To understand why ArrayBuffer and SharedArrayBuffer were added to JavaScript, you need to understand a bit about memory management.

You can think of memory in a machine as a bunch of boxes. I think of these like the mailboxes that you have in offices, or the cubbies that pre-schoolers have to store their things.

If you need to leave something for one of the other kids, you can put it inside a box.

A column of boxes with a child putting something in one of the boxes

Next to each one of these boxes, you have a number, which is the memory address. That’s how you tell someone where to find the thing you’ve left for them.

Each one of these boxes is the same size and can hold a certain amount of info. The size of the box is specific to the machine. That size is called word size. It’s usually something like 32-bits or 64-bits. But to make it easier to show, I’m going to use a word size of 8 bits.

A box with 8 smaller boxes in it

If we wanted to put the number 2 in one of these boxes, we could do it easily. Numbers are easy to represent in binary.

The number two, converted to binary 00000010 and put inside the boxes

What if we want something that’s not a number though? Like the letter H?

We’d need to have a way to represent it as a number. To do that, we need an encoding, something like UTF-8. And we’d need something to turn it into that number… like an encoder ring. And then we can store it.

The letter H, put through an encoder ring to get 72, which is then converted to binary and put in the boxes

When we want to get it back out of the box, we’d have to put it through a decoder to translate it back to H.

Automatic memory management

When you’re working in JavaScript you don’t actually need to think about this memory. It’s abstracted away from you. This means you don’t touch the memory directly.

Instead, the JS engine acts as an intermediary. It manages the memory for you.

A column of boxes with a rope in front of it and the JS engine standing at that rope like a bouncer

So let’s say some JS code, like React, wants to create a variable.

Same as above, with React asking the JS engine to create a variable

What the JS engine does is run that value through an encoder to get the binary representation of the value.

The JS engine using an encoder ring to convert the string to binary

And it will find space in the memory that it can put that binary representation into. This process is called allocating memory.

The JS engine finding space for the binary in the column of boxes

Then, the engine will keep track of whether or not this variable is still accessible from anywhere in the program. If the variable can no longer be reached, the memory is going to be reclaimed so that the JS engine can put new values there.

The garbage collector clearing out the memory

This process of watching the variables—strings, objects, and other kinds of values that go in memory—and clearing them out when they can’t be reached anymore is called garbage collection.

Languages like JavaScript, where the code doesn’t deal with memory directly, are called memory-managed languages.

This automatic memory management can make things easier for developers. But it also adds some overhead. And that overhead can sometimes make performance unpredictable.

Manual memory management

Languages with manually managed memory are different. For example, let’s look at how React would work with memory if it were written in C (which would be possible now with WebAssembly).

C doesn’t have that layer of abstraction that JavaScript does on the memory. Instead, you’re operating directly on memory. You can load things from memory, and you can store things to memory.

A WebAssembly version of React working with memory directly

When you’re compiling C or other languages down to WebAssembly, the tool that you use will add in some helper code to your WebAssembly. For example, it would add code that does the encoding and decoding bytes. This code is called a runtime environment. The runtime environment will help handle some of the stuff that the JS engine does for JS.

An encoder ring being shipped down as part of the .wasm file

But for a manually managed language, that runtime won’t include garbage collection.

This doesn’t mean that you’re totally on your own. Even in languages with manual memory management, you’ll usually get some help from the language runtime. For example, in C, the runtime will keep track of which memory addresses are open in something called a free list.

A free list next to the column of boxes, listing which boxes are free right now

You can use the function malloc (short for memory allocate) to ask the runtime to find some memory addresses that can fit your data. This will take those addresses off of the free list. When you’re done with that data, you have to call free to deallocate the memory. Then those addresses will be added back to the free list.

You have to figure out when to call those functions. That’s why it’s called manual memory management—you manage the memory yourself.

As a developer, figuring out when to clear out different parts of memory can be hard. If you do it at the wrong time, it can cause bugs and even lead to security holes. If you don’t do it, you run out of memory.

This is why many modern languages use automatic memory management—to avoid human error. But that comes at the cost of performance. I’ll explain more about this in the next article.

Article note: .oO(Kann man das vllt mit der Kubb-WM iwann mal verbinden …?

In Schweden haben sie jetzt ein Museum of Failure, in dem allerhand technologische Ideen ausgestellt werden, die sich dann am Ende doch nicht durchsetzen konnten. Schöne Sache, das.

Welcome to the new Swedish museum that takes an intellectual look at the biggest gadget flops of all time in an attempt to figure out what we can learn from them.

(Direktlink)

Article note: #superwerwaltung!

Vor einem Monat stellte die Aachener AfD-Ratsfrau Mara Lux eine Ratsanfrage. In dieser ging es ganz konkret auch um die Unterstützung der Stadt an die Antifa. Zwischen die Zeilen kann man die Frage nach dem sagenumworbenen Demo-Geld für Antifaschisten interpretieren.

Mittlerweile gibt es die Stellungnahme der Verwaltung in Aachen und die reagiert auf die Frage zur Antifa wie folgt und durchaus auch sehr plausibel.

Ob man das so bei der AfD in Aachen verstanden hat, ist bisher nicht geklärt. Hier die komplette Stellungnahme der Verwaltung.

Article note: #software2remember

There is new desktop app available in the Snap store: Vectr.

A free vector graphics editor for the Web and the Linux desktop

Following the Inkscape snap, there is a new vector graphics editor readily available to install. With project sharing built-in, a modern UI built with Electron and more on the roadmap (real-time collaboration, versioning…), you should keep an eye on Vectr!

Out of beta 9 months ago, it features a growing set of tools: shape tools, raster import, filters, and pretty much everything you need to get started on vector graphics with an easy to use interface.

To install Vectr as a snap, search for “vectr” in the Ubuntu Software Center, or on the command line:

sudo snap install vectr

Vectr also comes with a built-in tutorials viewer!

Cross-distribution on Linux

The mission statement of Vectr is to make graphic design accessible and simple for anyone, for a community of pro and amateurs around the world. Distributing Vectr as a snap takes this philosophy one step further, by enabling a simple app distribution mechanism for most Linux users.

Snap packaging was a perfect match for us.

Source: Vectr Launches Ubuntu Snap App – Vectr blog

With Arch Linux, Debian, Fedora, Gentoo, openSUSE and Ubuntu as part of the supported distributions, and a central store model, snaps take the headaches of Linux distribution away.

Vectr as a snap on Fedora, here used by not-a-designer.

Releases for everyone and releases for testers

Snaps also allow developers to release software in different “channels”, that users subscribe to (defaulting to the stable channel), in order to receive automated updates.

Four channels are available, with names hinting at the stability users can expect:

  • edge is for QA, testers and adventurous adopters
  • beta is where versions from the edge channel are moved to when they pass some level of testing and QA
  • candidate is commonly used for freezed pre-release versions
  • stable is what users install by default (the snap install command without any options) and is expected to only contain stable software. This is also the channel that enables snaps to appear in store search results.

For a primer on snapping an application, this tutorial will show you the way.