Skip to content

[mypyc] Speed up for loop over native generator #19415

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

Merged
merged 4 commits into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from 1 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
Prev Previous commit
Next Next commit
Refactor
  • Loading branch information
JukkaL committed Jul 9, 2025
commit a08363f87638c04531dd50db71ff9298623451c9
9 changes: 4 additions & 5 deletions mypyc/irbuild/for_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
short_int_rprimitive,
)
from mypyc.irbuild.builder import IRBuilder
from mypyc.irbuild.prepare import GENERATOR_HELPER_NAME
from mypyc.irbuild.targets import AssignmentTarget, AssignmentTargetTuple
from mypyc.primitives.dict_ops import (
dict_check_size_op,
Expand Down Expand Up @@ -518,10 +519,9 @@ def make_for_loop_generator(
if iterable_expr_reg is None:
iterable_expr_reg = builder.accept(expr)

helper_method = "__mypyc_generator_helper__"
it = iterable_expr_reg.type
for_obj: ForNativeGenerator | ForIterable
if isinstance(it, RInstance) and it.class_ir.has_method(helper_method):
if isinstance(it, RInstance) and it.class_ir.has_method(GENERATOR_HELPER_NAME):
# Directly call generator object methods if iterating over a native generator.
for_obj = ForNativeGenerator(builder, index, body_block, loop_exit, line, nested)
else:
Expand Down Expand Up @@ -656,14 +656,13 @@ def init(self, expr_reg: Value, target_type: RType) -> None:
def gen_condition(self) -> None:
builder = self.builder
line = self.line
helper_method = "__mypyc_generator_helper__"
self.return_value = Register(object_rprimitive)
err = builder.add(LoadErrorValue(object_rprimitive, undefines=True))
builder.assign(self.return_value, err, line)
ptr = builder.add(LoadAddress(object_pointer_rprimitive, self.return_value))
nn = builder.none_object()
helper_call =(
MethodCall(builder.read(self.iter_target), helper_method, [nn, nn, nn, nn, ptr], line)
helper_call = MethodCall(
builder.read(self.iter_target), GENERATOR_HELPER_NAME, [nn, nn, nn, nn, ptr], line
)
# We provide custom handling for error values.
helper_call.error_kind = ERR_NEVER
Expand Down
4 changes: 2 additions & 2 deletions mypyc/irbuild/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

from collections import defaultdict
from collections.abc import Iterable
from typing import NamedTuple
from typing import Final, NamedTuple

from mypy.build import Graph
from mypy.nodes import (
Expand Down Expand Up @@ -71,7 +71,7 @@
from mypyc.options import CompilerOptions
from mypyc.sametype import is_same_type

GENERATOR_HELPER_NAME = "__mypyc_generator_helper__"
GENERATOR_HELPER_NAME: Final = "__mypyc_generator_helper__"


def build_type_map(
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy