Skip to content

Commit

Permalink
refactor: improve performance of finding var names
Browse files Browse the repository at this point in the history
  • Loading branch information
iisakkirotko committed Mar 26, 2024
1 parent 821b4ea commit 8395690
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions solara/toestand.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,23 @@ def __init__(self, merge: Callable = merge_state):
if settings.main.log_level in ["DEBUG", "INFO"]:
import inspect

for frame in inspect.stack():
file = frame.filename
if (
not (
file.endswith("solara/toestand.py")
or file.endswith("solara/reactive.py")
or file.endswith("solara/hooks/use_reactive.py")
or file.endswith("reacton/core.py")
or file.endswith("components/markdown.py")
)
and frame.code_context is not None
# All subclasses of ValueBase have at least two calls within this file
frame = sys._getframe(2)
while frame:
file = frame.f_code.co_filename
if not (
file.endswith("solara/toestand.py")
or file.endswith("solara/reactive.py")
or file.endswith("solara/hooks/use_reactive.py")
or file.endswith("reacton/core.py")
or file.endswith("components/markdown.py")
):
if "=" not in frame.code_context[0]:
continue
elif any(op in frame.code_context[0].split("=")[1].lower() for op in ["reactive", "use_memo", "computed", "singleton"]):
declaration = frame.code_context[0].split("=")[0].strip()
frame_info = inspect.getframeinfo(frame)
if frame_info.code_context is None or "=" not in frame_info.code_context[0]:
# For some reason MyPy complains about types even though both are FrameType | None
frame = frame.f_back # type: ignore
elif any(op in frame_info.code_context[0].split("=")[1].lower() for op in ["reactive", "use_memo", "computed", "singleton"]):
declaration = frame_info.code_context[0].split("=")[0].strip()
if ":" in declaration:
declaration = declaration.split(":")[0].strip()
self._varname: Optional[str] = declaration
Expand All @@ -117,6 +118,8 @@ def __init__(self, merge: Callable = merge_state):
elif file.endswith("components/markdown.py"):
self._varname = "markdown_content"
break
else:
frame = frame.f_back # type: ignore
if not hasattr(self, "_varname"):
logger.info("No varname found")
self._varname = None
Expand Down

0 comments on commit 8395690

Please sign in to comment.