Skip to content
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

perf: use class_traits with a cache to speed up creation of widgets #3020

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maartenbreddels
Copy link
Member

Benchmark results for creating a button widgets.Button()

------------------------------------------------ benchmark: 1 tests -----------------------------------------------
Name (time in ms)              Min      Max    Mean  StdDev  Median     IQR  Outliers       OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------
test_performance_create     2.8579  36.1314  3.3029  2.0412  3.0983  0.2583      1;22  302.7645     266           1
-------------------------------------------------------------------------------------------------------------------

After this PR, in combination with ipython/traitlets#639

------------------------------------------------ benchmark: 1 tests -----------------------------------------------
Name (time in ms)              Min      Max    Mean  StdDev  Median     IQR  Outliers       OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------
test_performance_create     2.0695  62.7899  2.9496  3.6363  2.3238  0.8066      2;27  339.0254     290           1
-------------------------------------------------------------------------------------------------------------------

Which is a 30% performance increase.

Note that .class_traits is the same as .traits but is a classmethod, so that we can cache on a class level. Note that add_trait will create a new class, so will play nice with the cache. This is tested in case this changes.

Benchmark results for creating a button `widgets.Button()`
------------------------------------------------ benchmark: 1 tests -----------------------------------------------
Name (time in ms)              Min      Max    Mean  StdDev  Median     IQR  Outliers       OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------
test_performance_create     2.8579  36.1314  3.3029  2.0412  3.0983  0.2583      1;22  302.7645     266           1
-------------------------------------------------------------------------------------------------------------------

After this PR, in combination with ipython/traitlets#639

------------------------------------------------ benchmark: 1 tests -----------------------------------------------
Name (time in ms)              Min      Max    Mean  StdDev  Median     IQR  Outliers       OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------
test_performance_create     2.0695  62.7899  2.9496  3.6363  2.3238  0.8066      2;27  339.0254     290           1
-------------------------------------------------------------------------------------------------------------------

Which is a 30% performance increase.
@maartenbreddels
Copy link
Member Author

Note, without the traitlets PR this result is:

------------------------------------------------ benchmark: 1 tests -----------------------------------------------
Name (time in ms)              Min      Max    Mean  StdDev  Median     IQR  Outliers       OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------------
test_performance_create     2.2499  38.9077  2.6620  2.0604  2.4685  0.2669      1;22  375.6595     316           1
-------------------------------------------------------------------------------------------------------------------

@@ -547,6 +548,13 @@ def add_traits(self, **traits):
self.keys.append(name)
self.send_state(name)

@classmethod
@functools.lru_cache(None)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like an unlimited cache size is a bit dangerous. Maybe it's not in practice though?

@vidartf vidartf added this to the Future milestone Aug 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants