Re: The Why's of Loop Unrolling

Chris <[email protected]> Mon, 23 Oct 2006 01:48:44 -0700
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
Hi Edwin, I had the same objections when I first saw the performance 
claims of loop unrolling in Javascript - but it really works. I suggest 
you try it, and see for yourself.

The performance benefit is also well-documented:
http://www.google.com/search?q=javascript+loop+unrolling

If you rather not write your own unrolled loop, the second to last test 
has one for you to try:
http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html

Click the second-to-last test button (the last one is miswired to the 
previous test..) and you'll see the performance numbers speak for 
themselves. My post here is an investigation into why it does work.
-Chris


Edwin Martin wrote:
> Chris schreef:
>   
>> I came across some interesting information today that some here might 
>> find interesting.
>>
>> Often in discussing Javascript loop optimization, loop unrolling and 
>> Duff's Device come up:
>> http://home.earthlink.net/~kendrasg/info/js_opt/jsOptMain.html
>> (bottom of page)
>>
>> And often shortly thereafter, the "But why...?" question arises, and I'm 
>> yet to find a good answer, just guesses.
>>     
> The only purpose of loop unrolling is preventing jumps (from the bottom 
> of the loop to the top of the loop).
>
> This has it uses in assembly, where speed it very critical.
>
> It has almost no use in JavaScript. Because the JavaScript code gets 
> bigger with loop unrolling, it takes longer to download and probably 
> makes your site slower instead of faster. Loop unrolling is also a 
> maintanance nightmare.
>
>   
>> 2) A typical floating-point operation (for example) requires 4 CPU 
>> ticks, but a new one can be fed to the pipeline each tick. That means 
>> executing f++; 5 times would normally take 5*4=20 ticks, but with 
>> Out-Of-Order it can be done in 8 ticks. Note that from what I've read, 
>> Pentium 4 series CPUs have even longer pipelines, some ops taking over 
>> 20 ticks.
>>   
>>     
> But this is of no use in JavaScript. JavaScript is interpreted, not 
> compiled to machine code.
>
> You mix up two things of different scales. Like you're looking at a 
> globe trying to optimize city traffic.
>
> Because JavaScript is interpreted, you really don't know how the CPU 
> 'sees' your code. If you do happen to optimize your JavaScript for a 
> certain CPU, it is almost certainly not worth the time spend (the gains 
> will be minimal).
>
> To optimize JavaScript, there are other and better tricks.
>
> Edwin Martin
>   



Unsubscribe
[email protected]

List info
http://www.quirksmode.org/dom/list.html 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/wdf-dom/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/wdf-dom/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:[email protected] 
    mailto:[email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/