-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Bypass f-string tag round-trip for var operation operands #6798
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: claude/reflex-perf-optimizations-21kg8y-redis
Are you sure you want to change the base?
Changes from all commits
65dda29
e71be08
7b99834
5f75b20
1bebe58
e4eaa9c
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` no longer registers its operands in the never-evicting `_global_vars` dict (a memory leak, worst under dev hot-reload), and skipping the f-string tag round-trip makes var-operation construction ~25% faster for plain vars and ~2.6x faster for state-var operands. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -930,6 +930,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"): | ||
|
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. P2: Operand f-strings passed through Prompt for AI agents |
||
| return str(self) | ||
|
Comment on lines
+937
to
+938
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.
When the same Useful? React with 👍 / 👎. |
||
|
|
||
| hashed_var = hash(self) | ||
|
|
||
| _global_vars[hashed_var] = self | ||
|
|
@@ -1941,10 +1948,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"] = ( # pyright: ignore[reportIndexIssue] | ||
|
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. P2: Formatting a shared Var in another thread can silently lose its tag while any Prompt for AI agents |
||
| operand_dict.get("_format_without_tagging", 0) + 1 | ||
| ) | ||
| 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 # pyright: ignore[reportIndexIssue] | ||
| else: | ||
| del operand_dict["_format_without_tagging"] # pyright: ignore[reportIndexIssue] | ||
|
Comment on lines
+1959
to
+1973
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 increment loop runs before the |
||
|
|
||
| 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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
P2: Supported
NumberVarformat specs inside avar_operationstill grow_global_vars:NumberVar.__format__returns early with a formatted nested operation before this suppression check is reached. Applying the suppression-aware raw return in the number-format branches would avoid retaining one intermediate Var per operation.Prompt for AI agents