News for hackers. Stuff that matters.
Re:Armin Ronacher: Collections in C by tptacek on Tuesday November 23, 2010 @07:11PM (#1935928)
Armin seems to have reinvented the BSD queue macros.

Having once had the pleasure of inheriting a codebase animated by CPP-macro collections, allow me to be a voice in favor of running, not walking, from programmers who embrace them. They're tricky, they blow up, they're extremely noisy in the code, and (worst of all) they don't encapsulate, offering new devs a myriad of ways to write tangly hard-to-understand subtly broken code that works directly with the collection structure.

Type safety is entirely overrated. C requires so much deliberation to deploy even the simplest collection that the likelihood of you picking up a foo where a bar was what was provided is minimal, and easily diagnosed. Just use voidstar.

One of the first things I did at that job was to port STLport's Red-Black tree (from <map>) to C code, specialized on voidstar. It worked beautifully. If there are times when you don't want to use a void* collection (or a gossamer-thin wrapper around one), those are also times when you don't want a generic collection library to begin with.

Re:Armin Ronacher: Collections in C by strlen on Tuesday November 23, 2010 @09:16PM (#1936243)
There's also a much cleaner way to have interfaces and implementations in C, without relying on macro hackery.

Why, there's even a whole book on that: http://www.amazon.com/Interfaces-Implementations-Techniques-...

Like Norvig's Paradigms of Artificial Intelligence: Case Studies in Common Lisp and Joshua Bloch's Effective Java, it's one of those books which despite having a specific programming language in the title, is really about programming in general.

Re:Armin Ronacher: Collections in C by tptacek on Tuesday November 23, 2010 @09:20PM (#1936250)
This is my favorite C book ever, I recommend it wholeheartedly, and it recommends voidstars. =)
Re:Armin Ronacher: Collections in C by strlen on Tuesday November 23, 2010 @09:35PM (#1936275)
To those who don't yet have it on their shelves, source is available here:

http://code.google.com/p/cii/source/browse/#svn/trunk

Re:Armin Ronacher: Collections in C by nkurz on Wednesday November 24, 2010 @03:38AM (#1936859)
Respectful of you but not of the book, I have to ask: what do you like about it? My initial reactions were very negative (http://news.ycombinator.com/item?id=1705332). I love K&R, but recoiled in horror at every page I managed to read of this book.

I've written entire macro-based systems similar to those recommended by the author here, and find them very useful. Void pointers are great where they can be used, but I love the efficiency and clarity of the generated code approach.

Re:Armin Ronacher: Collections in C by tptacek on Wednesday November 24, 2010 @11:48AM (#1937813)
I found your objections to this book pedantic.

For instance, in a chapter on string atoms (symbols) --- a concept which virtually no C program in the world takes advantage of, despite the centrality of the concept to Lisp, Python, and Ruby --- and this is the first chapter in the book --- you were shocked by his use of a 43 byte string to hold a number string... because someone might be using that code on a 192 bit machine?

You missed the forest for the trees. If you don't want your code tainted by the number 43, don't write that code. The point of the book is how you structure your code, divide it into subsystems, and present coherent interfaces to the rest of your program.

Re:Armin Ronacher: Collections in C by nkurz on Thursday November 25, 2010 @02:12AM (#1939587)
I'm not bothered by the idea of a fixed length for strings, rather the explanation that it is best to use the number 43 instead of a named constant (MAX_STRING_LEN) to avoid namespace pollution. If the first paragraph I quoted had been standalone, or better as a short comment, I would have been in full approval.

Anyway, I appreciate your response. It's good try to appreciate what others see that I do not. Yes, if you ignore the details of the code and the explanations, there are some good parts. If you look _only_ at the big picture, it's probably a fine book. And I love his clearly prefixed naming conventions. But I think you'd do a lot better reading something like the SQLite source code rather than this book if you want to see examples of good C.

I guess I have to ask: do you feel that chapter 4 on using setjmp() and longjmp() plus some brittle macros for error handling is also good for learners? I thought it was technically very clear but about 40 years out of date as to good practice. Is this a forest or a tree?

Re:Armin Ronacher: Collections in C by loewenskind on Wednesday November 24, 2010 @09:33AM (#1937404)
As I recall, Meyer also recommends voidstars for generic collection implementations to deal with template bloat.
Re:Armin Ronacher: Collections in C by scalyweb on Wednesday November 24, 2010 @12:25AM (#1936590)
This is one of the last books that I'm still hoping will make it to a digital format somehow. I just can't see ordering a new paper copy again.
Re:Armin Ronacher: Collections in C by _delirium on Tuesday November 23, 2010 @09:48PM (#1936303)
I mostly agree (and use void* myself), but I don't 100% agree on the last point. There are a bunch of algorithms that are conceptually generic, but get a significant speed-up if type-specialized to certain types, mostly numeric ones. For example, I think STL's type-specializing sort is a pretty reasonable default, while C suffers from having only a non-type-specializing sort built in, which ends up much slower if you're sorting, say, an array of integers. Same with a bunch of machine-learning algorithms that work on generic comparable types, but work much faster if you type-specialize them to integer or double, the way C++ templates would do. That's the main use case I see those terrible C macro approaches used for. An alternative I've seen is to actually generate the type-specialized variants ahead-of-time using some external templating or macro system, or even some hacked-up Perl script.
Re:Armin Ronacher: Collections in C by cdavid on Tuesday November 23, 2010 @10:12PM (#1936345)
Fast atomic types support is indeed a good reason to do this sort of hackery - that's one of the reason why it is used in kernels. The difference of performance hashmap/vectors/sets between void* and int-specialized can be very significant (easily up to one order of magnitude if not more in some real cases).

I wished there was a language (not C++) which could help for this kind of things. Unfortunately, it becomes difficult very fast. One interesting approach is K as suggested by some FreeBSD hackers, but it never went into production AFAIK (http://wiki.freebsd.org/K)

Re:Armin Ronacher: Collections in C by tptacek on Tuesday November 23, 2010 @11:08PM (#1936470)
I'd question how much of that impact comes from specializing the collection code, and how much of it comes from allocation and locality. The temptation with void* collections is to malloc everything, which is lethal for performance, and one of the very few non-bug things I've come across whose fix actually yields a 10x performance improvement.
Re:Armin Ronacher: Collections in C by cdavid on Tuesday November 23, 2010 @11:23PM (#1936495)
Memory allocation can indeed be amortized with specialized allocator, but that's not what I had in mind. The context I usually operate with is numerical computation, and the indirection cost is very high in those cases, especially when you can access memory in blocks if you use specialized allocator. It would be very hard to do well for sure. It is well known that allocator is one of the main weakness of the STL (one of the reason for the existence of Electronic Arts STL).

I am certainly not advocating doing this in general - I think the need for atomic support in generic collections is quite low (I have been investigating the issue recently to add fast and generic support for sparse matrices in scipy). I am pretty sure the macro, specialized ones used in freebsd (tree/queue.h) and linux (rbtree, list) have been benchmarked to hell, though, and would trust them more than most STL implementations.

Re:Armin Ronacher: Collections in C by dtf on Wednesday November 24, 2010 @04:49AM (#1936951)
Seriously, why not C++? It's just C with some stricter type checking. Pretend it's C, then judiciously grab a pair of angle brackets when you need them. No macros required.
Re:Armin Ronacher: Collections in C by cdavid on Wednesday November 24, 2010 @09:41AM (#1937442)
C++ has portability issue (where portability does not mean supporting g++ and MSVC), poor interoperability, and is much harder to maintain in a distributed team of people with varying ability in the language (e.g. open source).

When those are not issues, C++ is appropriate. Otherwise, it is a pain.