Live data from Hacker News

What can you do in 2k LOC of C?

h4ck3r.net

51–60 of 110 posts

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

#51
post #16

$ git show e83c5163316f89bfbde7d9ab23ca2e25604af290 --stat commit e83c5163316f89bfbde7d9ab23ca2e25604af290 Author: Linus Torvalds Date: Thu Apr 7 15:13:13 2005 -0700 Initial revision of "git", the information manager from hell Makefile | 40 +++++++++ README | 168 ++++++++++++++++++++++++++++++++++++ cache.h | 93 ++++++++++++++++++++ cat-file.c | 23 +++++ commit-tree.c | 172 +++++++++++++++++++++++++++++++++++++ init-…

Ah, that's a good one, and worth study at that revision with so few lines. Thanks!

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

#52
post #20

Getting a standards-compliant XML parser into 2K lines is going to be a challenge, if you're not going to cheat on what a "line" is. You must be able to deal with both UTF-8 and UTF-16 [1] (and remember UTF-16 can be in either endian order), you have several tables of things like what chars are valid where, you've got data structures to declare, and there's a lot of edge cases that may not leap to mind but if you don…

Sounds like a challenge (to someone ;) A good XML test suite to confirm "fullness" must exist?

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

#53

Earlier quoted context omitted.

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.)

Thanks! Partly Python's gotten more tolerable as a language, partly I'm doing more things needing libraries, partly I mostly code inside https://github.com/darius/halp these days and it doesn't have a Scheme mode so far. I do have a couple of recent Scheme projects up on github though -- optilamb and selfcentered. Very impressed with LuaJIT2, btw -- I'd like to do more with it.

Halp is fun. Thanks. Installed it already.

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

#54

If you think about it for a second, you realize that TrueType rasterization can't be that hard because printers were doing it long ago on crappy little embedded processors, but the default is just to fall back on the big ugly library, and then wrap it and pretend it's not there. How about instead, just write some good code? This is why 'modern' software can still manage to bring a 3GHz quad-core to it's knees, IMHO

I personally suspect the explosion of storage space is the biggest factor in all this. I was just writing software for a uC with 2kb program space last week, and I started to bump into the limit. Optimizing my code for size actually resulted in much better code, because I had to stop and think "How can I do this... smarter ?" (Though if you're trying to fit 8kb into 2kb and have to start doing voodoo, it's true the q…

Current software (compiled code) would usually fit perfectly in the 20Mb hard drives I was using 20 years ago. The extra space is taken by various data files. I don't think I ever cared about compiled size with PC software, even when working with 360kb floppies.

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

#55
post #41

Earlier quoted context omitted.

Umm..13 people found it insightful/interesting? I don't know..

I can think of plenty of comments that were /actually/ insightful, though. This feels like, "oh, wow, you know how to use science! +1". sad.

It's not sad at all. HN is, for me at least, primarily a place for learning. I scan comments for people speaking of facts and first-hand experience, or links to similar things.

You provided some hard data, and some people who appreciated it pressed the up arrow. It's nothing more than that, and certainly not sad.

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

#56
post #49
post #20

Getting a standards-compliant XML parser into 2K lines is going to be a challenge, if you're not going to cheat on what a "line" is. You must be able to deal with both UTF-8 and UTF-16 [1] (and remember UTF-16 can be in either endian order), you have several tables of things like what chars are valid where, you've got data structures to declare, and there's a lot of edge cases that may not leap to mind but if you don…

> Getting a standards-compliant XML parser into 2K lines is going to be a challenge This is a pretty good argument against XML.

I would say its an argument for using JSON in situations where you need stuff to be small, fast and simple and XML in situations where you need it (where you want to do a queries easily over the document structure, which XPath is fairly decent at, interop with systems that insist on XML and things that are more document-ish rather than data-ish.

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

#57
post #20

Getting a standards-compliant XML parser into 2K lines is going to be a challenge, if you're not going to cheat on what a "line" is. You must be able to deal with both UTF-8 and UTF-16 [1] (and remember UTF-16 can be in either endian order), you have several tables of things like what chars are valid where, you've got data structures to declare, and there's a lot of edge cases that may not leap to mind but if you don…

A JSON parser at ~2K lines: https://github.com/johnezang/JSONKit I'm the author, so I'm obviously biased :). It has strict Unicode standard UTF-8 parsing ("passes" http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt) and has a whiz-bang LRU cache with a clever aging replacement policy that saves lots of time by reusing already seen and instantiated immutable objects (think JSON dictionaries: the keys in "key": value pairs tend to repeat an awful lot). A further benefit is this saves lots of memory too. For converting JSON to 'native' ObjC/Foundation objects, it's faster than the other ObjC JSON libraries by 2-10x (HEAVILY dependent on the JSON being parsed, typical is 2-4x), and serializing ObjC -> JSON is (usually) even faster.

So, in short, it's possible to write a very high performance JSON serializer and deserializer in ObjC in ~2K lines of code. For JSONKit, most it is actually pure C (which in turn uses Core Foundation, which is a pure C API interface version for the equivalent native ObjC objects), with public API ObjC stub bindings making use of the C code.

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

#58
post #47

Shocked that silentbicycle hasn't mentioned it already, but Arthur Whitney whipped up the first prototype/inspiration for the J language in a short bit of macro heavy C over the course of an afternoon. 42 lines? http://pastebin.com/s2usuqDq If this interests you at all, absolutely worth reading Roger Hui's retrospective on the subject (more about J + Ken Iverson, but definitely fascinating) http://keiapl.org/rhui/

> http://pastebin.com/s2usuqDq I'm going to save this for the next time someone asks why there aren't more females in CS/programming. I just tell them that this kind of code is held in high esteem, instead of being ridiculed for the stupidity that it is.

And how is that at all related to female numbers in CS/programming?

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

#59
post #47

Shocked that silentbicycle hasn't mentioned it already, but Arthur Whitney whipped up the first prototype/inspiration for the J language in a short bit of macro heavy C over the course of an afternoon. 42 lines? http://pastebin.com/s2usuqDq If this interests you at all, absolutely worth reading Roger Hui's retrospective on the subject (more about J + Ken Iverson, but definitely fascinating) http://keiapl.org/rhui/

> http://pastebin.com/s2usuqDq I'm going to save this for the next time someone asks why there aren't more females in CS/programming. I just tell them that this kind of code is held in high esteem, instead of being ridiculed for the stupidity that it is.

Are you saying that women can't appreciate elegance and brevity? Perhaps your kind of attitude is why there aren't more females in programming.

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

#60
post #27

Shocked that silentbicycle hasn't mentioned it already, but Arthur Whitney whipped up the first prototype/inspiration for the J language in a short bit of macro heavy C over the course of an afternoon. 42 lines? http://pastebin.com/s2usuqDq If this interests you at all, absolutely worth reading Roger Hui's retrospective on the subject (more about J + Ken Iverson, but definitely fascinating) http://keiapl.org/rhui/

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…

"That's interesting, but damn is that some ugly code"

I would disagree. That code is beautiful. Elegance and brevity; it's as close to a pure expression of the coder's thoughts as it's reasonably possible to get.

Post reply on HN