Re: The Why's of Loop Unrolling

Edwin Martin <[email protected]> Mon, 23 Oct 2006 10:16:53 +0200
Newsgroups gmane.comp.web.dom.wdf
Message-ID <[email protected]>
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/