Skip to content

[mypyc] feat: extend stararg fastpath from #19629 with star2 fastpath #19630

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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9fc66f8
[mypyc] feat: reuse existing tuple when calling fn(*args)
BobTheBuidler Aug 9, 2025
4db8e93
chore: cleanup
BobTheBuidler Aug 9, 2025
d709425
chore: update IR
BobTheBuidler Aug 9, 2025
301080a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2025
e028a72
fix: mypy errs
BobTheBuidler Aug 9, 2025
3315a97
Merge branch 'tupe' of https://github.com/BobTheBuidler/mypy into tupe
BobTheBuidler Aug 9, 2025
9057461
fix: mypy errs
BobTheBuidler Aug 9, 2025
d3b3f0d
chore: refactor
BobTheBuidler Aug 9, 2025
27a13a7
feat: extend stararg fastpath logic to handle lists and generic seque…
BobTheBuidler Aug 9, 2025
3e72105
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2025
3da4bf2
update IR
BobTheBuidler Aug 9, 2025
567b8a4
Merge branch 'stararg-fastpath' of https://github.com/BobTheBuidler/m…
BobTheBuidler Aug 9, 2025
2f0e0fe
fix mypy errs
BobTheBuidler Aug 9, 2025
82e39c4
[mypyc] feat: extend stararg fastpath from #19629 with star2 fastpath
BobTheBuidler Aug 9, 2025
aed4a8b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2025
2be0326
fix: separate fastpath for dict vs nondict
BobTheBuidler Aug 9, 2025
23536da
Merge branch 'star2arg-fastpath' of https://github.com/BobTheBuidler/…
BobTheBuidler Aug 9, 2025
847ea62
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Aug 9, 2025
f260ada
Update ll_builder.py
BobTheBuidler Aug 9, 2025
f5f96df
Update ll_builder.py
BobTheBuidler Aug 9, 2025
e4af7e4
Merge branch 'master' into star2arg-fastpath
BobTheBuidler Aug 13, 2025
6b8b293
Merge branch 'master' into star2arg-fastpath
BobTheBuidler Aug 14, 2025
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
56 changes: 46 additions & 10 deletions mypyc/irbuild/ll_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@
from mypyc.primitives.bytes_ops import bytes_compare
from mypyc.primitives.dict_ops import (
dict_build_op,
dict_copy,
dict_copy_op,
dict_new_op,
dict_ssize_t_size_op,
dict_update_in_display_op,
Expand Down Expand Up @@ -184,7 +186,12 @@
str_ssize_t_size_op,
unicode_compare,
)
from mypyc.primitives.tuple_ops import list_tuple_op, new_tuple_op, new_tuple_with_length_op
from mypyc.primitives.tuple_ops import (
list_tuple_op,
new_tuple_op,
new_tuple_with_length_op,
sequence_tuple_op,
)
from mypyc.rt_subtype import is_runtime_subtype
from mypyc.sametype import is_same_type
from mypyc.subtype import is_subtype
Expand Down Expand Up @@ -788,23 +795,52 @@ def _construct_varargs(

for value, kind, name in args:
if kind == ARG_STAR:
# star args fastpath
if star_result is None:
# fast path if star expr is a tuple:
# we can pass the immutable tuple straight into the function call.
if is_tuple_rprimitive(value.type):
if len(args) == 1:
# fn(*args)
return value, self._create_dict([], [], line)
elif len(args) == 2 and args[1][1] == ARG_STAR2:
# fn(*args, **kwargs)
if len(args) == 1:
# fn(*args)
if is_list_rprimitive(value.type):
value = self.primitive_op(list_tuple_op, [value], line)
elif not is_tuple_rprimitive(value.type):
value = self.primitive_op(sequence_tuple_op, [value], line)
# we can pass the immutable tuple straight into the function call.
return value, self._create_dict([], [], line)
elif len(args) == 2 and args[1][1] == ARG_STAR2:
# fn(*args, **kwargs)
# TODO: extend to cover(*args, **k, **w, **a, **r, **g, **s)
if is_tuple_rprimitive(value.type):
star_result = value
continue
elif is_list_rprimitive(value.type):
star_result = self.primitive_op(list_tuple_op, [value], line)
else:
star_result = self.primitive_op(sequence_tuple_op, [value], line)

star2_arg = args[1]
star2_value = star2_arg[0]
if is_dict_rprimitive(star2_value.type):
star2_fastpath_op = dict_copy_op
else:
star2_fastpath_op = dict_copy
return star_result, self.primitive_op(
star2_fastpath_op, [star2_value], line
)
# elif ...: TODO extend this to optimize fn(*args, k=1, **kwargs) case
# TODO optimize this case using the length utils - currently in review
star_result = self.new_list_op(star_values, line)
self.primitive_op(list_extend_op, [star_result, value], line)
elif kind == ARG_STAR2:
if star2_result is None:
if len(args) == 1:
# early exit with fastpath if the only arg is ARG_STAR2
# TODO: can we maintain an empty tuple in memory and just reuse it again and again?
if is_dict_rprimitive(value.type):
star2_fastpath_op = dict_copy_op
else:
star2_fastpath_op = dict_copy
return self.new_tuple([], line), self.primitive_op(
star2_fastpath_op, [value], line
)

star2_result = self._create_dict(star2_keys, star2_values, line)

self.call_c(dict_update_in_display_op, [star2_result, value], line=line)
Expand Down
2 changes: 1 addition & 1 deletion mypyc/primitives/dict_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
)

# Construct a dictionary from another dictionary.
function_op(
dict_copy_op = function_op(
name="builtins.dict",
arg_types=[dict_rprimitive],
return_type=dict_rprimitive,
Expand Down
2 changes: 1 addition & 1 deletion mypyc/primitives/tuple_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
)

# Construct tuple from an arbitrary (iterable) object.
function_op(
sequence_tuple_op = function_op(
name="builtins.tuple",
arg_types=[object_rprimitive],
return_type=tuple_rprimitive,
Expand Down
Loading
Loading