From 7667328ada9cb8ef6bb4c456636c6a7ccab2ad1a Mon Sep 17 00:00:00 2001 From: Phillip Cloud <417981+cpcloud@users.noreply.github.com> Date: Mon, 26 Feb 2024 02:48:35 -0500 Subject: [PATCH] perf(ir): avoid exponential growth on `name` attribute access (#8445) Fixes #8432. In short, the pattern we are using here for accessing `name` results in exponential growth of attribute accesses. --- ibis/expr/operations/core.py | 6 +++++- ibis/expr/tests/test_api.py | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/ibis/expr/operations/core.py b/ibis/expr/operations/core.py index 96d724f1e281..03b16957ad22 100644 --- a/ibis/expr/operations/core.py +++ b/ibis/expr/operations/core.py @@ -85,7 +85,11 @@ def __coerce__( # TODO(kszucs): figure out how to represent not named arguments @property def name(self) -> str: - names = (arg.name for arg in self.__args__ if hasattr(arg, "name")) + names = ( + name + for arg in self.__args__ + if (name := getattr(arg, "name", None)) is not None + ) return f"{self.__class__.__name__}({', '.join(names)})" @property diff --git a/ibis/expr/tests/test_api.py b/ibis/expr/tests/test_api.py index d72a642b088b..edb5adbbeafa 100644 --- a/ibis/expr/tests/test_api.py +++ b/ibis/expr/tests/test_api.py @@ -147,3 +147,12 @@ def test_implicit_coercion_of_null_literal(op): assert expr1.op() == expected assert expr2.op() == expected + + +def test_nested_name_property(): + x = ibis.literal(1) + n = 100 + for _ in range(n): # noqa: F402 + x = x + 1 + + assert x.op().name.count("Add") == n