Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mypyc] Faster int conversion in str.format #10774

Merged
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
19 changes: 14 additions & 5 deletions mypyc/irbuild/specialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,10 @@
)
from mypyc.ir.rtypes import (
RType, RTuple, str_rprimitive, list_rprimitive, dict_rprimitive, set_rprimitive,
bool_rprimitive, is_dict_rprimitive, c_int_rprimitive, is_str_rprimitive,
c_pyssize_t_rprimitive
bool_rprimitive, c_int_rprimitive, c_pyssize_t_rprimitive, is_dict_rprimitive,
is_int_rprimitive, is_str_rprimitive, is_short_int_rprimitive
)
from mypyc.primitives.int_ops import int_to_str_op
from mypyc.primitives.dict_ops import (
dict_keys_op, dict_values_op, dict_items_op, dict_setdefault_spec_init_op
)
Expand Down Expand Up @@ -373,9 +374,17 @@ def translate_str_format(

literals = split_braces(format_str)

variables = [builder.accept(x) if is_str_rprimitive(builder.node_type(x))
else builder.call_c(str_op, [builder.accept(x)], expr.line)
for x in expr.args]
# Convert variables to strings
variables = []
for x in expr.args:
node_type = builder.node_type(x)
if is_str_rprimitive(node_type):
var_str = builder.accept(x)
elif is_int_rprimitive(node_type) or is_short_int_rprimitive(node_type):
var_str = builder.call_c(int_to_str_op, [builder.accept(x)], expr.line)
else:
var_str = builder.call_c(str_op, [builder.accept(x)], expr.line)
variables.append(var_str)

# The first parameter is the total size of the following PyObject* merged from
# two lists alternatively.
Expand Down
2 changes: 1 addition & 1 deletion mypyc/primitives/int_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
error_kind=ERR_MAGIC)

# str(n) on ints
function_op(
int_to_str_op = function_op(
name='builtins.str',
arg_types=[int_rprimitive],
return_type=str_rprimitive,
Expand Down
50 changes: 20 additions & 30 deletions mypyc/test-data/irbuild-str.test
Original file line number Diff line number Diff line change
Expand Up @@ -169,35 +169,25 @@ def f(s: str, num: int) -> None:
def f(s, num):
s :: str
num :: int
r0 :: object
r1, r2, r3, r4, r5, s1, r6, s2, r7, s3 :: str
r8 :: object
r9 :: str
r10 :: object
r11 :: str
r12 :: object
r13, r14, r15, r16, r17 :: str
r0, r1, r2, r3, r4, s1, r5, s2, r6, s3, r7, r8, r9, r10, r11, r12, r13 :: str
L0:
r0 = box(int, num)
r1 = PyObject_Str(r0)
r2 = "Hi! I'm "
r3 = ", and I'm "
r4 = ' years old.'
r5 = CPyStr_Build(5, r2, s, r3, r1, r4)
s1 = r5
r6 = ''
s2 = r6
r7 = 'abc'
s3 = r7
r8 = box(int, num)
r9 = PyObject_Str(r8)
r10 = box(int, num)
r11 = PyObject_Str(r10)
r12 = box(int, num)
r13 = PyObject_Str(r12)
r14 = '}'
r15 = '{'
r16 = '}{'
r17 = CPyStr_Build(6, r14, r9, r15, r11, r16, r13)
s3 = r17
r0 = CPyTagged_Str(num)
r1 = "Hi! I'm "
r2 = ", and I'm "
r3 = ' years old.'
r4 = CPyStr_Build(5, r1, s, r2, r0, r3)
s1 = r4
r5 = ''
s2 = r5
r6 = 'abc'
s3 = r6
r7 = CPyTagged_Str(num)
r8 = CPyTagged_Str(num)
r9 = CPyTagged_Str(num)
r10 = '}'
r11 = '{'
r12 = '}{'
r13 = CPyStr_Build(6, r10, r7, r11, r8, r12, r9)
s3 = r13
return 1