Software Optimization, malloc, and LLVM
πŸ§‘πŸ»β€πŸ’»

Software Optimization, malloc, and LLVM

Tags
blog
Computer Science
Software Development
Thought Experiments
Learning Rust Programming Language made me realize how awesome the LLVM-based low-level programs are (talking about zig, c, and ocaml). Yeah C too; and yeah - memory safety, no string support, no OOP support - I understand those. And I came upon a lot of developers who tend to hate "C" because of pointers. So let me tell you a story. I have been trying to build the smallest (in terms of executive binary file size in the disc + memory footprint + CPU usage) desktop GUI app as possible and in this scenario each and every byte counts. I can not afford to create variables here and there and use an array of strings or characters and call it a day. I tried to create the least amount of variables I could and even when I had to, I always preferred to reuse the memory location of existing ones instead of a shallow-copied variable. Just for the sake of optimization. And here "C" shines. You have the control for everything with a trade-off of "You have the control for everything". Yes, you indeed have to manually de-allocate your arrays and vectors and strings but you are the driver of it and you can use it everywhere which is a double-edged sword. Very Powerful but be cautious. For example, I had to allocate some memory for my global variable in Rust and borrow the pointer to a function with modification access but it was so inconvenient to do it on Rust. This is definitely a skill issue of mine but even a lot of Senior Rust devs will agree that sometimes the borrow checking pattern does not make sense for some memory-sensitive applications. Which I also faced while solving one of the previous years' "Advent of Code" problems. Do not trust my words, try implementing a "Doubly Linked List" library in Rust. Having access to pointers helped me a lot, lot, lot, lot (one more time, sorry) lot in these situations and I can't imagine developing these on dynamically typed languages such as Python Or V8 (you can say Node but you can't ignore bun.js so). I have already made a Windows GUI app related to network sharing which is 210kb in app size and uses memory as low as 1.2-1.5 MB - yes, for Windows 11 x64. And around 500 KB RAM usage on my 20-year-old i386 Windows XP x86. And the whole docker version is 1MB (including the underlying operating system). Although I can easily shrink it a lot more as low as 200kb using Dynamic Links (C does it automatically, Rust doesn't). Having the always-running thought of not abusing memory forced me to write better and faster apps automatically. I did an AOC challenge back in 2021 where I used Python tuples comparison to differentiate unique patterns, it took me only to write two lines of code to do that. Yesterday I did the same challenge with rust, and this time around my mind automatically told me to allocate a fixed-sized memory and iterate over it just one time to get the unique patterns (For a sliding door problem!!!). And I laughed so hard when I saw that the rust solution was although being 10-20 times longer than Python, the execution speed was almost 50x faster than it. My take from this is, if you are developing something that is "Development Time" sensitive, I understand you choosing Python or V8. But if that's not your concern, and even if that is so, consider using Golang -> which is not as much a system-level language as Rust or c, but still pretty much close. And if you want to learn the concept of Computers, Computer Science, how everything works here, try choosing a System Language, Have fun, trust me you become so addicted to developing the best-optimized software that even you can't retain yourself from being a Super super-efficient Software Engineer and programmer. We really do learn every day, don't we? It is indeed a never-ending process for especially US, the engineers of Computer Science Realm.
Β