Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I wish I could understand LuaJit completly. I think if there is any (reasonably sized) software I would like to completly understand its this one.

There is not only lots of good algorithms but also a lot of fantastic engineering. Things that you can not learn by reading papers.

Congrats Mike Pall. Now lets see if you can do the same for GC community that you did to the JIT community. Good Luck.



Yeah I agree it's a very interesting piece of software.

But do you understand the prequisites -- namely processor microarchitecture? I know that's why I don't understand it. I imagine if I did it might not be that hard to understand.

Most people's knowledge of the stack stops somewhere around C. C hasn't changed for 40 years. It's a portable assembly language, i.e. the common mental model is that each construct in C is a handful of assembly instructions on any processor. But to get LuaJIT-type performance you have to understand exactly how the CPU works, i.e. pipelining and reordering, internal caching algorithms, internal branch prediction algorithms, and maybe to some extent multicore, although I guess LuaJIT is single-threaded like Lua.

Mike Pall's mental model is for sure not C (since a lot of it isn't even written in C), but the lower level CPU architecture. And that has changed a lot in the last 10 years. I think even if you coded a lot of assembly language in the 80's it wouldn't necessarily translate to working on something like LuaJIT. My impression from skimming some online posts is that he has read hundreds of pages of Intel/ARM reference documentation from cover to cover, for multiple CPUs. I think there are a limited number of people with the patience to do that, since it's non-portable and changes relatively quickly.

Another problem is I think that the open source tools that are available don't work at the right level. Like when you are running profilers, they are helping you profile assembly code generated by a C compiler. The performance characteristics he's taking advantage of are at a lower level. I think you need CPU-specific performance counters and so forth, and there's a different way of getting those for each make/model. Are there even open source tools that allow you to get this information? I'd appreciate a pointer.

It's just a different level of abstraction than most programmers are working with. Not that many people are even writing "low level" C these days, e.g. something like redis. A lot of "application level" C code you see these days is old and/or not particularly fast.

I would be interested if anyone has any pointers for this style of code other than "read a bunch of CPU manuals". :)


I think that most sampling profilers like oprofile (http://oprofile.sourceforge.net/news/) or perf (https://perf.wiki.kernel.org/index.php/Main_Page) will give you CPU-level profiling and performance counters. The UI of those tools isn't super-friendly; if you're on OS X the "Instruments" app gives the same information with a far superior UI.

I have written a JIT that uses LuaJIT's dynamic assembly language engine DynASM (http://luajit.org/dynasm.html). DynASM is an incredibly piece of engineering, it lets you write really readable code for generating machine code at runtime, and is extremely small and low-overhead.

I keep meaning to write an article that demonstrates how to use LuaJIT for a small but interesting JIT. I just never quite get around to it. I always wish that I could use to implement the Universal Machine from ICFP 2008 (http://www.boundvariable.org/task.shtml), which is absolutely the most delightful problem ever, but as I recall it makes extensive use of self-modifying code which makes JIT-ting much more difficult and less effective.


> I keep meaning to write an article that demonstrates how to use LuaJIT for a small but interesting JIT. I just never quite get around to it.

Please do.


Some hints for your quest:

- Linux's perf tool allows you to read HW performance counters. It's pretty self-explanatory, and some interesting ones are platform-neutral, whereas others you have to specify the CPU manufacturer's hex-code. (See Intel manual, for example.)

- For pipelining and HW/CPU details, I suggest you grab a copy of Hennesy and Patterson's Computer Architecture: A Quantitative Approach. There is also Computer Architecture: A Programmer's Perspective, which is good, but I think for what you're interested in, CAAQA is the better book.

- If you just want to play around with HW performance counters, you might want to try Intel's vtune, which comes (or at least did so, two years ago) as an Eclipse plugin/RCP-workbench.


"Another problem is I think that the open source tools that are available don't work at the right level. Like when you are running profilers, they are helping you profile assembly code generated by a C compiler. The performance characteristics he's taking advantage of are at a lower level. I think you need CPU-specific performance counters and so forth, and there's a different way of getting those for each make/model. Are there even open source tools that allow you to get this information? I'd appreciate a pointer."

The way forward is a sampling profiler.

By sampling the stack every (eg) millisecond, you can build a picture of what is taking the longest.


A sampling profiler still doesn't tell you what specific instructions are taking longer, how much memory contention is happening between CPUs, how many cache and TLB misses there are, etc.


Sure it can; you can look at the next or previous machine instructions.

If they occur more often than others than you can see which are taking longer.

The other metrics you mentioned are often built into the operating system/memory manager IIRC.


> But do you understand the prequisites -- namely processor microarchitecture?

I do not but I try to learn more about that (coursera ftw).

LuaJit is awesome on many level, first as you have pointed out attention to detailed on processer level is quite special and not many people can do that.

Other then the good usage of the processor is not the only reason luajit is fast. On the compiler level, the optimization luajit does are extensive. Even lots of novel innovations are in that that people writting papers should read (Mike Pall has posted a list of thing that he thinks are somewhat novel). The nature of the trace compiler makes many of these things less complicated then they would have been otherwise.


"Are there even open source tools that allow you to get this information? I'd appreciate a pointer."

http://icl.cs.utk.edu/papi/


I would love if someone starts a project - "Reading & Understanding LuaJIT". I've seen a similar project for PostgreSQL.


There are many compiler books but no books about JITs in general. There are some good blogpost about LuaJit but not that many. Following Mike Pall (MikeMike on HN or reddit is intressting too)

This is a good start: http://playingwithpointers.com/archives/1010


Great link; I haven't read that one before.

Just submitted as own item: http://news.ycombinator.com/item?id=4777186


Mike Pall wrote a brief summary for "Reading & Understanding Lua", which is likely a prerequisite.

http://www.reddit.com/r/programming/comments/63hth/ask_reddi...


Have you a link for that?


I just found about it today actually - http://code.google.com/p/postgres-learning/




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: