hbmqtt
hbmqtt copied to clipboard
Perf problem with logging plugin
Hi All
Thanks for the awesome work and nice code on hbmqtt. I've recently stumbled onto a perf problem regarding high-throughput writes and the plugin manager. I'm happy to submit a fix (I've fixed it locally) but want to make sure that I'm "doing the right thing". Here's the problem:
- The plugin manager keeps a list of _fired_tasks, so that it can cancel them on close(). This list needs to be searched to remove a task when the task is complete.
- Under high throughput scenarios, and for plugins that do something for each message, searching the delete to delete a task can totally dominate CPU time.
See the attached flamegraph for an example.
As for the solution. Well, I've temporarily just disabled tracking the ongoing tasks. Yes we can't cancel on shutdown, but in my experience it's rarely the case where cancelling is crucial or even generally valuable. Moreover this is called on close and the plugin manager has as lifecycle akin to the broker itself (and the aio loop that runs the tasks) as far as I can tell.
It'd be possible to use a better structure like a map or a set to make removal O(1) instead of O(simultaneous tasks) but honestly can't see the value in the added complexity.
Thoughts?