-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
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 |
|---|---|---|
|
|
@@ -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"): | ||
| return str(self) | ||
|
|
||
| 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] | ||
| 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 | ||
|
|
||
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 the same
Varis shared across threads, this instance-dict flag changes__format__process-wide while an operation body is running. A concurrentf"{var}"therefore returns the raw expression without its tag, so aVarconstructed from that string loses the original imports/hooks; overlapping operations can also race the non-atomic reference-count updates and leave the flag set or raise during cleanup. Use thread/task-local suppression rather than mutating the shared immutable operand.Useful? React with 👍 / 👎.