Optimize Function and MethodClosure calls

RESOLVED FIXED in Q2 12 - Cyril

Status

P2
normal
RESOLVED FIXED
7 years ago
7 years ago

People

(Reporter: edwsmith, Assigned: edwsmith)

Tracking

(Blocks: 1 bug)

unspecified
Q2 12 - Cyril

Details

Attachments

(2 attachments)

(Assignee)

Description

7 years ago
Using OP_call to invoke a Function or MethodClosure requires an unnecessarily long call path:

avmplus::op_call -> FunctionObject::call -> get_coerced_receiver -> MethodEnv::coerceEnter

This involves a null-checking fast path then three indirect calls.  We can do better, especially
when the call site has a Function (rather than * or ScriptObject)

1. Add an invocation pointer field to FunctionObject.  The virtual FunctionObject::call method just invokes that pointer, for generic callers who are calling virtual ScriptObject::call.

2. The target of the pointer field is customized for Function, MethodClosure, or WeakMethodClosure (each has different logic for argv[0]), then calls MethodEnv::coerceEnter.

3. At JIT callsites with Funciton, inline the null check then load & call this pointer.  Initially this straightens out the code and only requires two call paths.

Then, we could also JIT-compile the stub, similar to how invokers are JIT-compiled (jit-compile the argv[0] processing and the contents of the invoker stub into one stub).  Then the call only requires 1 indirect call in the common case.

All subclasses of FunctionObject must provide their own stub.  Hosts should never override FunctionObject::call.
(Assignee)

Comment 1

7 years ago
Created attachment 602967 [details] [diff] [review]
Streamline closure calls without jit-compilation

This patch implements steps 1-3 from the description.

It also introduces new APIs in avm.h that hosts can use instead of
directly calling ScriptObject::call or Toplevel::op_call.  The latter
has been removed entirely.

casual performance results show a few outliers (marked --) but
mostly noise and a few positive results where expected.

avm: tamarin-redux  version: 7251:8a05fc8ee494
avm2: avm + op_call patch
iterations: 5

                                        avm            avm2
test                           best     avg    best     avg   %dBst   %dAvg
Metric: iterations/second 
Dir: asmicro/
  alloc-0                      99.9    98.5   101.2   100.4     1.3     1.9   
  alloc-1                      48.1    47.9    47.9    47.8    -0.6    -0.2   
  alloc-10                     16.7    16.7    17.1    17.0     2.0     1.8   
  alloc-11                     17.5    17.4    18.0    18.0     3.2     3.4 + 
  alloc-12                     18.1    18.1    18.3    18.3     1.1     1.1 + 
  alloc-13                    105.3   105.1   106.8   106.6     1.4     1.5 + 
  alloc-14                     84.8    83.5    84.9    82.7     0.1    -0.9   
  alloc-2                      25.9    25.9    26.1    26.0     0.4     0.4   
  alloc-3                      18.1    18.1    18.1    18.1     0.1     0.1   
  alloc-4                      54.5    54.3    54.8    54.8     0.6     0.9   
  alloc-5                      38.8    38.6    38.5    38.5    -0.9    -0.4   
  alloc-6                      83.9    83.8    85.3    85.3     1.7     1.8 + 
  alloc-7                      43.9    43.8    44.3    44.3     1.1     1.1 + 
  alloc-8                      17.6    17.6    18.0    17.9     2.2     2.0   
  alloc-9                      17.7    17.6    18.1    18.0     2.1     2.3   
  arguments-1                 898.1   891.5   885.1   883.1    -1.4    -0.9 - 
  arguments-2                 550.9   550.3   522.0   519.4    -5.3    -5.6 --
  arguments-3                  23.3    23.3    24.0    23.9     2.7     2.3 + 
  arguments-4                  31.2    30.6    31.7    31.6     1.5     3.5   
  array-1                    4673.3  4670.1  5214.8  5209.4    11.6    11.5 ++
  array-2                     724.3   722.6   712.6   711.2    -1.6    -1.6 - 
  array-foreach-func          341.3   340.1   360.3   357.2     5.6     5.0 ++
y-foreach-method-sans-types   473.5   473.5   512.5   512.0     8.2     8.1 ++
y-foreach-method-with-types   355.6   354.2   365.3   363.0     2.7     2.5 + 
  array-init-1                480.5   472.7   474.5   474.0    -1.2     0.3 - 
  array-init-2                505.0   499.9   500.0   499.9    -1.0     0.0 - 
  array-init-3                320.4   318.6   314.1   312.6    -2.0    -1.9 - 
  array-init-4                309.4   308.2   305.8   305.0    -1.2    -1.0 - 
  array-pop-1                 364.9   358.2   351.6   350.0    -3.6    -2.3 - 
  array-push-1                312.1   307.8   305.8   302.7    -2.0    -1.7   
  array-read-C-1              576.4   571.1   578.8   572.8     0.4     0.3   
  array-read-C-2               32.8    32.5    32.6    32.4    -0.7    -0.4   
  array-read-Number-1         577.4   571.2   572.4   569.7    -0.9    -0.3   
  array-read-Number-2          33.5    33.4    33.2    33.2    -0.9    -0.7   
  array-read-int-1            589.8   589.6   589.4   588.9    -0.1    -0.1   
  array-read-int-2             33.5    33.4    33.2    33.2    -0.9    -0.8   
  array-read-int-5            358.9   358.5   358.9   358.7    -0.0     0.1   
  array-read-int-6            358.9   358.9   358.9   358.4    -0.0    -0.1   
  array-read-int-7            358.6   358.4   358.9   358.2     0.1    -0.0   
  array-read-length-1          51.7    51.7    51.8    51.7     0.1     0.1   
  array-shift-1               138.0   137.3   137.0   135.6    -0.7    -1.2   
  array-slice-1                25.5    25.5    26.0    26.0     2.0     2.1 + 
  array-sort-1                 32.5    32.4    32.4    32.3    -0.4    -0.3   
  array-sort-2                 2.85    2.84    2.86    2.85     0.3     0.5   
  array-sort-3                 24.6    24.5    24.6    24.5     0.2     0.1   
  array-sort-4                 11.3    11.2    11.7    11.6     3.3     3.3 + 
  array-unshift-1             174.8   174.1   181.3   180.6     3.7     3.7 + 
  array-write-C-1              54.3    53.5    54.8    53.9     1.0     0.8   
  array-write-C-2              29.8    29.7    30.4    30.1     2.0     1.4 + 
  array-write-Number-1         25.0    24.7    24.6    24.5    -1.5    -0.7   
  array-write-Number-2         17.9    17.9    18.1    18.0     0.9     0.6   
  array-write-int-1            65.8    65.1    65.2    64.8    -1.0    -0.3   
  array-write-int-2            31.8    31.7    31.5    31.4    -0.9    -0.9   
  array-write-int-3            69.7    68.3    69.1    69.1    -0.8     1.1   
  array-write-int-4            68.9    68.8    70.9    69.5     3.0     0.9 + 
  array-write-int-5            68.2    68.2    68.5    68.5     0.4     0.5   
 bytearray-read-available-1    96.5    96.5    96.6    96.6     0.1     0.1   
  bytearray-read-bool-1        20.4    20.3    20.4    20.4    -0.0     0.6   
  bytearray-read-byte-1        20.9    20.9    20.9    20.9    -0.1    -0.1   
  bytearray-read-byte-2        96.9    96.8    97.5    97.5     0.6     0.6   
  bytearray-read-byte-3        86.1    86.1    85.7    85.6    -0.5    -0.5   
  bytearray-read-bytes-1       14.6    14.5    14.7    14.7     1.2     1.4 + 
  bytearray-read-bytes-2      790.2   789.0   793.2   791.8     0.4     0.4   
  bytearray-read-double-1      13.2    13.1    13.1    13.1    -0.2    -0.1   
  bytearray-read-double-2      18.8    18.8    18.9    18.8     0.2    -0.3   
  bytearray-read-float-1       15.2    15.2    15.3    15.3     0.4     0.5   
  bytearray-read-float-2       17.9    17.9    17.8    17.8    -0.8    -0.6   
  bytearray-read-int-1         17.5    17.4    17.9    17.8     2.2     2.4 + 
  bytearray-read-int-2         20.3    20.2    20.0    19.9    -1.4    -1.5 - 
  bytearray-read-length-1     118.6   118.4   118.5   118.5    -0.1     0.1   
  bytearray-read-position-1   118.6   118.6   118.5   118.5    -0.1    -0.1   
  bytearray-read-short-1       19.7    19.7    19.8    19.8     0.7     0.7   
  bytearray-read-short-2       20.3    20.2    19.9    19.9    -1.6    -1.6 - 
  bytearray-read-ubyte-1       20.9    20.9    20.9    20.9    -0.1    -0.0   
  bytearray-read-uint-1        16.9    16.8    16.2    16.1    -4.2    -4.4 - 
  bytearray-read-uint-2        20.0    20.0    20.1    20.1     0.5     0.3   
  bytearray-read-ushort-1      20.0    19.9    19.8    19.8    -0.8    -0.5   
  bytearray-read-ushort-2      20.3    20.3    20.0    20.0    -1.2    -1.2 - 
  bytearray-read-utf-1         33.1    32.9    33.5    33.1     1.3     0.6   
  bytearray-read-utf-2         3.46    3.45    3.83    3.82    10.7    10.9 ++
  bytearray-read-utfbytes-1    31.4    31.2    31.5    31.4     0.3     0.5   
  bytearray-read-utfbytes-2    3.46    3.42    3.77    3.73     9.1     9.3 ++
  bytearray-write-bool-1       15.8    15.3    15.2    14.7    -4.2    -3.4   
  bytearray-write-byte-1       16.5    15.7    15.3    15.2    -7.5    -2.9 --
  bytearray-write-byte-2       69.5    67.8    70.0    68.3     0.7     0.7   
  bytearray-write-byte-3       66.8    66.2    66.3    65.7    -0.8    -0.7   
  bytearray-write-bytes-1      18.7    18.5    19.1    19.0     2.1     2.5   
  bytearray-write-bytes-2    1138.9  1135.5  1142.9  1137.3     0.4     0.2   
  bytearray-write-double-1     12.3    12.3    12.3    12.3    -0.3    -0.1   
  bytearray-write-double-2     17.2    17.1    17.7    17.6     3.0     2.8 + 
  bytearray-write-float-1      13.2    13.0    13.2    13.2    -0.0     1.3   
  bytearray-write-float-2      16.4    16.3    15.1    14.9    -7.8    -8.8 --
  bytearray-write-int-1        16.6    16.6    16.8    16.8     1.1     1.1 + 
  bytearray-write-int-2        15.7    15.3    19.3    19.1    23.3    25.1 ++
 bytearray-write-position-1   149.6   146.6   150.0   146.3     0.3    -0.2   
  bytearray-write-short-1      16.8    15.4    17.5    17.4     4.1    12.9   
  bytearray-write-short-2      19.2    19.1    19.2    18.8    -0.0    -1.6   
  bytearray-write-uint-1       16.5    16.2    16.6    16.2     0.9     0.1   
  bytearray-write-uint-2       17.1    16.9    19.3    19.1    12.7    13.0 ++
  bytearray-write-utf-1        43.2    42.6    43.3    42.2     0.2    -1.0   
  bytearray-write-utf-2        27.2    27.0    27.8    27.4     2.0     1.7 + 
 bytearray-write-utfbytes-1    55.0    54.5    55.0    54.8    -0.0     0.7   
 bytearray-write-utfbytes-2    30.3    30.2    31.1    30.9     2.6     2.3 + 
  closedvar-read-1           4586.4  4570.8  4567.4  4557.4    -0.4    -0.3   
  closedvar-write-1          6192.8  6175.4  6301.7  6221.0     1.8     0.7   
  closedvar-write-2          6287.7  6280.3  6226.8  6213.6    -1.0    -1.1 - 
  do-1                       4544.5  4542.7  4557.4  4543.3     0.3     0.0 + 
  for-1                      4586.4  4543.5  4543.5  4542.7    -0.9    -0.0 - 
  for-2                      2769.2  2767.8  2839.2  2810.8     2.5     1.6 + 
  for-3                      2901.1  2892.9  2894.1  2893.7    -0.2     0.0 - 
  for-in-1                    442.1   433.0   419.2   412.3    -5.2    -4.8 - 
  for-in-2                    430.1   423.2   415.8   407.6    -3.3    -3.7   
  funcall-1                   378.6   368.0   507.0   493.8    33.9    34.2 ++
  funcall-2                   341.3   333.1   348.3   342.0     2.0     2.7   
  funcall-3                   365.9   364.6   463.5   462.7    26.7    26.9 ++
  funcall-4                   135.3   134.7   135.7   135.5     0.3     0.6 + 
  globalvar-read-1           4801.2  4754.2  4767.2  4764.8    -0.7     0.2   
  globalvar-write-1          6564.4  6485.1  6548.5  6533.9    -0.2     0.8   
  isNaN-1                    4683.3  4597.6  4628.4  4563.6    -1.2    -0.7   
  isNaN-2                    4720.3  4710.9  4539.5  4537.9    -3.8    -3.7 - 
  isNaN-3                    4364.6  4215.6  4622.4  4490.3     5.9     6.5 + 
  lookup-array-fetch-1        720.6   719.9   713.3   710.5    -1.0    -1.3 - 
  lookup-array-in-1          1948.1  1940.5  1937.1  1928.9    -0.6    -0.6   
  lookup-negindex-array-1     482.0   480.9   484.0   478.4     0.4    -0.5   
  lookup-negindex-array-2     321.7   315.7   320.4   315.1    -0.4    -0.2   
  lookup-negindex-object-1    499.5   495.5   497.5   495.9    -0.4     0.1   
  lookup-negindex-object-2    394.6   394.2   394.6   394.1    -0.0    -0.0   
  lookup-object-fetch-1       885.1   884.1   884.1   881.9    -0.1    -0.2   
  lookup-object-in-1         1537.5  1535.9  1535.5  1533.7    -0.1    -0.1   
  number-toString-1            7.17    7.14    7.04    7.01    -1.8    -1.8   
  number-toString-2            75.6    75.6    76.8    76.8     1.6     1.5 + 
  object-read-length-1        274.9   274.4   265.5   265.3    -3.4    -3.3 - 
  oop-1                        3.87    3.86    3.80    3.78    -1.9    -2.1 - 
  parseFloat-1                 76.7    76.0    76.6    76.6    -0.1     0.8   
  parseInt-1                  200.2   192.8   198.0   194.1    -1.1     0.7   
  regex-exec-1                 74.3    73.7    73.7    72.0    -0.7    -2.4   
  regex-exec-2                 89.6    87.7    87.2    86.5    -2.6    -1.4   
  regex-exec-3                134.5   133.8   136.7   136.5     1.7     2.0 + 
  regex-exec-4                348.7   347.2   348.7   343.7    -0.0    -1.0   
  restarg-1                   899.1   878.3   885.1   870.9    -1.6    -0.8   
  restarg-2                   540.5   539.9   517.5   506.7    -4.3    -6.2 - 
  restarg-3                    44.3    43.3    44.6    44.5     0.8     2.8   
  restarg-4                    31.3    30.8    31.7    31.2     1.1     1.4   
  string-casechange-1          28.1    27.1    29.4    29.3     4.4     8.1 + 
  string-casechange-2          27.5    27.5    29.3    29.0     6.6     5.6 ++
  string-charAt-1            1561.4  1546.5  1563.4  1550.2     0.1     0.2   
  string-charAt-2              91.5    91.3    91.5    91.4     0.1     0.1   
  string-charCodeAt-1        1558.4  1549.5  1522.5  1521.7    -2.3    -1.8 - 
  string-charCodeAt-2        1417.6  1391.8  1419.6  1387.4     0.1    -0.3   
  string-charCodeAt-3        1159.8  1155.2  1232.8  1213.0     6.3     5.0 ++
  string-charCodeAt-4        2216.8  2186.4  2173.8  2144.7    -1.9    -1.9 - 
  string-charCodeAt-5        1001.0   992.2   976.0   974.8    -2.5    -1.8 - 
  string-charCodeAt-6        1507.5  1490.3  1515.5  1486.5     0.5    -0.3   
  string-charCodeAt-7        2259.7  2252.7  2134.9  2134.3    -5.5    -5.3 --
  string-fromCharCode-1       256.5   255.9   267.2   264.9     4.2     3.5 + 
  string-fromCharCode-2        70.6    69.9    67.9    67.9    -3.7    -2.9 - 
  string-indexOf-1            225.8   225.0   242.3   241.4     7.3     7.3 ++
  string-indexOf-2            169.5   168.4   135.9   133.4   -19.8   -20.7 --
  string-indexOf-3             70.9    70.2    69.7    68.9    -1.7    -1.8   
  string-lastIndexOf-1        506.5   500.1   517.5   509.1     2.2     1.8   
  string-lastIndexOf-2        180.1   179.8   135.5   132.8   -24.8   -26.1 --
  string-lastIndexOf-3        172.5   172.4   146.1   145.7   -15.3   -15.5 --
  string-slice-1              135.9   135.6   134.3   131.3    -1.1    -3.2   
  string-split-1               11.2    11.2    11.2    11.2     0.5     0.5   
  string-split-2               10.8    10.7    10.8    10.7    -0.1    -0.1   
  string-substring-1          138.4   138.3   139.3   138.7     0.6     0.3 + 
  switch-1                   1037.0  1035.8   948.1   946.1    -8.6    -8.7 --
  switch-2                    113.0   112.3   111.8   111.6    -1.1    -0.6 - 
  switch-3                    180.1   179.8   177.3   177.0    -1.6    -1.6 - 
  try-1                       254.2   242.7   276.7   261.9     8.8     7.9 + 
  try-2                        18.0    17.9    18.2    17.9     1.2     0.0   
  try-3                        58.0    56.6    58.7    58.3     1.2     3.0   
  vector-init-1               275.4   267.0   273.9   269.6    -0.6     1.0   
  vector-init-2               349.0   345.2   349.7   339.5     0.2    -1.7   
  vector-init-3               196.0   193.3   194.8   192.1    -0.6    -0.6   
  vector-init-4               158.1   157.7   167.5   167.3     6.0     6.0 ++
vector-nested-read-Number-1   353.9   348.7   354.9   348.4     0.3    -0.1   
vector-nested-read-Number-2   352.6   349.3   349.7   343.1    -0.8    -1.8   
  vector-push-1                62.5    61.1    63.9    62.6     2.3     2.4 + 
  vector-read-C-1             898.1   887.7   878.1   867.2    -2.2    -2.3 - 
  vector-read-C-2             876.1   874.8   893.1   878.7     1.9     0.5 + 
  vector-read-Number-1        892.2   879.1   876.1   875.5    -1.8    -0.4 - 
  vector-read-Number-2        902.1   881.5   898.1   883.3    -0.4     0.2   
  vector-read-char-1          131.9   131.9   134.7   132.5     2.1     0.5 + 
  vector-read-char-2          132.1   132.0   132.1   132.0    -0.0    -0.1   
  vector-read-int-1           875.1   874.9   877.1   876.7     0.2     0.2 + 
  vector-read-int-2           911.1   896.1   911.2   891.9     0.0    -0.5   
  vector-read-int-3           841.2   832.2   829.3   829.2    -1.4    -0.4 - 
  vector-read-int-4           875.1   875.0   910.2   890.2     4.0     1.7 + 
  vector-read-int-5           421.6   415.6   424.2   412.1     0.6    -0.9   
  vector-read-int-6           423.6   412.9   434.6   429.5     2.6     4.0   
  vector-read-int-7           423.6   422.4   413.6   409.3    -2.4    -3.1 - 
  vector-read-length-1        238.0   232.3   236.1   235.6    -0.8     1.4   
  vector-read-uint-1          908.1   893.3   911.1   897.3     0.3     0.4   
  vector-read-uint-2          875.1   875.1   877.1   877.1     0.2     0.2   
  vector-read-uint-3          490.5   477.4   489.0   478.8    -0.3     0.3   
  vector-read-uint-4          909.1   908.5   912.1   911.9     0.3     0.4 + 
  vector-write-Boolean-1       65.2    64.1    65.5    64.7     0.5     0.9   
  vector-write-C-1             96.8    96.1    95.9    93.7    -0.9    -2.5   
  vector-write-C-2             96.4    94.4    95.4    93.7    -1.0    -0.8   
  vector-write-Number-1       802.4   799.2   848.2   846.6     5.7     5.9 ++
  vector-write-Number-2       802.2   786.5   846.2   842.6     5.5     7.1 ++
  vector-write-Object-1        49.8    49.7    49.8    48.6    -0.0    -2.2   
  vector-write-Object-2        50.0    48.6    49.7    49.0    -0.5     0.8   
  vector-write-int-1          861.1   838.6   873.1   851.6     1.4     1.6   
  vector-write-int-2          864.1   840.2   877.1   863.6     1.5     2.8   
  vector-write-int-3          411.6   410.5   445.1   443.4     8.1     8.0 ++
  vector-write-int-4          412.6   411.6   433.6   429.8     5.1     4.4 ++
  vector-write-int-5          406.6   406.5   422.7   420.9     4.0     3.5 + 
  vector-write-uint-1         864.1   860.8   878.1   876.7     1.6     1.9 + 
  vector-write-uint-2         863.1   849.8   878.2   864.0     1.8     1.7   
  while-1                    4722.3  4678.5  4718.3  4715.7    -0.1     0.8
Assignee: nobody → edwsmith
Attachment #602967 - Flags: review?(wmaddox)

Updated

7 years ago
Attachment #602967 - Flags: review?(wmaddox) → review+
(Assignee)

Updated

7 years ago
Blocks: 557926
Priority: -- → P2
Target Milestone: --- → Q2 12 - Cyril

Comment 2

7 years ago
changeset: 7270:c5ecd19ed907
user:      Edwin Smith <edwsmith@adobe.com>
summary:   Bug 733017 - Optimize Function and MethodClosure calls (r=jmott+, wmaddox+)

http://hg.mozilla.org/tamarin-redux/rev/c5ecd19ed907

Comment 3

7 years ago
changeset: 7271:060cd4d36057
user:      Edwin Smith <edwsmith@adobe.com>
summary:   Revert Bug 733017 due to regression in AIR adl test suite

http://hg.mozilla.org/tamarin-redux/rev/060cd4d36057

Comment 4

7 years ago
changeset: 7289:2afc7c30f8e7
user:      Edwin Smith <edwsmith@adobe.com>
summary:   Bug 733017 - Optimize Function and MethodClosure calls (r=wmaddox+,jmott+)

http://hg.mozilla.org/tamarin-redux/rev/2afc7c30f8e7

Comment 5

7 years ago
changeset: 7289:2afc7c30f8e7
user:      Edwin Smith <edwsmith@adobe.com>
summary:   Bug 733017 - Optimize Function and MethodClosure calls (r=wmaddox+,jmott+)

http://hg.mozilla.org/tamarin-redux/rev/2afc7c30f8e7
(Assignee)

Updated

7 years ago
Status: NEW → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → FIXED

Comment 6

7 years ago
Created attachment 614981 [details] [diff] [review]
Removed unused method MethodFrame::toplevel()

It has been pointed out that MethodFrame::toplevel() is potentially error-prone, because it conceals hidden requirement that the MethodFrame be associated with a MethodEnv, while MethodFrame::env() and MethodFrame::cc() return NULL in the case that the frame does not refer to a MethodEnv or a CodeContext, respectively.  It appears, however, that MethodFrame::toplevel() is not used anywhere.  Since it is not used, may be problematic, and is at best only a small notational convenience, I suggest we remove it.
Attachment #614981 - Flags: review?(fklockii)
Comment on attachment 614981 [details] [diff] [review]
Removed unused method MethodFrame::toplevel()

Review of attachment 614981 [details] [diff] [review]:
-----------------------------------------------------------------

my favorite kind of review!  :)
Attachment #614981 - Flags: review?(fklockii) → review+

Comment 8

7 years ago
changeset: 7356:d5a39fd61620
user:      William Maddox <wmaddox@adobe.com>
summary:   Bug 733017: Remove unused MethodFrame::toplevel() (r=fklockii)

http://hg.mozilla.org/tamarin-redux/rev/d5a39fd61620
(In reply to Tamarin Bot from comment #5)
> changeset: 7289:2afc7c30f8e7
> user:      Edwin Smith <edwsmith@adobe.com>
> summary:   Bug 733017 - Optimize Function and MethodClosure calls
> (r=wmaddox+,jmott+)
> 
> http://hg.mozilla.org/tamarin-redux/rev/2afc7c30f8e7

Injected an assertion failure.

See Bug 753120
You need to log in before you can comment on or make changes to this bug.