devongovett/jensnockert: what was your experience with using CoffeeScript for such low level code? Did you need to consciously avoid certain CoffeeScript features that could negatively impact performance?
Overall positive, but as with any programming language, you have to be careful sometimes. For example, the for loop in CoffeeScript can be slow if you aren't careful:
for i in [0...10]
doSomething()
is slower than
for i in [0...10] by 1
doSomething()
because of the way it is compiled, so we always include the `by 1` as an optimization.
Another one is that if you have a function and the last thing in that function is a loop of some kind, CoffeeScript will try to return an array from that function collecting the results of the loop. So we explicitly put a return statement at the end of the function to avoid this and the performance penalties.
There are probably a few more to be careful of, but overall performance characteristics are similar to JS. I'm sure Jens has some thoughts too, but overall I think it was a positive experience.
This is one of the grayer areas. After your experience writing this decoder ... would you prefer CoffeeScript to keep its loops that can go in either direction, or would you prefer loops to always iterate upwards, and have to be explicit if you'd like to count from "[100..1] by -1" ?
I've programmed in dozens of languages, and I can't think of another one where loops could automatically run either up or down depending on the start and end values.
Not knowing CoffeeScript well, when I see this code:
n = -10
for i in [0...n]
doSomething()
I'd expect it to call doSomething zero times. At least that's what I'd expect in JavaScript or any of the other languages I've used.
Hmmm, well I think it's better if they can go in any direction by default otherwise people will be confused. The `by 1` should be considered an optimization IMO.
Unfortunately, that requires repeating the entire body of the loop, and so isn't workable/acceptable for our purposes. If you'd like to see the original conversation that led to the current compilation, it's all available on the GitHub issues.
Mostly avoided the regular for loops, replacing them with `for ... by 1`, to make sure the compiler knows in which direction to iterate.
Otherwise it is pretty similar to javascript, most coffeescript constructs map pretty simply down to javascript, so optimization tips for one language mostly applies to the other.
Perhaps I am missing something obvious here (very possible as I do not work with audio decoding) but the demo at http://codecs.ofmlabs.org/ appears to playback the MP3 (not tested the ALAC file yet) too quickly. The site reports the MP3 as lasting 4:25 whereas manually downloading the track from the above website Winamp says the track is 5:14 long and playback is noticeably slower.
Is this by design or a bug in the code (or a bug in Chrome (16-stable) that I used to check out the demo site)?
Well technically it is a missing feature in Chrome that we tried to work around to varying degrees of success. The Chrome audio API has no way to change the sample rate and uses whatever the native hardware sample rate is. We didn't have time to finish implementing sample rate conversion in JS (but it is coming) so we cheated a bit by dynamically loading a different file based on some common hardware sample rates. It seems that sometimes Chrome reports the sample rate wrong or something though, and we have seen this issue at least once before and will be looking into it and reporting browser bugs accordingly. Rest assured that this was just a hack, and real sample rate conversion is JS is coming along soon! :)