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

Micro-optimization: avoid Bogus[int] types that cause needless boxing #14354

Merged
merged 1 commit into from
Dec 28, 2022
Merged
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
27 changes: 15 additions & 12 deletions mypy/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
# A placeholder used for Bogus[...] parameters
_dummy: Final[Any] = object()

# A placeholder for int parameters
_dummy_int: Final = -999999


class TypeOfAny:
"""
Expand Down Expand Up @@ -540,8 +543,8 @@ def copy_modified(
values: Bogus[list[Type]] = _dummy,
upper_bound: Bogus[Type] = _dummy,
id: Bogus[TypeVarId | int] = _dummy,
line: Bogus[int] = _dummy,
column: Bogus[int] = _dummy,
line: int = _dummy_int,
column: int = _dummy_int,
) -> TypeVarType:
return TypeVarType(
self.name,
Expand All @@ -550,8 +553,8 @@ def copy_modified(
self.values if values is _dummy else values,
self.upper_bound if upper_bound is _dummy else upper_bound,
self.variance,
self.line if line is _dummy else line,
self.column if column is _dummy else column,
self.line if line == _dummy_int else line,
self.column if column == _dummy_int else column,
)

def accept(self, visitor: TypeVisitor[T]) -> T:
Expand Down Expand Up @@ -658,14 +661,14 @@ def copy_modified(
self,
*,
id: Bogus[TypeVarId | int] = _dummy,
flavor: Bogus[int] = _dummy,
flavor: int = _dummy_int,
prefix: Bogus[Parameters] = _dummy,
) -> ParamSpecType:
return ParamSpecType(
self.name,
self.fullname,
id if id is not _dummy else self.id,
flavor if flavor is not _dummy else self.flavor,
flavor if flavor != _dummy_int else self.flavor,
self.upper_bound,
line=self.line,
column=self.column,
Expand Down Expand Up @@ -1024,10 +1027,10 @@ def accept(self, visitor: TypeVisitor[T]) -> T:
def copy_modified(
self,
# Mark with Bogus because _dummy is just an object (with type Any)
type_of_any: Bogus[int] = _dummy,
type_of_any: int = _dummy_int,
original_any: Bogus[AnyType | None] = _dummy,
) -> AnyType:
if type_of_any is _dummy:
if type_of_any == _dummy_int:
type_of_any = self.type_of_any
if original_any is _dummy:
original_any = self.source_any
Expand Down Expand Up @@ -1745,8 +1748,8 @@ def copy_modified(
name: Bogus[str | None] = _dummy,
definition: Bogus[SymbolNode] = _dummy,
variables: Bogus[Sequence[TypeVarLikeType]] = _dummy,
line: Bogus[int] = _dummy,
column: Bogus[int] = _dummy,
line: int = _dummy_int,
column: int = _dummy_int,
is_ellipsis_args: Bogus[bool] = _dummy,
implicit: Bogus[bool] = _dummy,
special_sig: Bogus[str | None] = _dummy,
Expand All @@ -1766,8 +1769,8 @@ def copy_modified(
name=name if name is not _dummy else self.name,
definition=definition if definition is not _dummy else self.definition,
variables=variables if variables is not _dummy else self.variables,
line=line if line is not _dummy else self.line,
column=column if column is not _dummy else self.column,
line=line if line != _dummy_int else self.line,
column=column if column != _dummy_int else self.column,
is_ellipsis_args=(
is_ellipsis_args if is_ellipsis_args is not _dummy else self.is_ellipsis_args
),
Expand Down