Code makes the world a better place

Building a Simple HTTP Server in Elixir

Introduction

Elixir is a dynamic functional programming language designed for building scalable and maintainable systems.

Elixir is built on the Erlang VM, which makes it suitable for systems that require low latency, distribution, and fault tolerance. It is also used for web development, embedded software, data processing, multimedia processing, and more. The article “Game of Phones: History of Erlang and Elixir” explains the background and history of Elixir and is well worth reading.

Elixir is quite fun to write. After writing some Elixir, I realized that JavaScript and Rust have borrowed many concepts from functional programming. So when learning Elixir from scratch, many FP features—such as Pattern Matching and Enumerable—felt familiar because I had encountered them before.

After reading the official language guide, I felt I needed a small project to get more hands-on experience, so I looked into how to build a simple HTTP server with Elixir.

Continue reading

NCTU Observations and Reflections (3): First-Year Master's (Spring) — Fine Arts Club & Research

Preface

Following the previous post, which covered my coursework in the spring semester of my first year, this time I will talk about extracurricular activities and research. My only extracurricular was the Fine Arts Club. Compared with my undergraduate years—interning everywhere, contributing to open-source projects, and participating in communities—this semester felt much simpler. Because I decided to take my master’s program seriously relatively late, I was already behind when it came to exploring directions and finding a topic. Still, this semester I found a research area that genuinely interests me and also settled on a topic, and I began working on it over the summer.

Continue reading

A Comprehensive Guide to Using WebAssembly

1. Introduction

Let’s start with the basics: what is WebAssembly, and why do we need it?

Today, web technology is everywhere. People spend hours online every day, enjoying all kinds of services. More and more applications are moving to the web, and desktop or mobile apps increasingly adopt web-based implementations (Web Apps) or hybrid architectures (Hybrid Apps). In a sense, web technology has taken over the world.

The web runs on JavaScript (JS). As browsers and JS engines have evolved, we seem to have reached a bottleneck for performance optimization. No matter how fast JS becomes, because it is an interpreted language, it reads and compiles code line by line at runtime. This execution model is inevitably slower than compiled languages such as C++.

Then you might ask: why not use compiled languages like C++ directly on the web? Because whether you compile ahead of time or compile in the browser, there are trade-offs. Ahead-of-time compilation typically produces large binaries; shipping those to the browser can waste network transfer time. If you ship source-like representations and compile in the browser, you may still pay a significant compile time before you can run. With JS, the source files are relatively small (so transfer is acceptable), and the browser can compile incrementally as it reads, so earlier compiled parts can start running sooner without making the experience unbearably slow.

Either way, JS performance has limits. People wanted a way to make web applications run faster in browsers, and that is where WebAssembly (WASM) comes in. WASM is a low-level, assembly-like format whose performance can approach native code—similar to what you get from C++ or Rust. In web development, JS and WASM are used together. The basic idea is: keep general application logic in JS, but move compute-heavy parts into precompiled WASM to significantly improve performance. This way, you get both fast startup (JS) and high performance for heavy computation (WASM).

Next, this post introduces how to develop using JS together with WASM.

Continue reading

NCTU Observations and Reflections (2): First-Year Master's (Spring) — Coursework

Preface

This semester felt like the happiest semester I have had in the past two or three years. It was fulfilling: I took four courses, still participated in the Fine Arts Club, wrote quite a few posts, and found a research direction and topic. Studying CS really is a joyful thing. Broadly speaking, my M1 spring can be divided into three parts: coursework, the Fine Arts Club, and research. This post focuses on coursework.

Continue reading

Converting Pthreads to JavaScript with Emscripten and Performance Analysis

Introduction

Emscripten is a tool that compiles C/C++ to WebAssembly. Under the hood it goes through LLVM. It supports compiling Pthreads: it turns them into JavaScript Web Workers plus WebAssembly. It can even translate OpenGL to WebGL, allowing programs to run in a browser with performance close to native.

The focus of this post is exactly that: converting Pthreads into Web Workers + WebAssembly. I will take an example program and try the conversion in practice. Finding a good benchmark program is not easy, so I wrote a small parallel Pthread program to compute π as the test case.

I will first walk through how to use Emscripten to convert Pthreads to JS. While following the official documentation, I ran into several pitfalls; I also record them here so you don’t fall into the same holes. Then I will compare performance for (1) native C code, (2) Emscripten-generated JS/WASM, and (3) a Web Worker implementation written directly in JavaScript.

Continue reading