Tuesday, October 11, 2011

What is wrong with using the STL?

Why would anyone advise against using the STL? This question is often posed and my response is generally the same:

"it's fine for a regular OS but bad for an RTOS"

Here's why:
* Memory management. The stl will positively thrash memory. If you are worried about memory management, or have a low level OS like VXWorks, ThreadX, QNX, or any of the gaming OSs like XBox, PS3, or WII, I can positively say DO NOT USE THE STL. It works fine for a while, but performance degrades as memory fragments and eventually you will crash. This is true of most of the containers except vectors. You can work around this with custom allocators, but if you need to write that much code anyway (allocators are not trivial) then writing a simple container is very easy.

* Debugging. Things go wrong and it's never the fault of the stl, but finding where you went wrong is the nature of debugging. Debugging the stl is difficult at best. Also, many programmers do not understand design patterns and decorator is a tough onion-like pattern on which the stl is built. I have heard some people say that it's easy but hearing that is extremely rare.

* Naming. Why they names the array class a vector I'll never know, after all... there is the valarray. The set is a good name, but multiset is a weird name considering what it does. The unconventional naming makes it ugly to explain to beginners. Then there is the hash_set, hash_multiset, hash_map, and hash_multimap. Even senior programmers have a hard time remembering what all of these do.

* Performance. While the priority queue is fast, it is easy to beat using a combination of arrays and a binary tree. At my last job, we beat the stl implementation by over 10x. The sort algorithm is nice, but this is bad for large data sets and it's better to use a heap sort to divide the data into smaller datasets and use binary sort for the last mile.

* Missing algorithms. Where is your binary tree... I mean come on? There isn't much in comp sci more common than a binary tree. Boost fills in a lot of holes thankfully, but the stl offers very little.



* iterators. This is possibly the most difficult concept to explain to newbies that I've seen. They always get it wrong. If you have junior programmers, then iterators will be a constant headache. I've seen this at three companies, so apparently it's a problem.

Again, I am a fan. But there are definitely reasons to not use the stl and I have yet to work on a realtime OS where it's used. 

On Wiki, we find the following criticisms:
http://en.wikipedia.org/wiki/Standard_Template_Library#Criticisms

No comments: