| 1 |
|
| 2 |
from rpython.annotator.policy import AnnotatorPolicy |
| 3 |
from rpython.flowspace.model import Constant |
| 4 |
from rpython.annotator.classdesc import InstanceSource |
| 5 |
|
| 6 |
def set_attribute(classdesc, value, attr="_static_lookup_cache"): |
| 7 |
cls = classdesc.pyobj |
| 8 |
if attr in cls.__dict__: |
| 9 |
return |
| 10 |
classdesc.classdict[attr] = Constant(value) |
| 11 |
classdesc.immutable_fields.add(attr) |
| 12 |
classdesc.classdef.add_source_for_attribute(attr, classdesc) |
| 13 |
setattr(cls, attr, value) |
| 14 |
|
| 15 |
class PyPyAnnotatorPolicy(AnnotatorPolicy): |
| 16 |
def __init__(self, space): |
| 17 |
self.lookups = set() |
| 18 |
self.lookups_where = set() |
| 19 |
self.pypytypes = {} |
| 20 |
self.types_w = set() |
| 21 |
self.space = space |
| 22 |
|
| 23 |
def consider_lookup(self, bookkeeper, attr): |
| 24 |
from pypy.objspace.std import typeobject |
| 25 |
from pypy.interpreter import baseobjspace |
| 26 |
assert attr not in self.lookups |
| 27 |
cached = "cached_%s" % attr |
| 28 |
clsdef = bookkeeper.getuniqueclassdef(StaticLookupCache) |
| 29 |
classdesc = clsdef.classdesc |
| 30 |
set_attribute(classdesc, None, cached) |
| 31 |
for cls, w_type in self.pypytypes.items(): |
| 32 |
if cls._static_lookup_cache is not None: |
| 33 |
setattr(cls._static_lookup_cache, cached, w_type._lookup(attr)) |
| 34 |
source = InstanceSource(bookkeeper, cls._static_lookup_cache) |
| 35 |
clsdef.add_source_for_attribute(cached, source) |
| 36 |
self.lookups.add(attr) |
| 37 |
|
| 38 |
def consider_lookup_in_type_where(self, bookkeeper, attr): |
| 39 |
assert attr not in self.lookups_where |
| 40 |
from pypy.objspace.std import typeobject |
| 41 |
cached = "cached_where_%s" % attr |
| 42 |
clsdef = bookkeeper.getuniqueclassdef(typeobject.W_TypeObject) |
| 43 |
classdesc = clsdef.classdesc |
| 44 |
classdesc.immutable_fields.add(cached) |
| 45 |
classdesc.classdict[cached] = Constant((None, None)) |
| 46 |
clsdef.add_source_for_attribute(cached, classdesc) |
| 47 |
space = self.space |
| 48 |
w_moduletype = space.type(space.sys) |
| 49 |
for w_t in self.types_w: |
| 50 |
if not (w_t.is_heaptype() or w_t.is_cpytype() or |
| 51 |
space.issubtype_w(w_t, w_moduletype)): |
| 52 |
setattr(w_t, cached, w_t._lookup_where(attr)) |
| 53 |
source = InstanceSource(bookkeeper, w_t) |
| 54 |
clsdef.add_source_for_attribute(cached, source) |
| 55 |
self.lookups_where.add(attr) |
| 56 |
|
| 57 |
def specialize__lookup(self, funcdesc, args_s): |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
s_space, s_obj, s_name = args_s |
| 66 |
if s_name.is_constant(): |
| 67 |
attr = s_name.const |
| 68 |
def builder(translator, func): |
| 69 |
|
| 70 |
self.consider_lookup(funcdesc.bookkeeper, attr) |
| 71 |
d = {'__name__': '<ann_override_lookup>'} |
| 72 |
exec CACHED_LOOKUP % {'attr': attr} in d |
| 73 |
return translator.buildflowgraph(d['lookup_'+attr]) |
| 74 |
return funcdesc.cachedgraph(attr, builder=builder) |
| 75 |
else: |
| 76 |
return funcdesc.cachedgraph(None) |
| 77 |
|
| 78 |
def specialize__lookup_in_type_where(self, funcdesc, args_s): |
| 79 |
|
| 80 |
|
| 81 |
s_space, s_type, s_name = args_s |
| 82 |
if s_name.is_constant(): |
| 83 |
attr = s_name.const |
| 84 |
def builder(translator, func): |
| 85 |
|
| 86 |
self.consider_lookup_in_type_where(funcdesc.bookkeeper, attr) |
| 87 |
d = {'__name__': '<ann_override_lookup>'} |
| 88 |
exec CACHED_LOOKUP_IN_TYPE_WHERE % {'attr': attr} in d |
| 89 |
return translator.buildflowgraph(d['lookup_in_type_where_'+attr]) |
| 90 |
return funcdesc.cachedgraph(attr, builder=builder) |
| 91 |
else: |
| 92 |
return funcdesc.cachedgraph(None) |
| 93 |
|
| 94 |
def event(self, bookkeeper, what, x): |
| 95 |
from pypy.objspace.std import typeobject |
| 96 |
from pypy.interpreter import baseobjspace, module |
| 97 |
if what == "classdef_setup" and issubclass(x.classdesc.pyobj, baseobjspace.W_Root): |
| 98 |
classdesc = x.classdesc |
| 99 |
cls = classdesc.pyobj |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
if (getattr(cls, "typedef", None) is None or |
| 105 |
cls.user_overridden_class or |
| 106 |
issubclass(cls, module.Module)): |
| 107 |
set_attribute(classdesc, None) |
| 108 |
return |
| 109 |
w_type = self.space.gettypeobject(cls.typedef) |
| 110 |
if w_type.is_heaptype() or w_type.is_cpytype(): |
| 111 |
set_attribute(classdesc, None) |
| 112 |
return |
| 113 |
if '_static_lookup_cache' not in cls.__dict__: |
| 114 |
set_attribute(classdesc, StaticLookupCache()) |
| 115 |
assert '_static_lookup_cache' in cls.__dict__ |
| 116 |
else: |
| 117 |
assert 0, "should not be possible" |
| 118 |
self.pypytypes[cls] = w_type |
| 119 |
clsdef = bookkeeper.getuniqueclassdef(StaticLookupCache) |
| 120 |
for attr in self.lookups: |
| 121 |
cached = "cached_%s" % attr |
| 122 |
w_value = w_type._lookup(attr) |
| 123 |
setattr(cls._static_lookup_cache, cached, w_value) |
| 124 |
source = InstanceSource(bookkeeper, cls._static_lookup_cache) |
| 125 |
clsdef.add_source_for_attribute(cached, source) |
| 126 |
if isinstance(x, typeobject.W_TypeObject): |
| 127 |
clsdef = bookkeeper.getuniqueclassdef(typeobject.W_TypeObject) |
| 128 |
self.types_w.add(x) |
| 129 |
|
| 130 |
space = self.space |
| 131 |
w_moduletype = space.type(space.sys) |
| 132 |
for attr in self.lookups_where: |
| 133 |
if not (x.is_heaptype() or x.is_cpytype() or |
| 134 |
space.issubtype_w(x, w_moduletype)): |
| 135 |
cached = "cached_where_%s" % attr |
| 136 |
setattr(x, cached, x._lookup_where(attr)) |
| 137 |
source = InstanceSource(bookkeeper, x) |
| 138 |
clsdef.add_source_for_attribute(cached, source) |
| 139 |
return |
| 140 |
|
| 141 |
class StaticLookupCache(object): |
| 142 |
pass |
| 143 |
|
| 144 |
CACHED_LOOKUP = """ |
| 145 |
def lookup_%(attr)s(space, w_obj, name): |
| 146 |
cache = w_obj._static_lookup_cache |
| 147 |
if cache is None: |
| 148 |
w_type = space.type(w_obj) |
| 149 |
return w_type.lookup("%(attr)s") |
| 150 |
return cache.cached_%(attr)s |
| 151 |
""" |
| 152 |
|
| 153 |
CACHED_LOOKUP_IN_TYPE_WHERE = """ |
| 154 |
def lookup_in_type_where_%(attr)s(space, w_type, name): |
| 155 |
if not w_type.is_heaptype() and not w_type.is_cpytype(): |
| 156 |
return w_type.cached_where_%(attr)s |
| 157 |
return w_type.lookup_where("%(attr)s") |
| 158 |
""" |