-
Notifications
You must be signed in to change notification settings - Fork 1.2k
#12439 Change _observers from list to set #12440
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
base: trunk
Are you sure you want to change the base?
Conversation
CodSpeed Performance ReportMerging #12440 will degrade performances by 6.61%Comparing Summary
Benchmarks breakdown
|
glyph
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @allrob23 !
A couple of notes:
- obviously the fact that this breaks the test suite needs to be addressed :)
- please make a dedicated branch for this feature rather than using your fork's
trunkbranch for it - can you do this in 2 PRs, where the first adds a benchmark, and the second improves the benchmark? if this is a performance-oriented fix, given that we are lucky enough to have a performance regression suite on https://codspeed.io, we should use that suite to make sure it actually makes the difference that we think it does.
|
And, let's not forget the most important part: thank you for taking the time to contribute to Twisted. |
|
Hi @glyph, do you have any insights on this test failure? Could it be a flaky test? It’s passing in my environment: Test: test_trace |
|
@allrob23 The The tests are executed using You can use trial directly or use |
|
I understand, thanks for the explanation, maybe adding a |
|
I understand the overall message. I'm short on time right now, but I'll take care of it as soon as I’m available. |
It's possible that code was relying upon the ordering of log observers previously, and it's not entirely clear to me that they would have been wrong to do so. If we want to make the ordering non-deterministic, we should probably have some tooling to allow developers to test with an intentionally randomized or at least intentionally-different order (perhaps reversed) before unilaterally changing the behavior. |
for more information, see https://pre-commit.ci
|
I ran this benchmark separately in PR #12445, and on my machine the results were encouraging. After the changes, the observer add/remove performance improved significantly, especially for larger sets. |
|
please review |
adiroiban
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR.
I see that a performance test is failing, this needs to be fixed before merging.
I have never used the Twisted logger in production so I am not familiar with it's use cases.
In the documentation I see
Furthermore, no guarantees are made as to the order in which observers are called, so the effect of such modifications on other observers may be non-deterministic.
|
|
||
| self.assertEqual(traces[1], ((publisher, o1),)) | ||
| self.assertEqual(traces[2], ((publisher, o1), (publisher, o2))) | ||
| self.assertIn((publisher, o1), traces[1]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just asking. Can we use the stdlib assertCountEqual here ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I don't know if it would work to use assertCountEqual here
|
About the performance check: The change in BTW, on my machine, the Further highlight the overall positive impact. @adiroiban what do you think? |
|
Sorry for the delay. This PR has some improvemtns, but at the same time it contains a regression. I don't have much experience with writing/handling performance tests. Maybe there is a defect in the way I think that log publishing is much more important than add/remove of observers... and it matters that we have a performance issue here. My suggestion is try to get more eyes for this PR by asking for feedback over the mailing list https://mail.python.org/mailman3/lists/twisted.python.org/ |
I am also inclined to think that this is the more significant code path in most applications, but obviously I'd prefer it if both were fast. @allrob23 — I guess I'm curious for a more in-depth explanation of the motivating use-case for #12439 — is there a type of application where you want to add and remove log observers in a tight loop, and/or is there a place where Twisted is doing this itself leading to some other, higher-level slowdown? Thanks! |
|
Thank you for the responses! I quickly noticed that a benchmark test was incorrect, and the DummyObserver was taking up some time. I opened another PR (#12446) to fix this. I'm a bit busy with other tasks right now, but I can dive into the details of how we spotted these issues later. Please review PR #12446, and we can revisit this discussion once it's merged. |
glyph
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we are blocked on #12465 to get this to the point where we could even reasonably ack any perf regression, so I am taking this out of the queue for now.




Scope and purpose
Fixes #12439
This PR improves the performance of the LogPublisher class by replacing the list used for storing observers with a set. This change optimizes the lookup operation from O(N) to O(1) when checking if an observer is already registered. The scope of this PR is limited to this performance improvement and does not introduce any behavioral changes to the class.
Contributor Checklist:
This process applies to all pull requests - no matter how small.
Have a look at our developer documentation before submitting your Pull Request.
Below is a non-exhaustive list (as a reminder):
please review.Our bot will trigger the review process, by applying the pending review label
and requesting a review from the Twisted dev team.