def test_remove_duplicate_casts(): class A(object): def __init__(self, x, y): self.x = x self.y = y def getsum(self): return self.x + self.y class B(A): def __init__(self, x, y, z): A.__init__(self, x, y) self.z = z def getsum(self): return self.x + self.y + self.z def f(x, switch): a = A(x, x + 1) b = B(x, x + 1, x + 2) if switch: c = A(x, x + 1) else: c = B(x, x + 1, x + 2) return a.x + a.y + b.x + b.y + b.z + c.getsum() assert f(10, True) == 75 graph, t = get_graph(f, [int, bool], all_opts=False) num_cast_pointer = len(getops(graph)['cast_pointer']) changed = remove_duplicate_casts(graph, t) assert changed ops = getops(graph) assert len(ops['cast_pointer']) < num_cast_pointer print len(ops['cast_pointer']), num_cast_pointer graph_getsum = graphof(t, B.getsum.im_func) num_cast_pointer = len(getops(graph_getsum)['cast_pointer']) changed = remove_duplicate_casts(graph_getsum, t) assert changed if option.view: t.view() check_graph(graph, [10, True], 75, t) ops = getops(graph_getsum) assert len(ops['cast_pointer']) < num_cast_pointer print len(ops['cast_pointer']), num_cast_pointer
def test_remove_duplicate_casts(): class A(object): def __init__(self, x, y): self.x = x self.y = y def getsum(self): return self.x + self.y class B(A): def __init__(self, x, y, z): A.__init__(self, x, y) self.z = z def getsum(self): return self.x + self.y + self.z def f(x, switch): a = A(x, x + 1) b = B(x, x + 1, x + 2) if switch: c = A(x, x + 1) else: c = B(x, x + 1, x + 2) return a.x + a.y + b.x + b.y + b.z + c.getsum() assert f(10, True) == 75 graph, t = get_graph(f, [int, bool], all_opts=False) num_cast_pointer = len(getops(graph)["cast_pointer"]) changed = remove_duplicate_casts(graph, t) assert changed ops = getops(graph) assert len(ops["cast_pointer"]) < num_cast_pointer print len(ops["cast_pointer"]), num_cast_pointer graph_getsum = graphof(t, B.getsum.im_func) num_cast_pointer = len(getops(graph_getsum)["cast_pointer"]) changed = remove_duplicate_casts(graph_getsum, t) assert changed if option.view: t.view() check_graph(graph, [10, True], 75, t) ops = getops(graph_getsum) assert len(ops["cast_pointer"]) < num_cast_pointer print len(ops["cast_pointer"]), num_cast_pointer
def test_dont_decref_nongc_pointers(): S = lltype.GcStruct('S', ('x', lltype.Ptr(lltype.Struct('T', ('x', lltype.Signed)))), ('y', lltype.Ptr(lltype.GcStruct('Y', ('x', lltype.Signed)))) ) def f(): pass graph, t = make_deallocator(S) ops = getops(graph) assert len(ops['direct_call']) == 1
def test_dont_decref_nongc_pointers(): S = lltype.GcStruct( 'S', ('x', lltype.Ptr(lltype.Struct('T', ('x', lltype.Signed)))), ('y', lltype.Ptr(lltype.GcStruct('Y', ('x', lltype.Signed))))) def f(): pass graph, t = make_deallocator(S) ops = getops(graph) assert len(ops['direct_call']) == 1
def test_deallocator_less_simple(): TPtr = lltype.Ptr(lltype.GcStruct("T", ('a', lltype.Signed))) S = lltype.GcStruct( "S", ('x', lltype.Signed), ('y', TPtr), ('z', TPtr), ) dgraph, t = make_deallocator(S) ops = getops(dgraph) assert len(ops['direct_call']) == 2 assert len(ops['getfield']) == 2 assert len(ops['gc_free']) == 1
def test_boehm_simple(): class C: pass def f(): c = C() c.x = 1 return c.x t, transformer = rtype_and_transform( f, [], BoehmGCTransformer, check=False) ops = getops(graphof(t, f)) assert len(ops.get('direct_call', [])) <= 1 gcs = [k for k in ops if k.startswith('gc')] assert len(gcs) == 0
def test_deallocator_array(): TPtr = lltype.Ptr(lltype.GcStruct("T", ('a', lltype.Signed))) GcA = lltype.GcArray(('x', TPtr), ('y', TPtr)) A = lltype.Array(('x', TPtr), ('y', TPtr)) APtr = lltype.Ptr(GcA) S = lltype.GcStruct('S', ('t', TPtr), ('x', lltype.Signed), ('aptr', APtr), ('rest', A)) dgraph, t = make_deallocator(S) ops = getops(dgraph) assert len(ops['direct_call']) == 4 assert len(ops['getfield']) == 2 assert len(ops['getinteriorfield']) == 2 assert len(ops['getinteriorarraysize']) == 1 assert len(ops['gc_free']) == 1
def test_arraybarrier(): S = lltype.GcStruct("S", ('x', lltype.Signed)) A = lltype.GcArray(lltype.Ptr(S)) def f(): s1 = lltype.malloc(S) s1.x = 1 s2 = lltype.malloc(S) s2.x = 2 a = lltype.malloc(A, 1) a[0] = s1 a[0] = s2 t, transformer = rtype_and_transform(f, [], RefcountingGCTransformer, check=False) graph = graphof(t, f) ops = getops(graph) assert len(ops['getarrayitem']) == 2 assert len(ops['bare_setarrayitem']) == 2 assert len(ops['bare_setfield']) == 2
def test_simple_barrier(): S = lltype.GcStruct("S", ('x', lltype.Signed)) T = lltype.GcStruct("T", ('s', lltype.Ptr(S))) def f(): s1 = lltype.malloc(S) s1.x = 1 s2 = lltype.malloc(S) s2.x = 2 t = lltype.malloc(T) t.s = s1 t.s = s2 return t t, transformer = rtype_and_transform(f, [], RefcountingGCTransformer, check=False) graph = graphof(t, f) ops = getops(graph) assert len(ops['getfield']) == 2 assert len(ops['bare_setfield']) == 4