-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Bypass f-string tag round-trip for var operation operands #6747
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| `@var_operation` operands no longer pay the f-string tag round-trip (hash + permanent `_global_vars` registration + regex decode), cutting ~30-40% of var-operation construction cost and stopping internal operations from growing the never-evicting `_global_vars` dict. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -840,6 +840,13 @@ def __format__(self, format_spec: str) -> str: | |
| Returns: | ||
| The formatted var. | ||
| """ | ||
| # Operands of a running ``var_operation`` body interpolate as their | ||
| # raw JS expression: their VarData flows through the operation's | ||
| # ``_args``, so the tag round-trip (and its permanent ``_global_vars`` | ||
| # entry) is pure overhead there. See ``var_operation``. | ||
| if self.__dict__.get("_format_without_tagging"): | ||
| return self._js_expr | ||
|
Comment on lines
+847
to
+848
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because this flag is checked by every Useful? React with 👍 / 👎. |
||
|
|
||
| hashed_var = hash(self) | ||
|
|
||
| _global_vars[hashed_var] = self | ||
|
|
@@ -1856,10 +1863,34 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> Var[T]: | |
| for key, value in kwargs.items() | ||
| } | ||
|
|
||
| operands = [*args_vars.values(), *kwargs_vars.values()] | ||
| # Suppress f-string tagging for the operands while the body runs: | ||
| # their VarData reaches the operation through ``_args`` below, so the | ||
| # tag round-trip (hash + permanent ``_global_vars`` entry + regex | ||
| # decode of the return expression) is pure overhead. The suppression | ||
| # is ref-counted so a nested operation on the same var cannot clear | ||
| # an outer suppression early; vars created inside the body still tag | ||
| # normally and keep contributing VarData via the return expression. | ||
| for operand in operands: | ||
| operand_dict = operand.__dict__ | ||
| operand_dict["_format_without_tagging"] = ( | ||
| operand_dict.get("_format_without_tagging", 0) + 1 | ||
| ) | ||
|
Comment on lines
+1874
to
+1878
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The suppression counter is stored on the shared operand instance. If the same |
||
| try: | ||
| return_var = func(*args_vars.values(), **kwargs_vars) # pyright: ignore [reportCallIssue] | ||
| finally: | ||
| for operand in operands: | ||
| operand_dict = operand.__dict__ | ||
| remaining = operand_dict["_format_without_tagging"] - 1 | ||
| if remaining: | ||
| operand_dict["_format_without_tagging"] = remaining | ||
| else: | ||
| del operand_dict["_format_without_tagging"] | ||
|
|
||
| return CustomVarOperation.create( | ||
| name=func.__name__, | ||
| args=tuple(list(args_vars.items()) + list(kwargs_vars.items())), | ||
| return_var=func(*args_vars.values(), **kwargs_vars), # pyright: ignore [reportCallIssue, reportReturnType] | ||
| return_var=return_var, # pyright: ignore [reportArgumentType] | ||
| ).guess_type() | ||
|
|
||
| return wrapper | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an operation result is used as another operation's operand, such as
(a + b) * c, the operand is aCustomVarOperationwhose_js_expris empty and whose__str__renders the actual expression. This branch returns the empty field directly, so the outer operation can emit incomplete JavaScript instead of the nested expression.