Bug 16525 - Possible to optimize out for(var x = 0; x < 500; x++) in JSC?
Summary: Possible to optimize out for(var x = 0; x < 500; x++) in JSC?
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: Macintosh OS X 10.4
: P2 Normal
Assignee: Nobody
Depends on:
Reported: 2007-12-20 04:39 PST by Eric Seidel (no email)
Modified: 2008-09-02 23:49 PDT (History)
3 users (show)

See Also:


Note You need to log in before you can comment on or make changes to this bug.
Description Eric Seidel (no email) 2007-12-20 04:39:21 PST
Possible to optimize out for(var x = 0; x < 500; x++) in JSC?

I wonder if it wouldn't be possible to completely optimize out the variable handling for "x" in the above statement in JavaScriptCore.  Meaning, pull x completely out of JS and make it be only on c-stack.

This would require two things:
1.  Walk the for subtree and make sure x is never referenced.
2.  Assure that "x < 500" is a statement involving only x and constants.

Requirement #1 could be relaxed if we actually exposed x in the scope chain.

This sort of optimization might be way overly complicated at this point.  But I saw LessNode::evaluateToNumber() as 1.2% as well as LocalVarAccessNode::evaluateToNumber() as 1.6% on the sample tonight, and started to wonder.
Comment 1 Eric Seidel (no email) 2007-12-20 04:49:28 PST
Along this same vein: our time spent in LocalVarAccessNode ::evaluateToNumber() in pushing the float value onto the stack for return.  We could avoid calling evaluateToNumber() entirely if we could know that evaluateToInt32() would be sufficient.  Yay x68 calling conventions!
Comment 2 Mark Rowe (bdash) 2007-12-20 07:35:16 PST
How would you account for x being referenced inside an eval?
Comment 3 Eric Seidel (no email) 2007-12-20 08:25:10 PST
Any use of "eval" would count as a reference to "x" and would break this optimization.  This would obviously require our planned eval tainting.
Comment 4 Maciej Stachowiak 2007-12-23 21:11:29 PST
Note that a reference to x could be anywhere inside the same function or in nested function declarations or expressions.
Comment 5 Eric Seidel (no email) 2007-12-24 00:10:41 PST
Which would mean we'd need to find the enclosing ScopeNode and walk the entire subtree.  ick.
Comment 6 Cameron Zwarich (cpst) 2008-09-02 23:49:38 PDT
This is no longer relevant after SquirrelFish.