Skip to content

Commit

Permalink
perf: .keys finds the default for every instance, instead of every class
Browse files Browse the repository at this point in the history
This does it only for each class.
  • Loading branch information
maartenbreddels committed Sep 29, 2022
1 parent 892166d commit 07632dd
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion ipywidgets/widgets/widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
#-------------------------------------------------------------------------
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 07632dd

Please sign in to comment.