| 1 |
|
| 2 |
import py |
| 3 |
import sys |
| 4 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 5 |
|
| 6 |
class TestInstance(BaseTestPyPyC): |
| 7 |
|
| 8 |
def test_virtual_instance(self): |
| 9 |
def main(n): |
| 10 |
class A(object): |
| 11 |
pass |
| 12 |
|
| 13 |
i = 0 |
| 14 |
while i < n: |
| 15 |
a = A() |
| 16 |
assert isinstance(a, A) |
| 17 |
assert not isinstance(a, int) |
| 18 |
a.x = 2 |
| 19 |
i = i + a.x |
| 20 |
return i |
| 21 |
|
| 22 |
log = self.run(main, [1000], threshold = 400) |
| 23 |
assert log.result == 1000 |
| 24 |
loop, = log.loops_by_filename(self.filepath) |
| 25 |
assert loop.match(""" |
| 26 |
i7 = int_lt(i5, i6) |
| 27 |
guard_true(i7, descr=...) |
| 28 |
guard_not_invalidated(descr=...) |
| 29 |
i9 = int_add_ovf(i5, 2) |
| 30 |
guard_no_overflow(descr=...) |
| 31 |
--TICK-- |
| 32 |
jump(..., descr=...) |
| 33 |
""") |
| 34 |
|
| 35 |
def test_load_attr(self): |
| 36 |
src = ''' |
| 37 |
class A(object): |
| 38 |
pass |
| 39 |
a = A() |
| 40 |
a.x = 1 |
| 41 |
def main(n): |
| 42 |
i = 0 |
| 43 |
while i < n: |
| 44 |
i = i + a.x |
| 45 |
return i |
| 46 |
''' |
| 47 |
log = self.run(src, [1000]) |
| 48 |
assert log.result == 1000 |
| 49 |
loop, = log.loops_by_filename(self.filepath) |
| 50 |
assert loop.match(""" |
| 51 |
i9 = int_lt(i5, i6) |
| 52 |
guard_true(i9, descr=...) |
| 53 |
guard_not_invalidated(descr=...) |
| 54 |
i10 = int_add(i5, 1) |
| 55 |
--TICK-- |
| 56 |
jump(..., descr=...) |
| 57 |
""") |
| 58 |
|
| 59 |
def test_getattr_with_dynamic_attribute(self): |
| 60 |
src = """ |
| 61 |
class A(object): |
| 62 |
pass |
| 63 |
|
| 64 |
l = ["x", "y"] |
| 65 |
|
| 66 |
def main(): |
| 67 |
sum = 0 |
| 68 |
a = A() |
| 69 |
a.a1 = 0 |
| 70 |
a.a2 = 0 |
| 71 |
a.a3 = 0 |
| 72 |
a.a4 = 0 |
| 73 |
a.a5 = 0 # workaround, because the first five attributes need a promotion |
| 74 |
a.x = 1 |
| 75 |
a.y = 2 |
| 76 |
i = 0 |
| 77 |
while i < 500: |
| 78 |
name = l[i % 2] |
| 79 |
sum += getattr(a, name) |
| 80 |
i += 1 |
| 81 |
return sum |
| 82 |
""" |
| 83 |
log = self.run(src, []) |
| 84 |
assert log.result == 250 + 250*2 |
| 85 |
loops = log.loops_by_filename(self.filepath) |
| 86 |
assert len(loops) == 1 |
| 87 |
|
| 88 |
def test_mutate_class_int(self): |
| 89 |
def fn(n): |
| 90 |
class A(object): |
| 91 |
count = 1 |
| 92 |
def __init__(self, a): |
| 93 |
self.a = a |
| 94 |
def f(self): |
| 95 |
return self.count |
| 96 |
i = 0 |
| 97 |
a = A(1) |
| 98 |
while i < n: |
| 99 |
A.count += 1 |
| 100 |
i = a.f() |
| 101 |
return i |
| 102 |
|
| 103 |
log = self.run(fn, [1000], threshold=10) |
| 104 |
assert log.result == 1000 |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
entry_bridge, = log.loops_by_filename(self.filepath, is_entry_bridge=True) |
| 109 |
ops = entry_bridge.ops_by_id('mutate', opcode='LOAD_ATTR') |
| 110 |
assert log.opnames(ops) == ['guard_value', |
| 111 |
'guard_not_invalidated', |
| 112 |
'getfield_gc_i'] |
| 113 |
|
| 114 |
assert list(entry_bridge.ops_by_id('meth1', opcode='STORE_ATTR')) == [] |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
loop, = log.loops_by_filename(self.filepath) |
| 119 |
assert loop.match(""" |
| 120 |
i58 = int_lt(i38, i31) |
| 121 |
guard_true(i58, descr=...) |
| 122 |
guard_not_invalidated(descr=...) |
| 123 |
i59 = int_add_ovf(i57, 1) |
| 124 |
guard_no_overflow(descr=...) |
| 125 |
p60 = force_token() |
| 126 |
i61 = getfield_raw_i(..., descr=...) |
| 127 |
setfield_gc(ConstPtr(ptr39), i59, descr=...) |
| 128 |
i62 = int_lt(i61, 0) |
| 129 |
guard_false(i62, descr=...) |
| 130 |
jump(..., descr=...) |
| 131 |
""") |
| 132 |
|
| 133 |
def test_mutate_class(self): |
| 134 |
def fn(n): |
| 135 |
class LL(object): |
| 136 |
def __init__(self, n): |
| 137 |
self.n = n |
| 138 |
class A(object): |
| 139 |
count = None |
| 140 |
def __init__(self, a): |
| 141 |
self.a = a |
| 142 |
def f(self): |
| 143 |
return self.count |
| 144 |
i = 0 |
| 145 |
a = A(1) |
| 146 |
while i < n: |
| 147 |
A.count = LL(A.count) |
| 148 |
a.f() |
| 149 |
i += 1 |
| 150 |
return i |
| 151 |
|
| 152 |
log = self.run(fn, [1000], threshold=10) |
| 153 |
assert log.result == 1000 |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
entry_bridge, = log.loops_by_filename(self.filepath, is_entry_bridge=True) |
| 158 |
ops = entry_bridge.ops_by_id('mutate', opcode='LOAD_ATTR') |
| 159 |
assert log.opnames(ops) == ['guard_value', |
| 160 |
'guard_not_invalidated', |
| 161 |
'getfield_gc_r', 'guard_nonnull_class', |
| 162 |
'getfield_gc_r', 'guard_value', |
| 163 |
] |
| 164 |
|
| 165 |
assert list(entry_bridge.ops_by_id('meth1', opcode='STORE_ATTR')) == [] |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
loop, = log.loops_by_filename(self.filepath) |
| 170 |
assert loop.match(""" |
| 171 |
i70 = int_lt(i58, i33) |
| 172 |
guard_true(i70, descr=...) |
| 173 |
guard_not_invalidated(descr=...) |
| 174 |
p71 = getfield_gc_r(p64, descr=...) |
| 175 |
guard_value(p71, ConstPtr(ptr42), descr=...) |
| 176 |
p72 = force_token() |
| 177 |
p73 = force_token() |
| 178 |
i74 = int_add(i58, 1) |
| 179 |
i75 = getfield_raw_i(..., descr=...) |
| 180 |
i76 = int_lt(i75, 0) |
| 181 |
guard_false(i76, descr=...) |
| 182 |
p77 = new_with_vtable(descr=...) |
| 183 |
setfield_gc(p77, p64, descr=...) |
| 184 |
setfield_gc(p77, ConstPtr(null), descr=...) |
| 185 |
setfield_gc(p77, ConstPtr(null), descr=...) |
| 186 |
setfield_gc(p77, ConstPtr(null), descr=...) |
| 187 |
setfield_gc(p77, ConstPtr(null), descr=...) |
| 188 |
setfield_gc(p77, ConstPtr(ptr42), descr=...) |
| 189 |
setfield_gc(ConstPtr(ptr69), p77, descr=...) |
| 190 |
jump(..., descr=...) |
| 191 |
|
| 192 |
""") |
| 193 |
|
| 194 |
def test_python_contains(self): |
| 195 |
def main(): |
| 196 |
class A(object): |
| 197 |
def __contains__(self, v): |
| 198 |
return True |
| 199 |
|
| 200 |
i = 0 |
| 201 |
a = A() |
| 202 |
while i < 100: |
| 203 |
i += i in a |
| 204 |
b = 0 |
| 205 |
|
| 206 |
log = self.run(main, [], threshold=80) |
| 207 |
loop, = log.loops_by_filename(self.filepath) |
| 208 |
assert loop.match_by_id("contains", """ |
| 209 |
guard_not_invalidated(descr=...) |
| 210 |
i11 = force_token() |
| 211 |
i12 = int_add(i5, 1) |
| 212 |
""") |
| 213 |
|
| 214 |
def test_id_compare_optimization(self): |
| 215 |
def main(): |
| 216 |
class A(object): |
| 217 |
pass |
| 218 |
|
| 219 |
i = 0 |
| 220 |
a = A() |
| 221 |
while i < 300: |
| 222 |
new_a = A() |
| 223 |
if new_a != a: |
| 224 |
pass |
| 225 |
i += 1 |
| 226 |
return i |
| 227 |
|
| 228 |
log = self.run(main, []) |
| 229 |
loop, = log.loops_by_filename(self.filepath) |
| 230 |
assert loop.match_by_id("compare", "") |
| 231 |
|
| 232 |
def test_super(self): |
| 233 |
def main(): |
| 234 |
class A(object): |
| 235 |
def m(self, x): |
| 236 |
return x + 1 |
| 237 |
class B(A): |
| 238 |
def m(self, x): |
| 239 |
return super(B, self).m(x) |
| 240 |
i = 0 |
| 241 |
while i < 300: |
| 242 |
i = B().m(i) |
| 243 |
return i |
| 244 |
|
| 245 |
log = self.run(main, []) |
| 246 |
loop, = log.loops_by_filename(self.filepath) |
| 247 |
assert loop.match(""" |
| 248 |
i78 = int_lt(i72, 300) |
| 249 |
guard_true(i78, descr=...) |
| 250 |
guard_not_invalidated(descr=...) |
| 251 |
i79 = force_token() |
| 252 |
i80 = force_token() |
| 253 |
i81 = int_add(i72, 1) |
| 254 |
--TICK-- |
| 255 |
jump(..., descr=...) |
| 256 |
""") |
| 257 |
|
| 258 |
def test_super_no_args(self): |
| 259 |
def main(): |
| 260 |
class A(object): |
| 261 |
def m(self, x): |
| 262 |
return x + 1 |
| 263 |
class B(A): |
| 264 |
def m(self, x): |
| 265 |
return super().m(x) |
| 266 |
i = 0 |
| 267 |
while i < 300: |
| 268 |
i = B().m(i) |
| 269 |
return i |
| 270 |
|
| 271 |
log = self.run(main, []) |
| 272 |
loop, = log.loops_by_filename(self.filepath) |
| 273 |
assert loop.match(""" |
| 274 |
i78 = int_lt(i72, 300) |
| 275 |
guard_true(i78, descr=...) |
| 276 |
guard_not_invalidated(descr=...) |
| 277 |
p1 = force_token() |
| 278 |
p65 = force_token() |
| 279 |
p3 = force_token() |
| 280 |
i81 = int_add(i72, 1) |
| 281 |
|
| 282 |
# can't use TICK here, because of the extra setfield_gc |
| 283 |
ticker0 = getfield_raw_i(#, descr=<FieldS pypysig_long_struct_inner.c_value .*>) |
| 284 |
setfield_gc(p0, p65, descr=<FieldP pypy.interpreter.pyframe.PyFrame.vable_token .>) |
| 285 |
ticker_cond0 = int_lt(ticker0, 0) |
| 286 |
guard_false(ticker_cond0, descr=...) |
| 287 |
|
| 288 |
jump(..., descr=...) |
| 289 |
""") |
| 290 |
|
| 291 |
def test_float_instance_field_read(self): |
| 292 |
def main(): |
| 293 |
class A(object): |
| 294 |
def __init__(self, x, y): |
| 295 |
self.x = float(x) |
| 296 |
self.y = float(y) |
| 297 |
|
| 298 |
l = [A(i, i * 5) for i in range(2000)] |
| 299 |
|
| 300 |
res = 0 |
| 301 |
for x in l: |
| 302 |
res += x.x + x.y |
| 303 |
return res |
| 304 |
log = self.run(main, []) |
| 305 |
listcomp, loop, = log.loops_by_filename(self.filepath) |
| 306 |
if sys.maxint == 2**63 - 1: |
| 307 |
loop.match_by_id('get', """ |
| 308 |
p67 = getfield_gc_r(p63, descr=...) # map |
| 309 |
guard_value(p67, ConstPtr(ptr68), descr=...) # promote map |
| 310 |
guard_not_invalidated(descr=...) |
| 311 |
p69 = getfield_gc_r(p63, descr=...) # value0 |
| 312 |
i71 = getarrayitem_gc_i(p69, 0, descr=...) # x |
| 313 |
f71 = convert_longlong_bytes_to_float(i71) |
| 314 |
i73 = getarrayitem_gc_i(p69, 1, descr=...) # y |
| 315 |
f73 = convert_longlong_bytes_to_float(i73) |
| 316 |
f74 = float_add(f71, f73) # add them |
| 317 |
f75 = float_add(f57, f74) |
| 318 |
--TICK-- |
| 319 |
""") |
| 320 |
else: |
| 321 |
loop.match_by_id('get', """ |
| 322 |
p67 = getfield_gc_r(p63, descr=...) # map |
| 323 |
guard_value(p67, ConstPtr(ptr68), descr=...) # promote map |
| 324 |
guard_not_invalidated(descr=...) |
| 325 |
p69 = getfield_gc_r(p63, descr=...) # value0 |
| 326 |
f70 = getarrayitem_gc_f(p69, 0, descr=...) # x |
| 327 |
f71 = convert_longlong_bytes_to_float(f70) |
| 328 |
f90 = getarrayitem_gc_f(p69, 1, descr=...) # y |
| 329 |
f73 = convert_longlong_bytes_to_float(f90) |
| 330 |
f74 = float_add(f71, f73) # add them |
| 331 |
f75 = float_add(f57, f74) |
| 332 |
--TICK-- |
| 333 |
""") |
| 334 |
|
| 335 |
def test_float_instance_field_write(self): |
| 336 |
def main(): |
| 337 |
class A(object): |
| 338 |
def __init__(self, x): |
| 339 |
self.x = float(x) |
| 340 |
|
| 341 |
l = [A(i) for i in range(2000)] |
| 342 |
|
| 343 |
for a in l: |
| 344 |
a.x += 3.4 |
| 345 |
log = self.run(main, []) |
| 346 |
listcomp, loop, = log.loops_by_filename(self.filepath) |
| 347 |
if sys.maxint == 2**63 - 1: |
| 348 |
loop.match_by_id('set', """ |
| 349 |
p60 = getfield_gc_r(p56, descr=...) # map |
| 350 |
guard_value(p60, ConstPtr(ptr61), descr=...) |
| 351 |
guard_not_invalidated(descr=...) |
| 352 |
p62 = getfield_gc_r(p56, descr=...) # value |
| 353 |
i64 = getarrayitem_gc_i(p62, 0, descr=...) # x |
| 354 |
f64 = convert_longlong_bytes_to_float(i64) |
| 355 |
f66 = float_add(f64, 3.400000) |
| 356 |
i66 = convert_float_bytes_to_longlong(f66) |
| 357 |
i68 = getfield_raw_i(..., descr=...) |
| 358 |
setarrayitem_gc(p62, 0, i66, descr=...) # store x |
| 359 |
i71 = int_lt(i68, 0) |
| 360 |
guard_false(i71, descr=...) |
| 361 |
""") |
| 362 |
else: |
| 363 |
loop.match_by_id('set', """ |
| 364 |
p60 = getfield_gc_r(p56, descr=...) # map |
| 365 |
guard_value(p60, ConstPtr(ptr61), descr=...) |
| 366 |
guard_not_invalidated(descr=...) |
| 367 |
p62 = getfield_gc_r(p56, descr=...) # value |
| 368 |
f61 = getarrayitem_gc_f(p62, 0, descr=...) # x |
| 369 |
f64 = convert_longlong_bytes_to_float(f61) |
| 370 |
f66 = float_add(f64, 3.400000) |
| 371 |
f67 = convert_float_bytes_to_longlong(f66) |
| 372 |
i68 = getfield_raw_i(..., descr=...) |
| 373 |
setarrayitem_gc(p62, 0, f67, descr=...) # store x |
| 374 |
i71 = int_lt(i68, 0) |
| 375 |
guard_false(i71, descr=...) |
| 376 |
""") |
| 377 |
|
| 378 |
|
| 379 |
def test_namedtuple_construction(self): |
| 380 |
def main(): |
| 381 |
from collections import namedtuple |
| 382 |
A = namedtuple("A", "x y") |
| 383 |
res = 0 |
| 384 |
i = 0 |
| 385 |
while i < 2000: |
| 386 |
res += A(i, 0).x |
| 387 |
i += 1 |
| 388 |
log = self.run(main, []) |
| 389 |
loop, = log.loops_by_filename(self.filepath) |
| 390 |
assert loop.match(""" |
| 391 |
i7 = int_lt(i5, 2000) |
| 392 |
guard_true(i7, descr=...) |
| 393 |
guard_not_invalidated(descr=...) |
| 394 |
p1 = force_token() |
| 395 |
p2 = force_token() |
| 396 |
i20 = int_add_ovf(i19, i5) |
| 397 |
guard_no_overflow(descr=...) |
| 398 |
i9 = int_add(i5, 1) |
| 399 |
--TICK-- |
| 400 |
jump(..., descr=...) |
| 401 |
""") |
| 402 |
|
| 403 |
def test_add_instance_attr_big(self): |
| 404 |
def main(): |
| 405 |
class A(object): |
| 406 |
pass |
| 407 |
l = [] |
| 408 |
for i in range(10000): |
| 409 |
a = A() |
| 410 |
a.attr1 = 'a' |
| 411 |
a.attr2 = 'a' |
| 412 |
a.attr3 = 'a' |
| 413 |
a.attr4 = 'a' |
| 414 |
a.attr5 = 'a' |
| 415 |
a.attr6 = 'a' |
| 416 |
l.append(a) |
| 417 |
|
| 418 |
for a in l: |
| 419 |
a.attr7 = 'b' |
| 420 |
|
| 421 |
log = self.run(main, []) |
| 422 |
loop = log.loops_by_filename(self.filepath, is_entry_bridge=True)[1] |
| 423 |
ops = loop.ops_by_id('grow') |
| 424 |
assert "call_n" not in log.opnames(ops) |
| 425 |
|
| 426 |
def test_add_instance_attr_unboxed(self): |
| 427 |
def main(): |
| 428 |
class A(object): |
| 429 |
pass |
| 430 |
l = [] |
| 431 |
for i in range(10000): |
| 432 |
a = A() |
| 433 |
a.attr1 = 'a' |
| 434 |
a.attr2 = 'a' |
| 435 |
a.attr3 = 'a' |
| 436 |
a.attr4 = 'a' |
| 437 |
a.attr5 = 14 |
| 438 |
l.append(a) |
| 439 |
|
| 440 |
for a in l: |
| 441 |
a.attr7 = 12 |
| 442 |
|
| 443 |
log = self.run(main, []) |
| 444 |
loop = log.loops_by_filename(self.filepath, is_entry_bridge=True)[1] |
| 445 |
ops = loop.ops_by_id('grow') |
| 446 |
assert "call_n" not in log.opnames(ops) |
| 447 |
|
| 448 |
def test_aliasing_via_class(self): |
| 449 |
def main(): |
| 450 |
class A(object): |
| 451 |
pass |
| 452 |
class B(object): |
| 453 |
pass |
| 454 |
a = A() |
| 455 |
a.x = "hallo" |
| 456 |
l1 = [B(), B()] |
| 457 |
l1[0].x = 'hallo' |
| 458 |
l1[1].x = 'tchüss' |
| 459 |
l = [None, "abc"] |
| 460 |
res = 0 |
| 461 |
for i in range(1000): |
| 462 |
b = l1[i & 1] |
| 463 |
s = b.x |
| 464 |
res += len(s) |
| 465 |
a.x = l[i & 1] |
| 466 |
|
| 467 |
res += len(b.x) |
| 468 |
return res |
| 469 |
log = self.run(main, []) |
| 470 |
loop, = log.loops_by_filename(self.filepath) |
| 471 |
loop.match_by_id('getattr2', ''' |
| 472 |
setfield_gc(p14, p105, descr=...) # this setfield is the delayed write to a.x |
| 473 |
i106 = int_add_ovf(i103, i102) |
| 474 |
guard_no_overflow(descr=...) |
| 475 |
--TICK-- |
| 476 |
''') |