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

Do you have more resources on optimizations when targeting js?


No, sorry. I haven't done much in the way of optimizations with JS, but I'll explain how this is optimized. This is actually a simplified version of the structure used in IronBabel (emulator platform of mine, http://sourceforge.net/projects/ironbabel/ )

You have a hash table mapping an address to a block length, block function, and a hash/checksum. Your execution loop looks for the program counter in the hash table. If it exists, you take <block length> bytes from the program counter, hash/checksum it, and compare it against the hash table entry.

If the cache entry misses, you need to recompile the block. Starting from the program counter, read instructions and rather than executing the operation (as in an interpreter), you append the operation's code to a string. When you hit a branch and output the code, you break the loop. You now have the code of the block.

Wrap it in a function, eval it, and put the function in the cache.

There are a number of optimizations that can be done (see IronBabel for specifics), but the easiest is to group blocks into a state machine. Simply push all of the code into a big while(true) { switch(PC) { case 0xDEADBEEF: ... }} and break out if you hit a block that isn't in the state machine.




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

Search: