Bug 26014 - Declaring an unused variable outside an empty loop speeds up the loop
Summary: Declaring an unused variable outside an empty loop speeds up the loop
Status: RESOLVED WORKSFORME
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Mac OS X 10.5
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-05-25 14:45 PDT by David Smith
Modified: 2012-03-12 13:40 PDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Smith 2009-05-25 14:45:44 PDT
time ./jsc -e "function e() { for (var i = 0; i < 99999999; ++i) { } } e();"

real	0m0.389s
user	0m0.367s
sys	0m0.009s

time ./jsc -e "function e() { var a = 0; for (var i = 0; i < 99999999; ++i) { } } e();"

real	0m0.325s
user	0m0.312s
sys	0m0.009s

It seems like something has to be going wrong for this to be the case. Using 64 bit trunk jsc, r40951, on a 2GHz Core 2 Duo MacBook.
Comment 1 Mark Rowe (bdash) 2009-05-25 15:39:06 PDT
This may be due to cache effects.
Comment 2 Gavin Barraclough 2012-03-12 13:40:00 PDT
time /WebKitBuild/Release/jsc  -e "function e() { var a = 0; for (var i = 0; i < 99999999; ++i) { } } e();"

real	0m0.300s
user	0m0.293s
sys	0m0.006s

time /WebKitBuild/Release/jsc  -e "function e() { for (var i = 0; i < 99999999; ++i) { } } e();"

real	0m0.301s
user	0m0.293s
sys	0m0.006s

This doesn't reproduce for me.  As Mark says, this kind of effect can be caused by cache effects as stack layout changes, and is not necessarily unexpected.  We don't currently optimize to remove unused variables; we could do so, but every additional optimization pass comes at a runtime cost in JIT, and we would only want to do so if there is a demonstrable value in doing so (real programs tend not to declare lots of unused variables, and as your numbers show, it's not necessarily a performance regression even if they do!)

I don't think there is anything we need to fix here.