I just thought of this. Are there any disadvantages when making your server in Assembly (other than having to learn a bunch of stuff )? Wouldn’t servers be much faster if they were made in Assembly?
Corresponding tweet for this thread:
Share link for this tweet.
Maybe. But the cost is abstraction. Assembly is as close to bare metal you can get, without going into complete binary mode.
The following examples are borrowed from https://courses.cs.washington.edu/courses/cse378/03wi/lectures/mips-asm-examples.html:
A simple program
i = N*N + 3*N
could in assembly be written as
lw $t0, 4($gp) # fetch N mult $t0, $t0, $t0 # N*N lw $t1, 4($gp) # fetch N ori $t2, $zero, 3 # 3 mult $t1, $t1, $t2 # 3*N add $t2, $t0, $t1 # N*N + 3*N sw $t2, 0($gp) # i = ...
However, this isn’t an optimized solution.
lw $t0, 4($gp) # fetch N add $t1, $t0, $zero # copy N to $t1 addi $t1, $t1, 3 # N+3 mult $t1, $t1, $t0 # N*(N+3) sw $t1, 0($gp) # i = ...
See how complicated it gets real fast? Right here, we are only adding and multiplying numbers, nothing fancy at all. Your compiler would catch such an easy optimization, but you might have spend an hour to figure it out. That is what’s meant by abstraction. You sacrifice a bit of speed (sometimes it’s even negligible) in order to not have to write at such a low level.
Man, now I kinda feel bad for people who made games for the NES
Chris Sawyer wrote 99% of the original RollerCoaster Tycoon in x86 assembly. RollerCoaster Tycoon (video game) - Wikipedia
Many web/network applications performance is dominated by IO concerns rather than raw number crunching. There are some unique cases where you can benefit from such optimisation, however they are usually quite rare and quite small in comparison to focusing on other areas like DB query optimisation, caching etc. Dropping down to assembly is a lot of pain and very expensive in terms of development/ maintenance for what is likely to be a tiny percentage of the possible benefits. At the very least it is worth testing and benchmarking to find out what is actually slow in your application design before working on optimising.
There is little to gain by implementing an entire web server in Assembly. A more pragmatic approach is to implement it in a high level language like C++ or Rust, measure the performance and only then, eventually, convert the “slower” parts to Assembly.