From 07632dd6c097f58dfcec2b80d13a196088a37d4f Mon Sep 17 00:00:00 2001 From: "Maarten A. Breddels" Date: Thu, 29 Sep 2022 13:02:37 +0200 Subject: [PATCH] perf: .keys finds the default for every instance, instead of every class This does it only for each class. --- ipywidgets/widgets/widget.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ipywidgets/widgets/widget.py b/ipywidgets/widgets/widget.py index c8756da551..8b8fb2d481 100644 --- a/ipywidgets/widgets/widget.py +++ b/ipywidgets/widgets/widget.py @@ -304,6 +304,8 @@ def reg(widget): else: return reg(name) +# speed up .keys generation at widget instance creation time +_keys_cache = {} class Widget(LoggingHasTraits): #------------------------------------------------------------------------- @@ -460,7 +462,10 @@ def get_view_spec(self): @default('keys') def _default_keys(self): - return [name for name in self.traits(sync=True)] + cls = type(self) + if cls not in _keys_cache: + _keys_cache[cls] = [name for name in self.traits(sync=True)] + return _keys_cache[cls].copy() _property_lock = Dict() _holding_sync = False