Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[mypyc] Speed up native-to-native calls using await#19398

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JukkaL merged 7 commits intomasterfrommypyc-await-optimize-2
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from1 commit
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
PrevPrevious commit
NextNext commit
WIP minimal fast path works
  • Loading branch information
@JukkaL
JukkaL committedJul 8, 2025
commitfc1d373d2e90d558d8dd2fe93f206f2abcc015e2
2 changes: 2 additions & 0 deletionsmypyc/irbuild/context.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -167,6 +167,8 @@ def __init__(self, ir: ClassIR) -> None:
# Holds the arg passed to send
self.send_arg_reg: Value | None = None

self.stop_iter_value_reg: Value | None = None

# The switch block is used to decide which instruction to go using the value held in the
# next-label register.
self.switch_block = BasicBlock()
Expand Down
3 changes: 2 additions & 1 deletionmypyc/irbuild/generator.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -376,11 +376,12 @@ def setup_env_for_generator_class(builder: IRBuilder) -> None:
exc_tb = builder.add_local(Var("traceback"), object_rprimitive, is_arg=True)
# TODO: Use the right type here instead of object?
exc_arg = builder.add_local(Var("arg"), object_rprimitive, is_arg=True)
stop_iter_value_arg = builder.add_local(Var("stop_iter_value"), object_pointer_rprimitive, is_arg=True)
stop_iter_value_arg = builder.add_local(Var("stop_iter_ptr"), object_pointer_rprimitive, is_arg=True)
# TODO: do something with stop_iter_value_arg

cls.exc_regs = (exc_type, exc_val, exc_tb)
cls.send_arg_reg = exc_arg
cls.stop_iter_value_reg = stop_iter_value_arg

cls.self_reg = builder.read(self_target, fitem.line)
if builder.fn_info.can_merge_generator_and_env_classes():
Expand Down
16 changes: 16 additions & 0 deletionsmypyc/irbuild/nonlocalcontrol.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,11 +14,14 @@
Branch,
Goto,
Integer,
LoadErrorValue,
Register,
Return,
SetMem,
Unreachable,
Value,
)
from mypyc.ir.rtypes import object_rprimitive
from mypyc.irbuild.targets import AssignmentTarget
from mypyc.primitives.exc_ops import restore_exc_info_op, set_stop_iteration_value

Expand DownExpand Up@@ -108,10 +111,23 @@ def gen_return(self, builder: IRBuilder, value: Value, line: int) -> None:
# StopIteration instead of using RaiseStandardError because
# the obvious thing doesn't work if the value is a tuple
# (???).

true, false = BasicBlock(), BasicBlock()
r = builder.fn_info.generator_class.stop_iter_value_reg

builder.add(Branch(r, true, false, Branch.IS_ERROR))

builder.activate_block(true)

builder.call_c(set_stop_iteration_value, [value], NO_TRACEBACK_LINE_NO)
builder.add(Unreachable())
builder.builder.pop_error_handler()

builder.activate_block(false)

# Assign to provided pointer instead of raising an exception
builder.add(SetMem(object_rprimitive, r, value))
builder.add(Return(Integer(0, object_rprimitive)))

class CleanupNonlocalControl(NonlocalControl):
"""Abstract nonlocal control that runs some cleanup code."""
Expand Down
12 changes: 10 additions & 2 deletionsmypyc/irbuild/statement.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -958,21 +958,29 @@ def emit_yield_from_or_await(

if isinstance(iter_reg.type, RInstance) and iter_reg.type.class_ir.has_method(helper_method):
# Second fast path optimization: call helper directly (see also comment above).
fast_path = True
obj = builder.read(iter_reg)
nn = builder.none_object()
m = MethodCall(obj, helper_method, [nn, nn, nn, nn, Integer(0, object_pointer_rprimitive)], line)
stop_iter_val = Register(object_rprimitive)
builder.assign(stop_iter_val, Integer(0, object_rprimitive), line)
ptr = builder.add(LoadAddress(object_pointer_rprimitive, stop_iter_val))
m = MethodCall(obj, helper_method, [nn, nn, nn, nn, ptr], line)
# Generators have custom error handling, so disable normal error handling.
m.error_kind = ERR_NEVER
_y_init = builder.add(m)
else:
fast_path = False
_y_init = builder.call_c(next_raw_op, [builder.read(iter_reg)], line)

builder.add(Branch(_y_init, stop_block, main_block, Branch.IS_ERROR))

# Try extracting a return value from a StopIteration and return it.
# If it wasn't, this reraises the exception.
builder.activate_block(stop_block)
builder.assign(result, builder.call_c(check_stop_op, [], line), line)
if fast_path:
builder.assign(result, stop_iter_val, line)
else:
builder.assign(result, builder.call_c(check_stop_op, [], line), line)
# Clear the spilled iterator/coroutine so that it will be freed.
# Otherwise, the freeing of the spilled register would likely be delayed.
err = builder.add(LoadErrorValue(iter_reg.type))
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp