Jeff Walden wrote:
On 12/04/2012 03:57 PM, Brendan Eich wrote:
Allen Wirfs-Brock wrote:
The timing of copying is only an issue if the function actually assigns to a 
formal parameter.  Such assignments should be pretty easy to (conservatively) 
statically check for.
I'm telling you what engines do. Not what they might do. I did assignment 
analysis in SpiderMonkey for Firefox 3.6, it was helpful in its day. I think a 
bunch has been ripped out because modern JITs don't need it.

Just to be clear, that assignment analysis was not used to optimize arguments copying for strict mode.

[...]the point I made, cited above: " engines don't do it currently and feel little 
pressure to do so. Chicken and egg."

SpiderMonkey's implementation of the arguments object functions exactly as 
described here for functions with strict mode code -- actually for all 
functions, I think.  Regarding the strict mode semantics specifically, it was 
fairly easy to make those optimizations when I was implementing the various 
strict mode arguments semantics, so I did them.  There hasn't been a time where 
SpiderMonkey's had strict mode arguments semantics, without these optimizations.

I don't see this. Firefox 3 era (pre-ES5):

js> function f(x) { if (0) arguments; return x }
js> dis(f)
main:
00000:  getarg 0
00003:  return
00004:  stop

Source notes:

Latest SpiderMonkey:

js> function f(x) { if (0) arguments; return x }
js> dis(f)
flags:
loc     op
-----   --
00000:  arguments
00001:  setlocal 0
00004:  pop
main:
00005:  getarg 0
00008:  return
00009:  stop

Source notes:
 ofs  line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:    1     5 [   5] colspan 34
  2:    1     9 [   4] colspan 8

js> function g(x) { "use strict"; if (0) arguments; return x }
js> dis(g)
flags:
loc     op
-----   --
00000:  arguments
00001:  setlocal 0
00004:  pop
main:
00005:  getarg 0
00008:  return
00009:  stop

Source notes:
 ofs  line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:    3     5 [   5] colspan 16
  2:    3     5 [   0] colspan 32
  4:    3     9 [   4] colspan 8

True, no difference due to "use strict"; but both paths rely on the JIT to optimize away the useless arguments op. That op is emitted even though no code (live or dead) assigns to the formal parameter x.

Now let's go back to rev ba4d04033cd8 in the tracemonkey repo at hg.mozilla.org, and make the function assign to a formal somewhere in its control flow:

js> function f(x) { if (1) arguments; else x = 42; return x }
js> dis(f)
flags: NULL_CLOSURE
loc     op
-----   --
main:
00000:  getarg 0
00003:  return
00004:  stop

Source notes:
 off line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:   10     0 [   0] colspan  54
  2:   10     4 [   4] colspan  1

This is optimized to avoid reifying the arguments object, which is in a useless expression statement so not needed.

This state of SpiderMonkey supported strict mode, as demonstrated by the corresponding function g:

js> function g(x) { "use strict"; if (1) arguments; else x = 42; return x }
js> dis(g)
flags: NULL_CLOSURE
loc     op
-----   --
00000:  arguments
00001:  pop
main:
00002:  getarg 0
00005:  return
00006:  stop

Source notes:
 off line    pc  delta desc     args
---- ---- ----- ------ -------- ------
  0:    8     2 [   2] colspan  68
  2:    8     6 [   4] colspan  1

Here arguments is reified because the formal x is assigned and the parser saw 'arguments' used albeit in a useless expression statement.

(The colspan source notes, for column index in source maps, shows I have a patch pushed:

$ hg log -r tip
changeset:   73898:ba4d04033cd8
tag:         bug568142-big-srcnote-fun
tag:         qbase
tag:         qtip
tag:         tip
user:        Brendan Eich <[email protected]>
date:        Tue Oct 18 17:40:58 2011 -0700
summary:     imported patch bug568142-big-srcnote-fun

from 2011 in this old workarea, but it doesn't affect code generation.)

Some of this evolution shows how interpreter performance has mattered less and less (for the popular benchmarks, anyway). But it also shows that what I wrote was true: SpiderMonkey used to do more analysis in order to avoid reifying arguments objects, and ES5 strict mode in a function that assigns a formal required reifying on entry, to copy the actuals into arguments before any could be clobbered.

Other implementors should pipe up about strict mode effects on function code. I remember Oliver (JavaScriptCore) pointing out the arguments-copying requirement of strict mode as a deoptimization. See

https://mail.mozilla.org/pipermail/es5-discuss/2009-August/003144.html

and others in that thread.

/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to