Skip to content

Commit 809ffcc

Browse files
authored
[mypyc] Don't box ints, floats and bools for type() (#22455)
`type(x)` boxed `x` if it was an unboxed `int`, `float` or `bool`, and then read the type of the box. That allocates an object for every `float` and every int outside the small-int cache, only to free it again. For example, in ```python def count_exact_ints(n: int) -> int: k = 0 for i in range(10**6, 10**6 + n): if type(i) is int: k += 1 return k ``` the check was ``` r8 = box(int, i) r9 = CPy_TYPE(r8) dec_ref r8 r10 = load_address PyLong_Type r11 = r9 == r10 dec_ref r9 ``` Now `type(x)` doesn't box: - `float`, `bool` and fixed-width ints (`i64`, `u16` and so on): the type doesn't depend on the value, so it's a `load_address` of `PyFloat_Type`, `PyBool_Type` or `PyLong_Type`, like a reference to `float`, `bool` or `int`. The argument is still evaluated. `type(x) is float` is then a comparison of two constant addresses, without any reference counting, which the C compiler can fold. - `int`: a new primitive, `CPyTagged_Type()`. A short int boxes to an exact `int`, so that's its type. A long int is an object already, and it can be an instance of a subclass of `int`, so the type of that object is returned, as before. So the check above is now ``` r8 = CPyTagged_Type(i) dec_ref i :: int r9 = load_address PyLong_Type r10 = r8 == r9 dec_ref r8 ``` ### Performance Best of 40 rounds. ns per iteration of a compiled loop like the one above: | loop body | master | this PR | | |---|---|---|---| | `if type(i) is int`, `i` past 10**6 | 16.1 | 2.8 | 5.8x | | `if type(i & 255) is int` (small int cache) | 5.4 | 4.0 | 1.4x | | `if type(x) is float` (and `x += 1.0`) | 7.8 | 2.1 | 3.7x | For a `float`, a `bool` or a fixed-width int such as `i64`, `type(x)` is now a constant, so `type(x) is int` with `x: i64` doesn't depend on `x` at all (master: 15.8 ns per iteration, which was mostly boxing `x`).
1 parent 3e61c1a commit 809ffcc

5 files changed

Lines changed: 225 additions & 0 deletions

File tree

‎mypyc/irbuild/specialize.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
isinstance_slice,
172172
isinstance_type,
173173
slow_isinstance_op,
174+
type_of_int_op,
174175
)
175176
from mypyc.primitives.set_ops import isinstance_frozenset, isinstance_set
176177
from mypyc.primitives.str_ops import (
@@ -1545,6 +1546,35 @@ def translate_float(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Valu
15451546
return None
15461547

15471548

1549+
@specialize_function("builtins.type")
1550+
def translate_type(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
1551+
"""Get the type of an unboxed int, float or bool without boxing it.
1552+
1553+
Only these exact types are handled. A union such as 'bool | int' is boxed
1554+
already, and unboxing it as an int would lose the type of a bool.
1555+
"""
1556+
if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:
1557+
return None
1558+
arg = expr.args[0]
1559+
arg_type = builder.node_type(arg)
1560+
if is_int_rprimitive(arg_type):
1561+
# A long int can be an instance of a subclass of int
1562+
return builder.primitive_op(type_of_int_op, [builder.accept(arg)], expr.line)
1563+
if is_bool_rprimitive(arg_type):
1564+
type_name = "builtins.bool"
1565+
elif is_float_rprimitive(arg_type):
1566+
type_name = "builtins.float"
1567+
elif is_fixed_width_rtype(arg_type):
1568+
type_name = "builtins.int"
1569+
else:
1570+
return None
1571+
# The type doesn't depend on the value, but the argument can have side effects
1572+
builder.accept(arg)
1573+
type_obj = builder.load_builtin(type_name, expr.line)
1574+
assert type_obj is not None
1575+
return type_obj
1576+
1577+
15481578
@specialize_function("builtins.ord")
15491579
def translate_ord(builder: IRBuilder, expr: CallExpr, callee: RefExpr) -> Value | None:
15501580
if len(expr.args) != 1 or expr.arg_kinds[0] != ARG_POS:

‎mypyc/lib-rt/CPy.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,6 +1373,17 @@ static inline PyObject *CPy_TYPE(PyObject *obj) {
13731373
return result;
13741374
}
13751375

1376+
// type() of an int, without boxing it
1377+
static inline PyObject *CPyTagged_Type(CPyTagged x) {
1378+
// A short int boxes to an exact int, but a long int is an object that can
1379+
// be an instance of a subclass of int
1380+
if (likely(CPyTagged_CheckShort(x))) {
1381+
Py_INCREF(&PyLong_Type);
1382+
return (PyObject *)&PyLong_Type;
1383+
}
1384+
return CPy_TYPE(CPyTagged_LongAsObject(x));
1385+
}
1386+
13761387
PyObject *CPy_CalculateMetaclass(PyObject *type, PyObject *o);
13771388
PyObject *CPyType_ResolveBases(PyObject *bases);
13781389
PyObject *CPy_GetCoro(PyObject *obj);

‎mypyc/primitives/misc_ops.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,17 @@
344344
error_kind=ERR_NEVER,
345345
)
346346

347+
# type(x) for an int, without boxing x first. This is used by translate_type() in
348+
# irbuild/specialize.py, which checks for the exact type. As a function op it would
349+
# also match a union such as 'bool | int' and unbox it, so that type(True) would give int.
350+
type_of_int_op = custom_primitive_op(
351+
name="builtins.type",
352+
arg_types=[int_rprimitive],
353+
c_function_name="CPyTagged_Type",
354+
return_type=object_rprimitive,
355+
error_kind=ERR_NEVER,
356+
)
357+
347358
# Get 'builtins.type' (base class of all classes)
348359
type_object_op = load_address_op(name="builtins.type", type=object_rprimitive, src="PyType_Type")
349360

‎mypyc/test-data/irbuild-classes.test‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2519,6 +2519,97 @@ L0:
25192519
r0 = CPy_TYPE(x)
25202520
return r0
25212521

2522+
[case testTypeOfUnboxedValue]
2523+
from typing import Union
2524+
2525+
from mypy_extensions import u16
2526+
2527+
def type_of_int(x: int) -> type:
2528+
return type(x)
2529+
2530+
def type_of_float(x: float) -> type:
2531+
return type(x)
2532+
2533+
def type_of_bool(x: bool) -> type:
2534+
return type(x)
2535+
2536+
def type_of_u16(x: u16) -> type:
2537+
return type(x)
2538+
2539+
def type_of_bool_or_int(x: Union[bool, int]) -> type:
2540+
return type(x)
2541+
2542+
def is_exact_int(x: int) -> bool:
2543+
return type(x) is int
2544+
2545+
def is_exact_float(x: float) -> bool:
2546+
return type(x) is float
2547+
2548+
def get_float() -> float:
2549+
return 1.5
2550+
2551+
def type_of_call() -> type:
2552+
return type(get_float())
2553+
[out]
2554+
def type_of_int(x):
2555+
x :: int
2556+
r0 :: object
2557+
L0:
2558+
r0 = CPyTagged_Type(x)
2559+
return r0
2560+
def type_of_float(x):
2561+
x :: float
2562+
r0 :: object
2563+
L0:
2564+
r0 = load_address PyFloat_Type
2565+
return r0
2566+
def type_of_bool(x):
2567+
x :: bool
2568+
r0 :: object
2569+
L0:
2570+
r0 = load_address PyBool_Type
2571+
return r0
2572+
def type_of_u16(x):
2573+
x :: u16
2574+
r0 :: object
2575+
L0:
2576+
r0 = load_address PyLong_Type
2577+
return r0
2578+
def type_of_bool_or_int(x):
2579+
x :: union[bool, int]
2580+
r0 :: object
2581+
L0:
2582+
r0 = CPy_TYPE(x)
2583+
return r0
2584+
def is_exact_int(x):
2585+
x :: int
2586+
r0, r1 :: object
2587+
r2 :: bit
2588+
L0:
2589+
r0 = CPyTagged_Type(x)
2590+
r1 = load_address PyLong_Type
2591+
r2 = r0 == r1
2592+
return r2
2593+
def is_exact_float(x):
2594+
x :: float
2595+
r0, r1 :: object
2596+
r2 :: bit
2597+
L0:
2598+
r0 = load_address PyFloat_Type
2599+
r1 = load_address PyFloat_Type
2600+
r2 = r0 == r1
2601+
return r2
2602+
def get_float():
2603+
L0:
2604+
return 1.5
2605+
def type_of_call():
2606+
r0 :: float
2607+
r1 :: object
2608+
L0:
2609+
r0 = get_float()
2610+
r1 = load_address PyFloat_Type
2611+
return r1
2612+
25222613
[case testDunderNew]
25232614
from __future__ import annotations
25242615
from typing import Any

‎mypyc/test-data/run-misc.test‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,6 +1403,88 @@ def test_range_slice_memoryview() -> None:
14031403
assert is_memoryview(memoryview(b"x")) is True
14041404
assert is_memoryview(b"x") is False
14051405

1406+
[case testTypeOfUnboxedValue]
1407+
from typing import Any, Union
1408+
1409+
from mypy_extensions import i64, u16, u32, u64
1410+
from testutil import assertRaises
1411+
1412+
def type_of_int(x: int) -> type:
1413+
return type(x)
1414+
def type_of_float(x: float) -> type:
1415+
return type(x)
1416+
def type_of_bool(x: bool) -> type:
1417+
return type(x)
1418+
def type_of_i64(x: i64) -> type:
1419+
return type(x)
1420+
def type_of_u16(x: u16) -> type:
1421+
return type(x)
1422+
def type_of_u32(x: u32) -> type:
1423+
return type(x)
1424+
def type_of_u64(x: u64) -> type:
1425+
return type(x)
1426+
def type_of_bool_or_int(x: Union[bool, int]) -> type:
1427+
return type(x)
1428+
def is_exact_int(x: int) -> bool:
1429+
return type(x) is int
1430+
1431+
def float_arg(log: list[str]) -> float:
1432+
log.append("float")
1433+
return 1.5
1434+
def bool_arg(log: list[str]) -> bool:
1435+
log.append("bool")
1436+
return True
1437+
def u16_arg(log: list[str]) -> u16:
1438+
log.append("u16")
1439+
return 5
1440+
def float_error() -> float:
1441+
raise ValueError("float")
1442+
def u16_error() -> u16:
1443+
raise ValueError("u16")
1444+
1445+
type_: Any = type
1446+
MyInt: Any = type_("MyInt", (int,), {})
1447+
1448+
def test_type_of_unboxed_value() -> None:
1449+
big = MyInt(2**100)
1450+
assert type_of_int(5) is int
1451+
assert type_of_int(-(2**100)) is int
1452+
# A long int keeps its object, which can be an instance of a subclass
1453+
assert type_of_int(big) is MyInt
1454+
assert is_exact_int(5) and is_exact_int(2**100)
1455+
assert not is_exact_int(big)
1456+
assert type_of_float(1.5) is float
1457+
assert type_of_bool(True) is bool
1458+
assert type_of_bool(1 > 2) is bool
1459+
assert type_of_i64(-5) is int
1460+
assert type_of_i64(-(2**63)) is int
1461+
assert type_of_u16(2**16 - 1) is int
1462+
assert type_of_u32(2**32 - 1) is int
1463+
assert type_of_u64(2**64 - 1) is int
1464+
n = 2**70
1465+
assert type(n + 1) is int
1466+
assert type(n > 1) is bool
1467+
assert type(n / 2) is float
1468+
1469+
def test_type_of_union() -> None:
1470+
# A union is boxed, so a bool or an instance of an int subclass keeps its type
1471+
small = MyInt(5)
1472+
assert type_of_bool_or_int(True) is bool
1473+
assert type_of_bool_or_int(small) is MyInt
1474+
assert type_of_bool_or_int(5) is int
1475+
1476+
def test_argument_is_evaluated() -> None:
1477+
# The value isn't needed for the type of a float, bool or u16, but it's evaluated
1478+
log: list[str] = []
1479+
assert type(float_arg(log)) is float
1480+
assert type(bool_arg(log)) is bool
1481+
assert type(u16_arg(log)) is int
1482+
assert log == ["float", "bool", "u16"]
1483+
with assertRaises(ValueError, "float"):
1484+
type(float_error())
1485+
with assertRaises(ValueError, "u16"):
1486+
type(u16_error())
1487+
14061488
[case testIsInstanceMixedTuple]
14071489
from typing import Any
14081490
from mypy_extensions import mypyc_attr

0 commit comments

Comments
 (0)