Live data from Hacker News

A quick look at the Redis source code

heychinaski.com

31–34 of 34 posts

Re: A quick look at the Redis source code

#31

I'm certainly novice in C, but as I was reading, I wondered about this {"get",getCommand,2,"r",0,NULL,1,1,1,0,0}, "The fourth field, set to "r", is specifying that the command is read only and doesn’t modify any keys’ value or state. There are a whole bunch of one letter flags that you can specify in this string that are explained in detail in the nearby block comment. The field following this string should always be…

In defence of the technique, the command table is quite succinct and arguably more readable at a glance than if there were a bunch of constants |ed together. I have no idea whether this was the original motivation though.

Yes. It is certainly more pithy.

ACTUALLY! It reminds me of a technique Bisqwit used when he made his emulator. He used strings to define the behavior of certain instructions, the strings were actually interpreted at compile time. Though I think this is a C++ specific trick.

http://www.youtube.com/watch?v=y71lli8MS8s

he brings in the instruction table at 1:30

Re: A quick look at the Redis source code

#32
post #25
post #8

Redis is my favorite example of a very clean and beautiful example of C code, just the right amount of comments, good variable names. It is great work. You can tell Salvatore cares and has passion for what he does just by looking at his work. https://github.com/antirez/redis/tree/unstable/src

What are you comparing that with? I find it fairly normal for C code dating from past, say 2000. Also, one thing that made a negative impression on me is https://github.com/antirez/redis/blob/unstable/src/adlist.c : /* Free the whole list. * * This function can't fail. */ void listRelease(list *list) { unsigned long len; listNode *current, *next; current = list->head; ... If you write such a comment, you better make…

Compared to an average of other projects coded in C out there.

Others mentioned kernel, GNOME. Redis is nice because it is fairly self contained. It has a good set of networking code, some command parsing, storage. Kind of a happy medium.

Anyway, I didn't look in depth at understanding the semantics of every module.

Here are some more things (besides NULL check I can see that are worrisome in that particular piece of code):

* can len be out of sync with actual list length

* can list->free be defined (non null) but value wasn't allocated with an malloc perhaps

* what if an element is inserted twice in list, will list->free be called on already free-ed area

* on 64 bit machine unsigned long will be 64 bit and on 32 bit it will be 32 bit, is that problem?

Which ones do you see?

Now those are issues I see by looking at the module in isolation. But it is not quite an isolated. It is part of a large module. Sometimes there are invariants that are enforced at the input (at the system boundary) and so it is not necessary to always keep checking for NULL or validating inputs in every single internal function.

That is one good lesson I learned from Erlang. Check your inputs at the boundary then code for the "happy" path and let error result in quick and early failure. Maybe prefer a quick segfault rather than a dangling pointer or wondering later how exactly to handle NULL pointer if it is not really expected to be NULL.

Re: A quick look at the Redis source code

#33
post #21

Earlier quoted context omitted.

"And quite frankly, I stumble upon badly written Python way more often than C." I'm curious, which public codebases would you consider to be examples of well written Python?

The often cited requests library, flask and Django. Some counter examples are high profile projects such as IPython and Matplotlib.

Thanks. I'll spend some time with Requests over the weekend. It looks like the person who maintains that library is also behind this http://docs.python-guide.org/en/latest/#writing-great-code
Post reply on HN