Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
RVec,
bitmap_rprimitive,
bool_rprimitive,
bytes_rprimitive,
c_pointer_rprimitive,
c_pyssize_t_rprimitive,
dict_rprimitive,
Expand Down Expand Up @@ -1159,7 +1158,8 @@ def get_sequence_type_from_type(self, target_type: Type) -> RType:
if target_type.type.fullname == "builtins.str":
return str_rprimitive
elif target_type.type.fullname == "builtins.bytes":
return bytes_rprimitive
# Items of bytes objects are ints
return int_rprimitive
try:
return self.type_to_rtype(target_type.args[0])
except IndexError:
Expand Down
16 changes: 13 additions & 3 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
c_pyssize_t_rprimitive,
int_rprimitive,
is_bool_or_bit_rprimitive,
is_bytearray_rprimitive,
is_dict_rprimitive,
is_fixed_width_rtype,
is_immutable_rprimitive,
Expand Down Expand Up @@ -257,7 +258,16 @@ def can_preallocate_from_generator(builder: IRBuilder, gen: GeneratorExpr) -> bo
if isinstance(rtype, RTuple):
# for_loop_helper_with_index crashes for empty tuples
return bool(rtype.types)
return is_sequence_rprimitive(rtype) or isinstance(rtype, RVec)
return is_indexed_sequence_rprimitive(rtype) or isinstance(rtype, RVec)


def is_indexed_sequence_rprimitive(rtype: RType) -> bool:
"""Do we iterate over a primitive sequence by index, instead of using an iterator?

We use an iterator for bytearray, since there is no fast unsafe item access for it,
and since overrides of __len__ and __getitem__ in a subclass must not be used.
"""
return is_sequence_rprimitive(rtype) and not is_bytearray_rprimitive(rtype)


def sequence_from_generator_preallocate_helper(
Expand Down Expand Up @@ -722,7 +732,7 @@ def make_for_loop_generator(
return async_obj

rtyp = builder.node_type(expr)
if is_sequence_rprimitive(rtyp) or isinstance(rtyp, RVec):
if is_indexed_sequence_rprimitive(rtyp) or isinstance(rtyp, RVec):
# Special case "for x in <seq>" for concrete sequence types.
expr_reg = accept_loop_state(builder, expr, reassigned)
target_type = builder.get_sequence_type(expr)
Expand Down Expand Up @@ -801,7 +811,7 @@ def make_for_loop_generator(
expr.callee.fullname == "builtins.reversed"
and len(expr.args) == 1
and expr.arg_kinds == [ARG_POS]
and is_sequence_rprimitive(builder.node_type(expr.args[0]))
and is_indexed_sequence_rprimitive(builder.node_type(expr.args[0]))
):
# Special case "for x in reversed(<list>)".
expr_reg = accept_loop_state(builder, expr.args[0], reassigned)
Expand Down
2 changes: 2 additions & 0 deletions mypyc/test-data/fixtures/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ def decode(self, x: str = ..., y: str = ...) -> str: ...
def join(self, x: Iterable[object]) -> bytes: ...
def startswith(self, t: bytes) -> bool: ...
def endswith(self, t: bytes) -> bool: ...
def append(self, x: int) -> None: ...
def clear(self) -> None: ...
def __iter__(self) -> Iterator[int]: ...

class memoryview:
Expand Down
109 changes: 109 additions & 0 deletions mypyc/test-data/irbuild-bytes.test
Original file line number Diff line number Diff line change
Expand Up @@ -399,3 +399,112 @@ def foo(b: bytes, ba: bytearray) -> None:
[out]
main:7: error: Argument 1 to "bytes_func" has incompatible type "bytearray"; expected "bytes"
main:8: error: Argument 1 to "bytearray_func" has incompatible type "bytes"; expected "bytearray"

[case testBytesFor_64bit]
def f(b: bytes) -> int:
t = 0
for c in b:
t += c
return t
[out]
def f(b):
b :: bytes
t :: int
r0, r1 :: native_int
r2, r3, r4 :: bit
r5, r6, r7, r8, c, r9 :: int
r10 :: native_int
L0:
t = 0
r0 = var_object_size b
r1 = 0
L1:
r2 = r1 < r0 :: signed
if r2 goto L2 else goto L8 :: bool
L2:
r3 = r1 <= 4611686018427387903 :: signed
if r3 goto L3 else goto L4 :: bool
L3:
r4 = r1 >= -4611686018427387904 :: signed
if r4 goto L5 else goto L4 :: bool
L4:
r5 = CPyTagged_FromInt64(r1)
r6 = r5
goto L6
L5:
r7 = r1 << 1
r6 = r7
L6:
r8 = CPyBytes_GetItem(b, r6)
c = r8
r9 = CPyTagged_Add(t, c)
t = r9
L7:
r10 = r1 + 1
r1 = r10
goto L1
L8:
return t

[case testBytearrayFor]
def f(b: bytearray) -> int:
t = 0
for c in b:
t += c
return t
[out]
def f(b):
b :: bytearray
t :: int
r0, r1 :: object
r2, c, r3 :: int
r4 :: bit
L0:
t = 0
r0 = PyObject_GetIter(b)
L1:
r1 = PyIter_Next(r0)
if is_error(r1) goto L4 else goto L2
L2:
r2 = unbox(int, r1)
c = r2
r3 = CPyTagged_Add(t, c)
t = r3
L3:
goto L1
L4:
r4 = CPy_NoErrOccurred()
L5:
return t

[case testBytearrayComprehension]
def f(b: bytearray) -> list[int]:
return [c + 1 for c in b]
[out]
def f(b):
b :: bytearray
r0 :: list
r1, r2 :: object
r3, c, r4 :: int
r5 :: object
r6 :: i32
r7, r8 :: bit
L0:
r0 = PyList_New(0)
r1 = PyObject_GetIter(b)
L1:
r2 = PyIter_Next(r1)
if is_error(r2) goto L4 else goto L2
L2:
r3 = unbox(int, r2)
c = r3
r4 = CPyTagged_Add(c, 2)
r5 = box(int, r4)
r6 = PyList_Append(r0, r5)
r7 = r6 >= 0 :: signed
L3:
goto L1
L4:
r8 = CPy_NoErrOccurred()
L5:
return r0
66 changes: 27 additions & 39 deletions mypyc/test-data/irbuild-lists.test
Original file line number Diff line number Diff line change
Expand Up @@ -785,11 +785,9 @@ def test():
r2 :: list
r3 :: native_int
r4, r5, r6 :: bit
r7, r8, r9, r10 :: int
r11 :: object
r12, x, r13 :: int
r14 :: object
r15 :: native_int
r7, r8, r9, r10, x, r11 :: int
r12 :: object
r13 :: native_int
a :: list
L0:
r0 = b'abc'
Expand All @@ -815,15 +813,13 @@ L5:
r8 = r9
L6:
r10 = CPyBytes_GetItem(source, r8)
r11 = box(int, r10)
r12 = unbox(int, r11)
x = r12
r13 = f2(x)
r14 = box(int, r13)
CPyList_SetItemUnsafe(r2, r3, r14)
x = r10
r11 = f2(x)
r12 = box(int, r11)
CPyList_SetItemUnsafe(r2, r3, r12)
L7:
r15 = r3 + 1
r3 = r15
r13 = r3 + 1
r3 = r13
goto L1
L8:
a = r2
Expand All @@ -847,11 +843,9 @@ def test():
r1 :: list
r2 :: native_int
r3, r4, r5 :: bit
r6, r7, r8, r9 :: int
r10 :: object
r11, x, r12 :: int
r13 :: object
r14 :: native_int
r6, r7, r8, r9, x, r10 :: int
r11 :: object
r12 :: native_int
a :: list
L0:
r0 = b'abc'
Expand All @@ -876,15 +870,13 @@ L5:
r7 = r8
L6:
r9 = CPyBytes_GetItem(r0, r7)
r10 = box(int, r9)
r11 = unbox(int, r10)
x = r11
r12 = f2(x)
r13 = box(int, r12)
CPyList_SetItemUnsafe(r1, r2, r13)
x = r9
r10 = f2(x)
r11 = box(int, r10)
CPyList_SetItemUnsafe(r1, r2, r11)
L7:
r14 = r2 + 1
r2 = r14
r12 = r2 + 1
r2 = r12
goto L1
L8:
a = r1
Expand Down Expand Up @@ -914,11 +906,9 @@ def test():
r3 :: list
r4 :: native_int
r5, r6, r7 :: bit
r8, r9, r10, r11 :: int
r12 :: object
r13, x, r14 :: int
r15 :: object
r16 :: native_int
r8, r9, r10, r11, x, r12 :: int
r13 :: object
r14 :: native_int
a :: list
L0:
r0 = __main__.source :: static
Expand Down Expand Up @@ -948,15 +938,13 @@ L7:
r9 = r10
L8:
r11 = CPyBytes_GetItem(r0, r9)
r12 = box(int, r11)
r13 = unbox(int, r12)
x = r13
r14 = f2(x)
r15 = box(int, r14)
CPyList_SetItemUnsafe(r3, r4, r15)
x = r11
r12 = f2(x)
r13 = box(int, r12)
CPyList_SetItemUnsafe(r3, r4, r13)
L9:
r16 = r4 + 1
r4 = r16
r14 = r4 + 1
r4 = r14
goto L3
L10:
a = r3
Expand Down
Loading
Loading