def f(): a1 = lltype.malloc(TP, 3) a2 = lltype.malloc(TP, 3) for i in range(3): a1[i].x = 2 * i a1[i].y = 2 * i + 1 rgc.ll_arraycopy(a1, a2, 0, 0, 3) for i in range(3): assert a2[i].x == 2 * i assert a2[i].y == 2 * i + 1
def fn(): l = lltype.malloc(TP, 100) for i in range(100): l[i] = lltype.malloc(TP.OF.TO, i) l2 = lltype.malloc(TP, 50) rgc.ll_arraycopy(l, l2, 40, 0, 50) rgc.collect() for i in range(50): assert l2[i] == l[40 + i] return 0
def ll_list2fixed(l): n = l.length olditems = l.items if n == len(olditems): return olditems else: LIST = typeOf(l).TO newitems = malloc(LIST.items.TO, n) rgc.ll_arraycopy(olditems, newitems, 0, 0, n) return newitems
def fn(): l = lltype.malloc(TP, 100) l2 = lltype.malloc(TP, 100) for i in range(100): l[i] = lltype.malloc(S) rgc.ll_arraycopy(l, l2, 50, 0, 50) # force nursery collect x = [] for i in range(20): x.append((1, lltype.malloc(S))) for i in range(50): assert l2[i] == l[50 + i] return 0
def fn(): l = lltype.malloc(TP, 100) for i in range(100): l[i] = i * 3 l2 = lltype.malloc(TP, 50) rgc.ll_arraycopy(l, l2, 40, 0, 50) # force a nursery collect x = [] for i in range(20): x.append((1, lltype.malloc(S))) for i in range(50): assert l2[i] == (40 + i) * 3 return 0
def test_ll_arraycopy_1(): TYPE = lltype.GcArray(lltype.Signed) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) for i in range(10): a1[i] = 100 + i for i in range(6): a2[i] = 200 + i rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == 100 + i for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i+2] else: assert a2[i] == 200 + i
def test_ll_arraycopy_small(): TYPE = lltype.GcArray(lltype.Signed) for length in range(5): a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) org1 = range(20, 30) org2 = range(50, 56) for i in range(len(a1)): a1[i] = org1[i] for i in range(len(a2)): a2[i] = org2[i] rgc.ll_arraycopy(a1, a2, 4, 2, length) for i in range(10): assert a1[i] == org1[i] for i in range(6): if 2 <= i < 2 + length: assert a2[i] == a1[i+2] else: assert a2[i] == org2[i]
def test_ll_arraycopy_4(): S = lltype.GcStruct('S') TYPE = lltype.GcArray(lltype.Ptr(S)) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) org1 = [None] * 10 org2 = [None] * 6 for i in range(10): a1[i] = org1[i] = lltype.malloc(S) for i in range(6): a2[i] = org2[i] = lltype.malloc(S) rgc.ll_arraycopy(a1, a2, 4, 2, 3) for i in range(10): assert a1[i] == org1[i] for i in range(6): if 2 <= i < 5: assert a2[i] == a1[i+2] else: assert a2[i] == org2[i]
def ll_dict_grow(d): # note: this @jit.look_inside_iff is here to inline the three lines # at the end of this function. It's important because dicts start # with a length-zero 'd.entries' which must be grown as soon as we # insert an element. if d.num_live_items < d.num_ever_used_items // 2: # At least 50% of the allocated entries are dead, so perform a # compaction. If ll_dict_remove_deleted_items detects that over # 75% of allocated entries are dead, then it will also shrink the # memory allocated at the same time as doing a compaction. ll_dict_remove_deleted_items(d) return True new_allocated = _overallocate_entries_len(len(d.entries)) # Detect a relatively rare case where the indexes numeric type is too # small to store all the entry indexes: there would be 'new_allocated' # entries, which may in corner cases be larger than 253 even though we # have single bytes in 'd.indexes' (and the same for the larger # boundaries). The 'd.indexes' hashtable is never more than 2/3rd # full, so we know that 'd.num_live_items' should be at most 2/3 * 256 # (or 65536 or etc.) so after the ll_dict_remove_deleted_items() below # at least 1/3rd items in 'd.entries' are free. fun = d.lookup_function_no & FUNC_MASK toobig = False if fun == FUNC_BYTE: assert d.num_live_items < ((1 << 8) - MIN_INDEXES_MINUS_ENTRIES) toobig = new_allocated > ((1 << 8) - MIN_INDEXES_MINUS_ENTRIES) elif fun == FUNC_SHORT: assert d.num_live_items < ((1 << 16) - MIN_INDEXES_MINUS_ENTRIES) toobig = new_allocated > ((1 << 16) - MIN_INDEXES_MINUS_ENTRIES) elif IS_64BIT and fun == FUNC_INT: assert d.num_live_items < ((1 << 32) - MIN_INDEXES_MINUS_ENTRIES) toobig = new_allocated > ((1 << 32) - MIN_INDEXES_MINUS_ENTRIES) # if toobig: ll_dict_remove_deleted_items(d) assert d.num_live_items == d.num_ever_used_items return True newitems = lltype.malloc(lltype.typeOf(d).TO.entries.TO, new_allocated) rgc.ll_arraycopy(d.entries, newitems, 0, 0, len(d.entries)) d.entries = newitems return False
def fn(): l1 = lltype.malloc(TP, 65) l2 = lltype.malloc(TP, 33) for i in range(65): l1[i] = lltype.malloc(S) l = lltype.malloc(TP, 100) i = 0 while i < 65: l[i] = l1[i] i += 1 rgc.ll_arraycopy(l, l2, 0, 0, 33) x = [] # force minor collect t = (1, lltype.malloc(S)) for i in range(20): x.append(t) for i in range(33): assert l2[i] == l[i] return 0
def _ll_list_resize_hint_really(l, newsize, overallocate): """ Ensure l.items has room for at least newsize elements. Note that l.items may change, and even if newsize is less than l.length on entry. """ # This over-allocates proportional to the list size, making room # for additional growth. The over-allocation is mild, but is # enough to give linear-time amortized behavior over a long # sequence of appends() in the presence of a poorly-performing # system malloc(). # The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ... if newsize <= 0: ll_assert(newsize == 0, "negative list length") l.length = 0 l.items = _ll_new_empty_item_array(typeOf(l).TO) return elif overallocate: if newsize < 9: some = 3 else: some = 6 some += newsize >> 3 new_allocated = newsize + some else: new_allocated = newsize # new_allocated is a bit more than newsize, enough to ensure an amortized # linear complexity for e.g. repeated usage of l.append(). In case # it overflows sys.maxint, it is guaranteed negative, and the following # malloc() will fail. items = l.items newitems = malloc(typeOf(l).TO.items.TO, new_allocated) before_len = l.length if before_len: # avoids copying GC flags from the prebuilt_empty_array if before_len < newsize: p = before_len else: p = newsize rgc.ll_arraycopy(items, newitems, 0, 0, p) l.items = newitems
def test_ll_arraycopy_2(): TYPE = lltype.GcArray(lltype.Void) a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) rgc.ll_arraycopy(a1, a2, 4, 2, 3)
def f(a, b, c, d, e): ll_arraycopy(a, b, c, d, e)
def f(): a1 = lltype.malloc(TYPE, 10) a2 = lltype.malloc(TYPE, 6) rgc.ll_arraycopy(a2, a1, 0, 1, 5)
def ll_arraycopy(source, dest, source_start, dest_start, length): SRCTYPE = typeOf(source) # lltype rgc.ll_arraycopy(source.ll_items(), dest.ll_items(), source_start, dest_start, length)