| 1 |
""" |
| 2 |
Two bytecodes to speed up method calls. Here is how a method call looks |
| 3 |
like: (on the left, without the new bytecodes; on the right, with them) |
| 4 |
|
| 5 |
<push self> <push self> |
| 6 |
LOAD_ATTR name LOAD_METHOD name |
| 7 |
<push arg 0> <push arg 0> |
| 8 |
... ... |
| 9 |
<push arg n-1> <push arg n-1> |
| 10 |
CALL_FUNCTION n CALL_METHOD n |
| 11 |
""" |
| 12 |
|
| 13 |
from pypy.interpreter import function |
| 14 |
from rpython.rlib import jit |
| 15 |
from pypy.objspace.std.mapdict import LOAD_METHOD_mapdict, \ |
| 16 |
LOAD_METHOD_mapdict_fill_cache_method |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
def LOAD_METHOD(f, nameindex, *ignored): |
| 26 |
from pypy.objspace.std.typeobject import MutableCell |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
space = f.space |
| 35 |
w_obj = f.popvalue() |
| 36 |
|
| 37 |
if not jit.we_are_jitted(): |
| 38 |
|
| 39 |
if LOAD_METHOD_mapdict(f, nameindex, w_obj): |
| 40 |
return |
| 41 |
|
| 42 |
w_name = f.getname_w(nameindex) |
| 43 |
w_value = None |
| 44 |
|
| 45 |
w_type = space.type(w_obj) |
| 46 |
if w_type.has_object_getattribute(): |
| 47 |
name = space.text_w(w_name) |
| 48 |
|
| 49 |
|
| 50 |
version_tag = w_type.version_tag() |
| 51 |
if version_tag is None: |
| 52 |
_, w_descr = w_type._lookup_where(name) |
| 53 |
w_descr_cell = None |
| 54 |
else: |
| 55 |
_, w_descr_cell = w_type._pure_lookup_where_with_method_cache( |
| 56 |
name, version_tag) |
| 57 |
w_descr = w_descr_cell |
| 58 |
if isinstance(w_descr, MutableCell): |
| 59 |
w_descr = w_descr.unwrap_cell(space) |
| 60 |
if w_descr is None: |
| 61 |
|
| 62 |
|
| 63 |
w_value = w_obj.getdictvalue(space, name) |
| 64 |
|
| 65 |
else: |
| 66 |
if space.type(w_descr).flag_method_descriptor: |
| 67 |
w_value = w_obj.getdictvalue(space, name) |
| 68 |
if w_value is None: |
| 69 |
|
| 70 |
|
| 71 |
f.pushvalue(w_descr) |
| 72 |
f.pushvalue(w_obj) |
| 73 |
if not jit.we_are_jitted(): |
| 74 |
|
| 75 |
LOAD_METHOD_mapdict_fill_cache_method( |
| 76 |
space, f.getcode(), name, nameindex, w_obj, w_type, |
| 77 |
w_descr_cell) |
| 78 |
return |
| 79 |
if w_value is None: |
| 80 |
w_value = space.getattr(w_obj, w_name) |
| 81 |
f.pushvalue(w_value) |
| 82 |
f.pushvalue_none() |
| 83 |
|
| 84 |
@jit.unroll_safe |
| 85 |
def CALL_METHOD(f, oparg, *ignored): |
| 86 |
|
| 87 |
n_args = oparg |
| 88 |
w_self = f.peekvalue_maybe_none(n_args) |
| 89 |
n = n_args + (w_self is not None) |
| 90 |
|
| 91 |
w_callable = f.peekvalue(n_args + 1) |
| 92 |
w_result = f.space.call_valuestack( |
| 93 |
w_callable, n, f, methodcall=w_self is not None, dropvalues=n_args+2) |
| 94 |
f.pushvalue(w_result) |
| 95 |
|
| 96 |
@jit.unroll_safe |
| 97 |
def CALL_METHOD_KW(f, n_arguments, *ignored): |
| 98 |
from pypy.objspace.std.tupleobject import W_AbstractTupleObject |
| 99 |
|
| 100 |
w_self = f.peekvalue_maybe_none(n_arguments + 1) |
| 101 |
|
| 102 |
space = f.space |
| 103 |
|
| 104 |
|
| 105 |
w_tup_varnames = space.interp_w(W_AbstractTupleObject, f.popvalue()) |
| 106 |
n_keywords = space.len_w(w_tup_varnames) |
| 107 |
keyword_names_w = [None] * n_keywords |
| 108 |
keywords_w = [None] * n_keywords |
| 109 |
for i in range(n_keywords): |
| 110 |
keyword_names_w[i] = w_tup_varnames.getitem(space, i) |
| 111 |
w_value = f.peekvalue(n_keywords - 1 - i) |
| 112 |
keywords_w[i] = w_value |
| 113 |
f.dropvalues(n_keywords) |
| 114 |
n_arguments -= n_keywords |
| 115 |
n = n_arguments + (w_self is not None) |
| 116 |
arguments = f.popvalues(n) |
| 117 |
if w_self is None: |
| 118 |
f.popvalue_maybe_none() |
| 119 |
w_callable = f.popvalue() |
| 120 |
args = f.argument_factory( |
| 121 |
arguments, keyword_names_w, keywords_w, None, None, |
| 122 |
methodcall=w_self is not None, w_function=w_callable) |
| 123 |
if f.get_is_being_profiled() and function.is_builtin_code(w_callable): |
| 124 |
w_result = f.space.call_args_and_c_profile(f, w_callable, args) |
| 125 |
else: |
| 126 |
w_result = f.space.call_args(w_callable, args) |
| 127 |
f.pushvalue(w_result) |
| 128 |
|
| 129 |
|
| 130 |
def call_method_opt(space, w_obj, methname, *arg_w): |
| 131 |
"""An optimized version of space.call_method() |
| 132 |
based on the same principle as above. |
| 133 |
""" |
| 134 |
w_type = space.type(w_obj) |
| 135 |
if w_type.has_object_getattribute(): |
| 136 |
w_descr = space.lookup(w_obj, methname) |
| 137 |
if w_descr is not None and space.type(w_descr).flag_method_descriptor: |
| 138 |
w_value = w_obj.getdictvalue(space, methname) |
| 139 |
if w_value is None: |
| 140 |
|
| 141 |
|
| 142 |
return space.call_function(w_descr, w_obj, *arg_w) |
| 143 |
w_name = space.newtext(methname) |
| 144 |
w_meth = space.getattr(w_obj, w_name) |
| 145 |
return space.call_function(w_meth, *arg_w) |