drshapeless


shapeless-blog is getting rusty

Tags: go | rust

Create: 2023-05-27, Update: 2023-05-27

I originally created shapeless-blog in Go, which was simple and fun. Although not having any performance issues, I was not satisfied that Go is fundamentally a garbage collected language, which means I don't have full control over the memory management. Since the end of 2022, I have been planning to port shapeless-blog into Rust. And after a few monthes of silence, now is the time.

Personal update

I have been busy in the past few monthes. There were a lot of things going on in my life.

I dated a girl and got a girlfriend. That requires quite a lot of confidence to a nerd like myself.

I started trading options, and lost quite a portion of my fortune, fuck that.

My workload in the workplace has increased significantly. I do not have much concentration to work on the things I like.

I have significantly less time with the computers. The work as a clinical medical practitioner is demanding.

The thing that did not change is that I am still a Emacs user. With most of the things being settled, I squeezed out some time to work on some hobby project.

Why switch to Rust

shapeless-blog was originally written in Go, shapeless-blog-go. It works fine, and has a very minimal memory footprint. It is fast. Writing in Go is such a joy as it is a very simple language. Go programmer also has the tendency to use as little external libraries as possible, which is very uncommon in nowadays world. (Considering the JS dev who imports the leftpad dependency.)

The only thing I am not satisfied with is the garbage collector. The garbage collector of Go is great. But the existence of the garbage collector is enough to upset me. I want to have full control of the memory management.

And the trigger point is the encounter of this article by Discord. Although in any possible future, I am not facing any of the issues that Discord had. How could a tiny little blog have performance issues at all.

As a personal project, I do what I like. I like to switch to Rust, so I did.

Under-estimation

I have never written Rust seriously in the past. The only useful Rust project I have ever created is a helper tool epub2cbz, which extracts all the images in a epub file and export it into a cbz format, just for me to read comics more pleasantly on my Kobo e-reader.

I would regard myself as a Rust programmer who just finished the hello world stage. I was confident that given the previous experience in Go, the shift to Rust should not have a steep learning curve.

I was wrong. I was so wrong to a point that I almost gave up twice.

First, I wanted to give up when I see the ocean of different libraries in Rust that do the same thing. With a Go programmer mindset, I thought I just need a router libraries to start web backend programming in Rust. Wrong, I need to have a async library first, which is tokio. I got lost. I started to read some books about Rust web programming. Most of them recommended using the Actix web. I follow the tutorials, but I would like to try out the latest version of the library. However, the Rust library moves so fast that the tutorial written a year ago cannot even compile.

I dived into Reddit, discovered that a lot of people recommend using Axum and sqlx. I settled with these two as the backbone of my project, just to find out that there are almost no step-by-step tutorials at all. I finally found out this repo, realworld-axum-sqlx. Later, I was so lost in the error handling.

This is the second time I almost gave up. The error handler in Rust is so different from Go. What the fuck is "result"? What the fuck is "match"? What the fuck is "?"? (Yes, the "?" operator almost killed me.) I later discovered anyhow, was overwhelmed by the complexity of it, abandoned it in the end. I rewrote my whole project to just make error handling no looking like a mess.

I expected the borrow checker would be the biggest pain in the ass, but turns out that, it almost never causes any issues except I would have to clone some strings to pass around different functions.

The type system is very great. The compiler error is excellent.

Result

After spending a few weeks learning and writing, and thanks to the existence of ChatGPT (it has answered me so many stupid questions), I finished porting shapeless-blog from Go to Rust.

However, this is not an identical port. Some features were added, like multiple users. Some were removed, like the on the fly templates updating.

But despite stripping out a lot of functions, the binary size of shapeless-blog has increased from 7.9M to 13M. This may be due to the better logging system, embedded migrations and templates. I doubt that it may also be because of the shift from SQLite to PostgreSQL.

Next Rust project

I have a personal project which uses Go as a backend API and Svelte as the frontend. I am planning on porting it into pure Rust using leptos. I am sure this is not going to be easy, but should probably be fun.