Live data from Hacker News

What can you do in 2k LOC of C?

h4ck3r.net

31–40 of 110 posts

Re: What can you do in 2k LOC of C?

#31
post #27

Earlier quoted context omitted.

That's interesting, but damn is that some ugly code. Slightly obfuscated on purpose? Though of course it wouldn't win the IOCCC. On the other hand, it's notable that many IOCCC submissions happen to pack a lot of functionality in often less than 2k. I remember reading a few descriptions of some the winning entries, but I can't find that now. Here's a glimpse though: http://cboard.cprogramming.com/brief-history-cprogr…

Having spent a lot of time meditating on it (as sofuture mentioned), it isn't "slightly obfuscated" so much as "stubbornly written like APL rather than C". If you become inexplicably fascinated by that code and want help unraveling it, my contact info is in my profile. I don't consider it noteworthy as a useful program under 2kloc (on modern hardware it usually just crashes, it's quite cavalier with pointer casting,…

Yeah, I realized that fact a little late. Relevant snippet from the link about that initial bit of code for J:

I showed this fragment to others in the hope of interesting someone competent in both C and APL to take up the work, and soon recruited Roger Hui, who was attracted in part by the unusual style of C programming used by Arthur, a style that made heavy use of preprocessing facilities to permit writing further C in a distinctly APL style.

Re: What can you do in 2k LOC of C?

#32
post #9

Roberto Ierusalimschy's lpeg is around 2.4k loc of ansi C without any dependency beside libC and lua.h (needed to interface with Lua, since it's a Lua library). It implements an efficient pattern matching system based on Parsing Expression Grammars (akin to CFGs, but without ambiguities). It consists of a Pattern/Grammar to bytecode compiler and a custom VM to interpret the result of the compiling phase. Nice and cle…

The Lua code itself (while not Also, in my experience, LPEG is a great fit for middle-ground parsing - more complex than Perlish regexps (which it handles well), and including those a bit more complex still, but it's awkward for really complex parsing (since it inherently combines lexing and parsing).

But I dissed Perl and Tcl, so -1. Screw useful information. ;)

Re: What can you do in 2k LOC of C?

#34
post #31

Earlier quoted context omitted.

Having spent a lot of time meditating on it (as sofuture mentioned), it isn't "slightly obfuscated" so much as "stubbornly written like APL rather than C". If you become inexplicably fascinated by that code and want help unraveling it, my contact info is in my profile. I don't consider it noteworthy as a useful program under 2kloc (on modern hardware it usually just crashes, it's quite cavalier with pointer casting,…

Yeah, I realized that fact a little late. Relevant snippet from the link about that initial bit of code for J: I showed this fragment to others in the hope of interesting someone competent in both C and APL to take up the work, and soon recruited Roger Hui, who was attracted in part by the unusual style of C programming used by Arthur, a style that made heavy use of preprocessing facilities to permit writing further…

Arthur's C is stubbornly unconventional, but often, he's got a point.

Also, the APL community seems to be pretty disjoint from the rest of CS (though Arthur is also a Lisper). I think it'd be mutually beneficial if the APLers and the MLers got together, in particular.

Re: What can you do in 2k LOC of C?

#35

The core of my protobuf-decoding library upb ( https://github.com/haberman/upb/wiki ) is 3k SLOC of C. That includes * a hash table implementation * a reference-counted string type * a generic interface for doing tree traversals of protobuf data * the protobuf decoder itself (which implements the previous interface) * all the code to load proto descriptors (including bootstrapping the first one, which is necessary to…

It really says something about C that so many useful-but-small systems have a good chunk of code devoted to hash table implementations, atoms ("a reference-counted string type"), etc.

When working with C, it sometimes makes sense to have bespoke data structures, but it always makes me think of Hanson's _C Interfaces and Implementations_, Greenspun's tenth rule, etc.

My 1500loc C project also has a hash table implementation (and, soon, dynamic arrays (gasp!)). That and serializing custom data structures to disk accounts for more than half of its code. It's fast as hell, but I originally prototyped it in like 100 lines of Lua.

Re: What can you do in 2k LOC of C?

#36

Anyone know how big nginx is? I wouldnt at all be surprised if its under 2k LOC.

You're probably joking, but sloccount says: ansic: 82714 (99.68%) perl: 124 (0.15%) sh: 78 (0.09%) asm: 48 (0.06%) cpp: 17 (0.02%) (for nginx-0.8.53)

Why have I gotten 13 upvotes for this? Serious question.

Re: What can you do in 2k LOC of C?

#37
post #13

Write a basic Lisp interpreter/compiler and write everything else in Lisp? :) Would compiling Lisp code count against the challenge's LOC limit?

I did once write a 2k-line bytecode interpreter and runtime for R4RS Scheme -- the compiler, library, and debugger were another 2-3kloc of Scheme. I even used it as my main hacking platform for years. But I didn't post it here because: * It depends on the Boehm garbage collector. * (This is embarrassing.) It stopped working after some version bump of GCC, and the cause is not at all obvious to me. Since I don't Schem…

No blame, but I'm curious "why you don't Scheme much anymore". Was it due to Python, Common Lisp, etc?

I switched from (Chicken) Scheme to Common Lisp (and, in parallel, from Python to Lua), and, having read a lot of old code of yours, I'm curious about your choices - you seem consistently sensible and pragmatic. (Sorry to put you on the spot.)

Re: What can you do in 2k LOC of C?

#39

Earlier quoted context omitted.

You're probably joking, but sloccount says: ansic: 82714 (99.68%) perl: 124 (0.15%) sh: 78 (0.09%) asm: 48 (0.06%) cpp: 17 (0.02%) (for nginx-0.8.53)

Why have I gotten 13 upvotes for this? Serious question.

It's a show of thanks for taking the trouble to give a solid answer backed by facts. Just as bad memes are publicly swatted down, good work (even small) should be praised to set an example for others.

Re: What can you do in 2k LOC of C?

#40
post #39

Earlier quoted context omitted.

Why have I gotten 13 upvotes for this? Serious question.

It's a show of thanks for taking the trouble to give a solid answer backed by facts. Just as bad memes are publicly swatted down, good work (even small) should be praised to set an example for others.

If actual facts are that rare in software engineering, then that's one hell of a "code smell". It really wasn't hard to get.
Post reply on HN