Skip to content

Commit

Permalink
Assignment expression code optimization x 2.
Browse files Browse the repository at this point in the history
This commit is the last in a commit chain significantly optimizing
generation of **assignment expressions** (i.e., PEP 572-compliant Python
expressions containing the ":=" walrus operator) throughout code
snippets dynamically generated by @beartype's type-checking code
generator. Since strings are expensive to repeatedly generate, @beartype
now internally caches and reuses frequently used local variables across
assignment expressions. In truth, this means nothing. But @leycec just
*had* to do this to satisfy his aberrant OCD disorder. I swear! It isn't
my fault! It's fast or it ain't @beartype. (*Holy bawling bowlers!*)
  • Loading branch information
leycec committed Mar 9, 2024
1 parent c32ed7d commit 5424d27
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 22 deletions.
2 changes: 1 addition & 1 deletion beartype/_check/checkmagic.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@
'''
Substring prefixing all local variables providing a **pith** (i.e., either the
current parameter or return value *or* item contained in the current parameter
or return value being type-checked by the current call).
or return value type-checked by the current call).
'''


Expand Down
26 changes: 10 additions & 16 deletions beartype/_check/code/codemake.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from beartype._check.checkmagic import (
ARG_NAME_CLS_STACK,
ARG_NAME_GETRANDBITS,
VAR_NAME_PITH_PREFIX,
VAR_NAME_PITH_ROOT,
)
from beartype._check.code.codemagic import (
Expand All @@ -44,6 +43,7 @@
add_func_scope_type_or_types,
express_func_scope_type_ref,
)
from beartype._check.code.snip.codesnipcls import PITH_INDEX_TO_VAR_NAME
from beartype._check.code.snip.codesnipstr import (
CODE_HINT_CHILD_PLACEHOLDER_PREFIX,
CODE_HINT_CHILD_PLACEHOLDER_SUFFIX,
Expand Down Expand Up @@ -507,14 +507,14 @@ class variable or method annotated by this hint *or* :data:`None`).
# effectively a leaf node with respect to performing this optimization.

# Integer suffixing the name of each local variable assigned the value of
# the current pith in a Python >= 3.8-specific assignment expression, thus
# uniquifying this variable in the body of the current wrapper function.
# the current pith in a assignment expression, thus uniquifying this
# variable in the body of the current wrapper function.
#
# Note that this integer is intentionally incremented as an efficient
# low-level scalar rather than as an inefficient high-level
# "itertools.Counter" object. Since both are equally thread-safe in the
# internal context of this function body, the former is preferable.
pith_curr_assign_expr_name_counter = 0
pith_curr_var_name_index = 0

# Python >= 3.8-specific assignment expression assigning this full Python
# expression to the local variable assigned the value of this expression.
Expand Down Expand Up @@ -1034,18 +1034,12 @@ def _enqueue_hint_child(pith_child_expr: str) -> str:
# and thus unnecessarily inefficient code.
if pith_curr_expr is not VAR_NAME_PITH_ROOT:
# Increment the integer suffixing the name of this variable
# *BEFORE* defining this local variable.
pith_curr_assign_expr_name_counter += 1

#FIXME: Optimize away this inefficient string formatting
#with an efficient dictionary lookup implemented similarly
#to that of the "indent_curr" assignment, above.
# Reduce the current pith expression to the name of this
# local variable.
pith_curr_var_name = (
f'{VAR_NAME_PITH_PREFIX}'
f'{pith_curr_assign_expr_name_counter}'
)
# *BEFORE* defining this variable.
pith_curr_var_name_index += 1

# Name of this local variable.
pith_curr_var_name = PITH_INDEX_TO_VAR_NAME[
pith_curr_var_name_index]

# Assignment expression assigning this full expression to
# this variable.
Expand Down
89 changes: 89 additions & 0 deletions beartype/_check/code/snip/codesnipcls.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,93 @@
'''

# ....................{ IMPORTS }....................
from beartype._check.checkmagic import VAR_NAME_PITH_PREFIX

# ....................{ SUBCLASSES }....................
class PithIndexToVarName(dict):
'''
**Local pith variable name cache** (i.e., dictionary mapping from the
1-based index uniquely identifying each **pith** (i.e., current parameter or
return value *or* item contained in the current parameter or return value
type-checked by the current call in the body of a runtime type-checker
dynamically generated by :mod:`beartype`) to the corresponding name of a
prospective local variable assigned that value in that body).
See Also
--------
:data:`.PITH_INDEX_TO_VAR_NAME`
Singleton instance of this dictionary subclass.
'''

# ....................{ DUNDERS }....................
def __missing__(self, pith_index: int) -> str:
'''
Dunder method explicitly called by the superclass
:meth:`dict.__getitem__` method implicitly called on the first ``[``-
and ``]``-delimited attempt to access a local pith variable name
uniquely identified by the passed 1-based index.
Caveats
-------
**This method intentionally prohibits 0.** Why? Because the existing
:data:`beartype._check.checkmagic.VAR_NAME_PITH_ROOT` string global
already efficiently caches the local root pith variable name, which is
sufficiently common to warrant globalization.
Parameters
----------
pith_index : int
1-based index suffixing the local pith variable name to be created,
cached, and returned.
Returns
-------
str
Prospective name of this local pith variable.
Raises
------
AssertionError
If either:
* ``pith_level`` is *not* an integer.
* ``pith_level`` is a **non-positive integer** (i.e., is less than
or equal to 0).
'''
assert isinstance(pith_index, int), f'{repr(pith_index)} not integer.'
assert pith_index > 0, f'{pith_index} <= 0.'
# print(f'Generating indentation level {indent_level}...')

# Prospective name of this local pith variable.
pith_var_name = f'{VAR_NAME_PITH_PREFIX}{pith_index}'

# Cache this name.
self[pith_index] = pith_var_name

# Return this name.
return pith_var_name

# ....................{ MAPPINGS }....................
PITH_INDEX_TO_VAR_NAME = PithIndexToVarName()
'''
**Indentation cache singleton** (i.e., global dictionary efficiently mapping
from 1-based indentation levels to the corresponding indentation string
constant).
Caveats
-------
**Indentation string constants should always be accessed via this cache rather
than manually generated.** This cache dynamically creates and efficiently caches
indentation string constants on the first access of those constants, obviating
the performance cost of string formatting required to create these constants.
Examples
--------
.. code-block:: pycon
>>> from beartype._check.code.snip.codesnipcls import PITH_INDEX_TO_VAR_NAME
>>> PITH_INDEX_TO_VAR_NAME[1]
'__beartype_pith_1'
>>> PITH_INDEX_TO_VAR_NAME[2]
'__beartype_pith_2'
'''
10 changes: 5 additions & 5 deletions beartype/_data/code/datacodeindent.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ def __missing__(self, indent_level: int) -> str:
* ``indent_level`` is a **non-positive integer** (i.e., is less than
or equal to 0).
'''
assert isinstance(indent_level, int)
assert indent_level > 0
assert isinstance(indent_level, int), (
f'{repr(indent_level)} not integer.')
assert indent_level > 0, f'{indent_level} <= 0.'
# print(f'Generating indentation level {indent_level}...')

# String constant indented to this level of indentation.
Expand All @@ -67,8 +68,7 @@ def __missing__(self, indent_level: int) -> str:
# Return this string constant.
return indent_code

# ....................{ DICTS }....................
#FIXME: Unit test us up, please.
# ....................{ MAPPINGS }....................
INDENT_LEVEL_TO_CODE = IndentLevelToCode()
'''
**Indentation cache singleton** (i.e., global dictionary efficiently mapping
Expand All @@ -93,7 +93,7 @@ def __missing__(self, indent_level: int) -> str:
' '
'''

# ....................{ CODE ~ indent }....................
# ....................{ STRINGS }....................
CODE_INDENT_1 = INDENT_LEVEL_TO_CODE[1]
'''
Code snippet expanding to a single level of indentation.
Expand Down
Empty file.
53 changes: 53 additions & 0 deletions beartype_test/a00_unit/a60_check/a00_code/snip/test_codesnipcls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env python3
# --------------------( LICENSE )--------------------
# Copyright (c) 2014-2024 Beartype authors.
# See "LICENSE" for further details.

'''
Beartype **type-checking expression snippet class unit tests.**
This submodule unit tests the public API of the public
:mod:`beartype._check.code.snip.codesnipcls.PITH_INDEX_TO_VAR_NAME` submodule.
'''

# ....................{ IMPORTS }....................
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# WARNING: To raise human-readable test errors, avoid importing from
# package-specific submodules at module scope.
#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# ....................{ TESTS }....................
def test_pith_index_to_var_name() -> None:
'''
Test the :obj:`beartype._check.code.snip.codesnipcls.PITH_INDEX_TO_VAR_NAME`
dictionary singleton.
'''

# ....................{ IMPORTS }....................
# Defer test-specific imports.
from beartype._check.code.snip.codesnipcls import PITH_INDEX_TO_VAR_NAME
from beartype._check.checkmagic import VAR_NAME_PITH_PREFIX
from pytest import raises

# ....................{ PASS }....................
# Assert this dictionary indexed by various positive integers creates and
# returns the expected local pith variable names.
assert PITH_INDEX_TO_VAR_NAME[1] == f'{VAR_NAME_PITH_PREFIX}1'
assert PITH_INDEX_TO_VAR_NAME[2] == f'{VAR_NAME_PITH_PREFIX}2'

# Assert this dictionary internally caches these constants.
assert PITH_INDEX_TO_VAR_NAME[1] is PITH_INDEX_TO_VAR_NAME[1]
assert PITH_INDEX_TO_VAR_NAME[2] is PITH_INDEX_TO_VAR_NAME[2]

# ....................{ FAIL }....................
# Assert that attempting to index this dictionary by non-integer indices
# raises the expected exception.
with raises(AssertionError):
PITH_INDEX_TO_VAR_NAME[2.34]

# Assert that attempting to index this dictionary by non-positive indices
# raises the expected exception.
with raises(AssertionError):
PITH_INDEX_TO_VAR_NAME[0]
with raises(AssertionError):
PITH_INDEX_TO_VAR_NAME[-1]

0 comments on commit 5424d27

Please sign in to comment.