| 1 |
from rpython.rlib import jit |
| 2 |
from rpython.jit.metainterp.test.support import LLJitMixin |
| 3 |
|
| 4 |
@jit.dont_look_inside |
| 5 |
def escape(x): |
| 6 |
return x |
| 7 |
|
| 8 |
class ImmutableFieldsTests: |
| 9 |
|
| 10 |
def test_fields(self): |
| 11 |
class X(object): |
| 12 |
_immutable_fields_ = ["x"] |
| 13 |
|
| 14 |
def __init__(self, x): |
| 15 |
self.x = x |
| 16 |
|
| 17 |
def f(x): |
| 18 |
y = escape(X(x)) |
| 19 |
return y.x + 5 |
| 20 |
res = self.interp_operations(f, [23]) |
| 21 |
assert res == 28 |
| 22 |
self.check_operations_history(getfield_gc_i=1, int_add=1) |
| 23 |
|
| 24 |
def test_fields_subclass(self): |
| 25 |
class X(object): |
| 26 |
_immutable_fields_ = ["x"] |
| 27 |
|
| 28 |
def __init__(self, x): |
| 29 |
self.x = x |
| 30 |
|
| 31 |
class Y(X): |
| 32 |
_immutable_fields_ = ["y"] |
| 33 |
|
| 34 |
def __init__(self, x, y): |
| 35 |
X.__init__(self, x) |
| 36 |
self.y = y |
| 37 |
|
| 38 |
def f(x, y): |
| 39 |
X(x) |
| 40 |
z = escape(Y(x, y)) |
| 41 |
return z.x + z.y + 5 |
| 42 |
res = self.interp_operations(f, [23, 11]) |
| 43 |
assert res == 39 |
| 44 |
self.check_operations_history(getfield_gc_i=2, int_add=2) |
| 45 |
|
| 46 |
def f(x, y): |
| 47 |
|
| 48 |
z = escape(Y(x, y)) |
| 49 |
return z.x + z.y + 5 |
| 50 |
res = self.interp_operations(f, [23, 11]) |
| 51 |
assert res == 39 |
| 52 |
self.check_operations_history(getfield_gc_i=2, int_add=2) |
| 53 |
|
| 54 |
def test_array(self): |
| 55 |
class X(object): |
| 56 |
_immutable_fields_ = ["y[*]"] |
| 57 |
|
| 58 |
def __init__(self, x): |
| 59 |
self.y = x |
| 60 |
def f(index): |
| 61 |
l = [1, 2, 3, 4] |
| 62 |
l[2] = 30 |
| 63 |
a = escape(X(l)) |
| 64 |
return a.y[index] |
| 65 |
res = self.interp_operations(f, [2], listops=True) |
| 66 |
assert res == 30 |
| 67 |
self.check_operations_history(getfield_gc_r=1, getarrayitem_gc_i=0, getarrayitem_gc_pure_i=1) |
| 68 |
|
| 69 |
def test_array_index_error(self): |
| 70 |
class X(object): |
| 71 |
_immutable_fields_ = ["y[*]"] |
| 72 |
|
| 73 |
def __init__(self, x): |
| 74 |
self.y = x |
| 75 |
|
| 76 |
def get(self, index): |
| 77 |
try: |
| 78 |
return self.y[index] |
| 79 |
except IndexError: |
| 80 |
return -41 |
| 81 |
|
| 82 |
def f(index): |
| 83 |
l = [1, 2, 3, 4] |
| 84 |
l[2] = 30 |
| 85 |
a = escape(X(l)) |
| 86 |
return a.get(index) |
| 87 |
res = self.interp_operations(f, [2], listops=True) |
| 88 |
assert res == 30 |
| 89 |
self.check_operations_history(getfield_gc_r=1, getarrayitem_gc_i=0, getarrayitem_gc_pure_i=1) |
| 90 |
|
| 91 |
def test_array_in_immutable(self): |
| 92 |
class X(object): |
| 93 |
_immutable_ = True |
| 94 |
_immutable_fields_ = ["lst[*]"] |
| 95 |
|
| 96 |
def __init__(self, lst, y): |
| 97 |
self.lst = lst |
| 98 |
self.y = y |
| 99 |
|
| 100 |
def f(x, index): |
| 101 |
y = escape(X([x], x+1)) |
| 102 |
return y.lst[index] + y.y + 5 |
| 103 |
res = self.interp_operations(f, [23, 0], listops=True) |
| 104 |
assert res == 23 + 24 + 5 |
| 105 |
self.check_operations_history(getfield_gc_r=1, getfield_gc_i=1, |
| 106 |
getarrayitem_gc_i=0, getarrayitem_gc_pure_i=1, |
| 107 |
int_add=3) |
| 108 |
|
| 109 |
|
| 110 |
def test_raw_field_and_array(self): |
| 111 |
from rpython.rtyper.lltypesystem import lltype |
| 112 |
X = lltype.Struct('X', |
| 113 |
('a', lltype.Signed), |
| 114 |
('b', lltype.Array(lltype.Signed, |
| 115 |
hints={'nolength': True, 'immutable': True})), |
| 116 |
hints={'immutable': True}) |
| 117 |
|
| 118 |
x = lltype.malloc(X, 4, flavor='raw', immortal=True) |
| 119 |
x.a = 6 |
| 120 |
x.b[2] = 7 |
| 121 |
xlist = [x, lltype.nullptr(X)] |
| 122 |
def g(num): |
| 123 |
if num < 0: |
| 124 |
num = 0 |
| 125 |
return num |
| 126 |
g._dont_inline_ = True |
| 127 |
def f(num): |
| 128 |
num = g(num) |
| 129 |
x = xlist[num] |
| 130 |
return x.a * x.b[2] |
| 131 |
|
| 132 |
res = self.interp_operations(f, [0], disable_optimizations=True) |
| 133 |
assert res == 42 |
| 134 |
self.check_operations_history(getfield_raw_i=1, |
| 135 |
getarrayitem_raw_i=1, |
| 136 |
int_mul=1) |
| 137 |
|
| 138 |
|
| 139 |
res = self.interp_operations(f, [-1], disable_optimizations=True) |
| 140 |
assert res == 42 |
| 141 |
self.check_operations_history(getfield_raw_i=0, |
| 142 |
getarrayitem_raw_i=0, |
| 143 |
int_mul=0) |
| 144 |
|
| 145 |
def test_read_on_promoted(self): |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
class Foo(object): |
| 151 |
_immutable_fields_ = ['n'] |
| 152 |
def __init__(self, n): |
| 153 |
self.n = n |
| 154 |
f1 = Foo(42); f2 = Foo(43) |
| 155 |
@jit.dont_look_inside |
| 156 |
def some(m): |
| 157 |
return [f1, f2][m] |
| 158 |
@jit.dont_look_inside |
| 159 |
def do_stuff_with(n): |
| 160 |
print n |
| 161 |
def main(m): |
| 162 |
f = some(m) |
| 163 |
n = f.n |
| 164 |
f = jit.hint(f, promote=True) |
| 165 |
res = f.n * 6 |
| 166 |
do_stuff_with(n) |
| 167 |
return res |
| 168 |
res = self.interp_operations(main, [1]) |
| 169 |
assert res == 43 * 6 |
| 170 |
self.check_operations_history(int_mul=0) |
| 171 |
|
| 172 |
def test_read_on_promoted_array(self): |
| 173 |
class Foo(object): |
| 174 |
_immutable_fields_ = ['lst[*]'] |
| 175 |
def __init__(self, lst): |
| 176 |
self.lst = lst |
| 177 |
f1 = Foo([42]); f2 = Foo([43]) |
| 178 |
@jit.dont_look_inside |
| 179 |
def some(m): |
| 180 |
return [f1, f2][m] |
| 181 |
@jit.dont_look_inside |
| 182 |
def do_stuff_with(n): |
| 183 |
print n |
| 184 |
def main(m): |
| 185 |
f = some(m) |
| 186 |
n = f.lst[0] |
| 187 |
f = jit.hint(f, promote=True) |
| 188 |
res = f.lst[0] * 6 |
| 189 |
do_stuff_with(n) |
| 190 |
return res |
| 191 |
res = self.interp_operations(main, [1]) |
| 192 |
assert res == 43 * 6 |
| 193 |
self.check_operations_history(int_mul=0) |
| 194 |
|
| 195 |
|
| 196 |
class TestLLtypeImmutableFieldsTests(ImmutableFieldsTests, LLJitMixin): |
| 197 |
pass |