The Cost of Libraries

June 12, 2020

I’ve been writing a fair bit of C recently, for fun as much as anything (projects including a basic software rasterizer, a bytecode interpreter and compiler, and basic boot code for x86_64, potentially becoming a small kernel). One of the more interesting aspects of it has been how little I’ve used external1 libraries as compared with other languages.

Some of that might be the problems I’m working on, that they’re quite nice an self-contained in some way. But I found that I was only inclined to use a library when it really paid for itself. If the problem was either core to what I was doing, or small to medium in size, it ended up better to write my own solution. Usually, the cost of implementing things by hand is not as bad as expected, and you can tailor the solutions nicely to the use case. Overall, I think this improves the coherence of software, not just by virtue of being self-contained, but also by forcing reflection on whether the obvious implementation is actually necessary and optimal.

The cost of having to use a library (or write an implementation myself) forced me to consider other ways to solve a problem. Sometimes I found more efficient ways to do things with what I already had2.

There is a degree to which being able to easily install libraries and use them is eroding the overall quality of software. Despite what people suggest, I don’t believe that people are properly auditing these codebases to determine whether their libraries are actually any good. It’s too easy to just import and carry on. A lot of people’s programming is just assembling other people’s work with a bit of glue code in between.

It’s not that the cost of using a C library is all that high, but its enough.

It also reminded me just how much more satisfying it is to build software this way (the professional work I’ve done in the guts of network stacks had this same sense of satisfaction). It’s not that being able to use libraries easily is inherently a bad thing, but there is a good balance to strike. Having a cost involved in that choice helps to make the decision actually happen.

  1. I am keen to create my own collection of libraries for reuse. Code reuse isn’t a bad thing. Use without proper consideration is. ↩︎

  2. By way of example, inside a compiler I thought I needed to maintain a linked-list or vector of memory addresses that would need updating at a later point. I would have to implement those structures, and they would need to be allocated somewhere. However, since those position in memory weren’t being used, instead I could keep a single variable on the stack pointing to the first such address, and then thread the rest of the addresses back through the unused slots (terminating with a null pointer). This probably isn’t quite as clear, but it is an efficient use of resources. ↩︎